Draw transparent path - java

I'm developing a java application for draw through the drag of the mouse.
I need the JPanel to be transparent, but in this way lose the function rubber.
With the getBackground() can set the transparent color to use, but the path is not going to erase what was previously drawn.

Drawing with transparent means... drawing with transparent, so the previous color stays intact. What you want is clearing a part of the image (you're drawing on an image surface, right?). That can be done using the clearing rule of AlphaComposite.
// Assuming Graphics2D g, maybe from BufferedImage
g.setComposite(AlphaComposite.Clear);
// drawing now clears

Related

Change initial background color in libgdx without glClearColor?

is it possible to change the default background color in libgdx. i want to change the background color from black to whatever color i desire, and not clear the screen. i am using ShapeRenderer to create a rectangle that is filled. i then have that rectangle move around the screen and i want to keep the previously rendered rectangle.
using Gdx.gl.glClearColor() and Gdx.gl.glClear() obviously re-renders the background color over top the previous, clearing the previously rendered rectangle.
if i dont use the Gdx.gl.glClearColor() and Gdx.gl.glClear() the background is black and the rectangle rendering and movement is what i want, other then the fact that the default background color is black. witch is not the desired look i need.
The frame buffer has no concept of what is background and not. If you are working exclusively with the screen's frame buffer, the only way to change the color is to clear the screen and redraw everything. So in your case you would need to store the steps you used to render everything the first time and replay those steps after clearing the screen.
What might be easier would be to use a separate frame buffer for all the content of your scene, and draw that over the background. This would require a frame buffer with an alpha component, and you would need to use blendFuncSeparate when rendering so alpha is preserved in this frame buffer so it can be drawn over the background. You would use a transparent clear color within the frame buffer, and whatever color you want on the main frame buffer.
libGDX FrameBuffers are a big topic and too much to explain here. You should start with the documentation, and if you start implementing it and have issues you can create a new question.

How to draw Image pattern with javaFX Canvas

I want to draw a repeating image on a javafx canvas, is there any way to do it other than manually reapeatedly drawing the same image over and over?
This can be done by setting the Paint for filling some area. A subclass of Paint is
https://openjfx.io/javadoc/12/javafx.graphics/javafx/scene/paint/ImagePattern.html
which does exactly what you want.

Rotate text present on JLabel without rotating border

I want to rotate the text present on my JComponent to vertical, JComponent also contains border painted in paintComponent method, I don't want to rotate that border, only text.
I already used graphics2D rotate function, but it rotate component border as well, which fails when my component is rectangle.
Please suggest me any approch to rotate only text.
This is currently my JComponent :
And what I want :
Actually it's not duplicate. I already used below code:
Graphics2D g2 = (Graphics2D) g;
g2.rotate(Math.PI / 4, bi.getWidth() / 2, bi.getHeight() / 2);
but problem is that, it also rotate border, I don't want that.
All the solutions given are not working, it rotate the border as well. I don't want to rotate the border, only text.
This is what I get after rotate with some angle:
Remember that the Graphics object has a lot of state in it. This includes the current transformation. Your current code modifies the state and does not restore it to the original situation.
After drawing your image, you should "undo" the rotation. This can be done either by rotating in the other the direction or by creating a new graphics object (g2.create()) specifically to draw the rotated content. If you do the latter, make sure to dispose() the temporary graphics object you created.

How to prevent JPanel from repainting everything?

Making a paint-like application that works by saving mouse points in an arraylist. My idea is to have a "points" arraylist with all the previously drawn stuff, and a "temp" arraylist to get and modify the current brush stroke the user just entered. This is necessary as the user can change the color and size, so my idea is to modify the current brush stroke based off what buttons have been pressed, then add that brush stroke to the rest of the picture. I searched around StackOverflow and found some code but cant get it to work how I want to (assuming I found the right code).
#Override
public void paintComponent(Graphics g1) {
super.paintComponent(g1);
final Graphics2D g = (Graphics2D)g1.create();
try {
g.setColor(brushColor);
for (Point point : tempArrayList){
g.fillOval(point.x, point.y, brushSize, brushSize);
}
} finally {
g.dispose();
}
The problem is that I need to clear the tempArrayList for the next brush stroke, which I can do when they change the color/size, but then it erases what was previously there. I am starting to think that I don't even need the "points" arraylist as descibed above because I was hoping that the g1 graphic would just save what the g graphic created.
I guess I just need to figure out how to add the g graphic to g1
Painting is controlled by the Swing API, there is a expectation that whenever paintComponent is called, you will repaint the entire state of the component (as part of your component might have been damaged due to some system event), so the short answer is, no, you can't...however....
You Could...
Paint to a BufferedImage instead and paint the BufferedImage when paintComponent is called
You Could...
Establish a series of "paintable" objects which contain information about what is to be painted and how it is to the painted, including the brush stroke, stroke color and fill color.
These would then be added to some kind of List and "painted" when the paintComponent method is called
Check out Custom Painting Approaches for exampled of painting from:
A List of objects
A BufferedImage.
The examples show how to draw Rectangles of different colours.

With Java's graphics shape drawing operations, is there an easy way to give the shapes transparency gradients and blurry edges?

What I would like is to give circles a fill color with a gradient that starts from the middle and then as it moves out to the edges becomes progressively more transparent, giving a blur effect. What is the simplest way of doing this?
Try setting an appropiately defined java.awt.RadialGradientPaint (using Colors with alpha), then render your circles using that. You may need to translate the graphics coordinate system to get the gradient centered in the circle. (http://docs.oracle.com/javase/7/docs/api/java/awt/RadialGradientPaint.html)
Or just make an image in a graphics program and simply draw the image.

Categories

Resources