I'm drawing onto a transparent background, but in each loop, I'd like the screen to be cleared, instead of drawing on the screen from before.
Is there a way to wipe the drawing screen in Java?
You may be looking for Graphics#clearRect(). You'll probably need to set the surface background to a transparent color, though:
graphics.setBackground(new Color(255, 255, 255, 0));
Related
I'm using libgdx particle editor to make my particle effects, and I noticed that when the background is black everything is fine but when the background isn't black, or if I render the particle in front of a sprite the particle color is affected.
with black background
with colored background
initialization:
ParticleEffect effect = Pools.obtain(ParticleEffect.class);
effect.load(Gdx.files.internal("effects/myEffect.p"), Gdx.files.internal("effects"));
effect.setPosition((pos.x - 0.3f) * 30, (pos.y - 6) * 30);
effect.start();
rendering:
effect.draw(batch, Gdx.graphics.getDeltaTime())
What I'm trying to do is render the particles with its original color even if the background isn't black. Thanks in advance
Simple, with the editor has an option called "aditive" that is well down, disable it and will have particles with alpha 1.
I cannot find any info on how to draw shapes on a graphic canvas, making sure what I draw is not the same color as the background
there are some solutions out there but they all use images drawing only with per pixel operations/loops or filters; I also tried different Composite operations, but none suit what I want
so lets say I do
g.setColor(color.white) // relevant in this case ? not sure
g.fillRect(...)
I want the rectangle to be in inverted colors of the background so it is always visible
sorry I cant provide more code, I really dont know how to achieve this
thanks
Your paint method could retrieve the current color, and search for its complementary color:
Color originalColor = g.getColor();
g.setColor(complementaryColor(originalColor));
g.fillRect(0, 0, 50, 50);
The complementaryColor method is inspired from this topic : Reverse opposing colors
Color complementaryColor(final Color bgColor) {
Color complement = new Color(255 - bgColor.getRed(),
255 - bgColor.getGreen(),
255 - bgColor.getBlue());
return complement;
}
I have a JLayeredPane with 2 JPanels inside of it.
The one on top has a semi transparant background color:
this.setBackground(new Color(0, 0, 0, 150));
Now when i set the size of this JPanel to be equal to the size of the frame
`
this.ghostPanel.setSize(width, height);
My whole JPanel turns gray, but when i give it height - 1, It displays the correct transparant view.
this.ghostPanel.setSize(width, height - 1);
I don't really understand why this happens. I would love to find a valid explanation!
Thanks in advance!
Swing components are either opaque or transparent, Swing doesn't know how to paint components which have a alpha based color.
By using a alpha based color, Swing doesn't know that it should paint the components below it when the component is updated, so you be up with some weird paint artefacts
Instead, make the component transparent (setOpaque(false)), then override it's paintComponent method and use a AlphaComposite to fill the background
I am new to Java, What I am trying to do is simple, I want to make a JLabel have a transparent color when the mouse enters the label area and get the label to its original color when the mouse leaves the label area.
I suppose this is simple and in my code works but I get a strange effect when the mouse is on the label. This is the code that I use:
In the event mouseEntered of the label:
private void lblNuevoCLMouseEntered(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
bgcolor=(new Color(0, 0, 0, 100));
lblNuevoCL.setBackground(bgcolor);
}
In the event mouseExited:
private void lblNuevoCLMouseExited(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
lblNuevoCL.setBackground(new java.awt.Color(206,206,255));
}
This is what happens when the mouse enters the label( the square to the left of the image):
it gets transparent using an RGB color with alpha but apart from getting transparent
a strange background appears on the label as you see in the picture.
Make the JLabel opaque to allow the background color to be set
lblNuevoCL.setOpaque(true);
What you need to do is to make the background of the JLabel magically disappear. The way to do this is to use setOpaque().
What setOpaque does is this (taken from docs):
If true the component paints every pixel within its bounds.
Otherwise, the component may not paint some or all of its pixels,
allowing the underlying pixels to show through.
The last line has the solution to your problem.
When the mouse enters, you use lblNuevoCL.setOpaque(false); to make it have a transparent
background
lblNuevoCL.setOpaque(true); when the mouse exits so that it goes back to its original colors. That way, you do not have to worry about what color the background is.
Remember that Ubuntu's JLabel will have a different background color than that of Windows. hardcoding the color can cause it to look weird.
it gets transparent using an RGB color with alpha
See Backgrounds With Transparency for an explanation (and a couple of solutions) of the problems you will have when you use transparency.
i have a transparent JFrame AWTUtilities.setWindowOpaque(this, false);
I have a problem when resizing the window. I need something that could clear the background of the window before drawing on it,I need to make it all the background empty and transparent. Now the painting is draw over the old background and looks ugly.
I tried to draw a transparent image over the background but i have same issues.
If you have an opaque component you're completely responsible for drawing its content. The windowing system or AWT does NOTHING to set the background to some defined state.
So at least you should do something like the basic Canvas code
g.clearRect(0, 0, width, height);
What exactly do you mean with "transparent". Do you want to look through to the desktop? Do you want to see panel behind your component (then at least it should not be "opaque").