I want to make a shape transparant (the shape should be semi-opaque). How can I do this in Java? This is a part of my code:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(40, 40, 40, 40);
}
The color you are currently using, Color.RED, does not use alpha, which is basically how transparent your color will be.
g.setColor(new Color(255, 0, 0, 125));
This will create a new color, using RGBA. The color I created uses 255 for red, 0 for blue and 0 for green. 125 is the alpha
Related
How do I get Java graphic location information? do I get the location information of the 2D object that I created? I created the object as follows. I need the location information of these objects.
public void paint(Graphics g) {
//backGround
g.setColor(Color.darkGray);
g.fillRect(1, 1, 400, 400);
//left object
g.setColor(Color.green);
g.fillRect(Pxi, Pyi, 20, 20);
//right object
g.setColor(Color.yellow);
g.fillRect(Pxs, Pys, 20, 20);
g.dispose();
}
g.fillRect (Pxs, Pys, 20, 20) I need the location information of these objects.
If you use g.fillRect(x, y, 20, 20) you draw colored rectangle into your screen (I mean Canvas or JPanel) at x and y position. In your case Pxs and Pys coordinates of drawing in user space.I think you should read this to understand more about Java2D API: https://docs.oracle.com/javase/tutorial/2d/overview/coordinate.html
I want to draw a circle on a buffered image that act like a png
i want to use this circle in order to replace the mouse cursor for a paint application i am working on.
i cant download a circle png from google as i need to keep changing the size of this circle depending on the width of the tool i am using it for.
here is my attempt so far
public static BufferedImage getCircle() {
BufferedImage bufferedImage = new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB);
Color transparent = new Color(0x00FFFFFF, true);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
//trying to make the bufferedImage transparent
g.setComposite(AlphaComposite.Src);
g.setColor(transparent);
g.setBackground(transparent);
g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
//drawing the circle
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.black);
g.drawOval(0, 0, 200, 200);
return bufferedImage;
}
it should look like:
However my code currently only creates a white square.
Your code has two problems (as already shown in the comments). The first is that you draw a circle with a radius of 200px into an image of dimensions 30px. If you closely look you can barely see a black pixel in the lower right corner.
Fix it by adjusting your dimensions such that it fits inside, for example like:
BufferedImage bufferedImage = new BufferedImage(60, 60, BufferedImage.TYPE_INT_RGB);
...
g.drawOval(5, 5, 50, 50);
Next is that you want to achieve a transparent background. To do so you need to set the type of the image to a color model which supports transparency, like ARGB (A = Alpha = transparency) instead of RGB:
BufferedImage bufferedImage = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);
Last you probably want to increase the thickness of your border to achieve the image you showed. You do so by using g.setStroke(...):
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(5));
g.drawOval(5, 5, 50, 50);
With this setting you achieve the following result (with transparency):
Play with the values to adjust the circle to your exact needs.
I'm making a game in which there will be red and blue Shapes moving around on screen. I've looked high and low for how to make anywhere they overlap a different color (purple). I am only using Java2D, which to my understanding does not support Shaders. I looked into drawing the red shapes to one BufferedImage and the blue shapes to another, then trying to use AlphaComposite to combine the colors and draw it to the screen, but it never produced correct results. I'm using 127,0,0 and 0,0,127 for red and blue instead of 255 because 255,0,255 looks, in my opinion, terrible for purple. I would effectively like this.
Thanks to copeg's suggestion, I was able to figure it out. Here is the code segment (Context: the shapes I'm drawing are attacks):
//Attacks
BufferedImage attackImg = new BufferedImage(S_WIDTH, S_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D ag = (Graphics2D) attackImg.getGraphics();
//Make all of attackImg a transparent image
ag.setComposite(AlphaComposite.Clear);
ag.fillRect(0, 0, S_WIDTH, S_HEIGHT);
ag.setComposite(AlphaComposite.SrcOver);
//Render red attacks to attackImg
ag.setColor(new Color(127, 0, 0, 255));
for(Shape s : redAttacks)
ag.fill(s);
//Render overlap areas using composites to attackImg
ag.setColor(new Color(127, 0, 127, 255));
ag.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
g.setColor(new Color(0, 0, 127, 255));
for(Shape s : blueAttacks)
{
ag.fill(s);
g.fill(s); //Render blue attacks
}
//Render red and purple attacks
g.drawImage(attackImg, 0, 0, null);
I'm new to Java GUI. I have two questions. Is there a way to repeat a gradient image horizontally like you would when working with CSS? If not, what is the conventional way of creating a gradient in Java?
In Swing, the GradientPaint class can be used to draw a gradient. Below is an example that will draw a square containing a linear gradient between white and red (assuming this code is within a class that extends JComponent):
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
GradientPaint gradient = new GradientPaint(0,0,Color.WHITE, 100, 0, Color.RED);
g2d.setPaint(gradient);
g2d.fillRect(0,0,100,100);
}
I working on a project in which I have to simulate a memory manager and show some memory snapshots. I have created a draw class via examples I have found here in which I override the paintComponet(). Everything draws fine.
I would like to be able to draw a rectangle to represent a memory partition and then overlay another rectangle over top to represent an incoming job (ie Job1 is in this partition3). What seems to occur is that I add the partition first (which will always be the case) and then when I add the job it will sit behind the partition block. Is there a way other than drawing the Job first to shift these after the job is created?
Here is the paint override
#Override public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// set up rendering to allow anti-aliasing
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// create the rectangle to represent the memory partition block
// x = address position h = amount of memory (y & w are predefined for the display block)
Rectangle2D rect = new Rectangle2D.Double(x, y, w, h); // create the rectangle
g2d.setPaint(partColor); // set it's color
g2d.fill(rect); // fill it in
// create the transparency for the text
Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
g2d.setComposite(comp);
// draw the text with color, type and size and center the text in the block created above
g2d.setPaint(Color.black);
g2d.setFont(new Font("Tahoma", Font.PLAIN, 12));
g2d.drawString(text, (int)((w / 2) - (text.length()/2)), (int)h/2);
}
The call to draw is in my window class (this will place the partition in front of the job) but I need to order to be reversed without changing the order of the calls.
// Draw both Text and Block with transparency
DrawPartition part1 = new DrawPartition(Color.blue, 0, 0, 110, 100, "part1");
part1.setBounds(5, 5, 110, 100);
snapPanel.add(part1);
DrawJob job1 = new DrawJob(Color.green, 0, 0, 110, 100, "Job 1");
job1.setBounds(5, 15, 110, 100);
snapPanel.add(job1);
Is there some reason you can't do this?
// Draw both Text and Block with transparency
DrawPartition part1 = new DrawPartition(Color.blue, 0, 0, 110, 100, "part1");
part1.setBounds(5, 5, 110, 100);
DrawJob job1 = new DrawJob(Color.green, 0, 0, 110, 100, "Job 1");
job1.setBounds(5, 15, 110, 100);
snapPanel.add(job1);
snapPanel.add(part1);
A more general answer would be to add a z component to each of your rectangles. Then you can loop through your rectangles in the paintComponent method, drawing them in z order.