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
Related
I am somewhat new to java, and I'm trying to make a game that has large "pixels" that are actually 5*5 pixels large. I have tried to turn the graphics into an image and then resize the image, but it always returns a huge amount of errors. I am implementing ActionListener and using timer to create a game loop, if that means anything important.
I have the variable screen set as a createVolatileImage(160, 160);
public void actionPerformed(ActionEvent e) {
timer.start();
Graphics draw = screen.getGraphics();
draw.setColor(new Color(50, 100, 50));
draw.fillRect(0, 0, 100, 100);
draw.setColor(Color.black);
draw.drawString("Text",10,10);
draw = getGraphics();
draw.drawImage(screen, 0, 0, 800, 800, 0, 0, 160, 160, null);
draw.dispose();
}
I want to set database data on Image Label at specific position
dragLabel.setHorizontalTextPosition(JLabel.CENTER);
dragLabel.setVerticalTextPosition(JLabel.BOTTOM);
I found exact which i wanted...
Try this
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hi", 30, 10); //these are x and y positions
g.drawString("jj", 30, 20);
}
};
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
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.
I would like to draw some shapes on a JPanel by overriding paintComponent. I would like to be able to pan and zoom. Panning and zooming is easy to do with AffineTransform and the setTransform method on the Graphics2D object. After doing that I can easyli draw the shapes with g2.draw(myShape) The shapes are defined with the "world coordinates" so it works fine when panning and I have to translate them to the canvas/JPanel coordinates before drawing.
Now I would like to change the quadrant of the coordinates. From the 4th quadrant that JPanel and computer often uses to the 1st quadrant that the users are most familiar with. The X is the same but the Y-axe should increase upwards instead of downwards. It is easy to redefine origo by new Point(origo.x, -origo.y);
But How can I draw the shapes in this quadrant? I would like to keep the coordinates of the shapes (defined in the world coordinates) rather than have them in the canvas coordinates. So I need to transform them in some way, or transform the Graphics2D object, and I would like to do it efficiently. Can I do this with AffineTransform too?
My code for drawing:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.blue);
AffineTransform at = g2.getTransform();
at.translate(-origo.x, -origo.y);
at.translate(0, getHeight());
at.scale(1, -1);
g2.setTransform(at);
g2.drawLine(30, 30, 140, 20);
g2.draw(new CubicCurve2D.Double(30, 65, 23, 45, 23, 34, 67, 58));
}
This is an off the cuff answer, so it's untested, but I think it will work.
Translate by (0, height). That should reposition the origin to the lower left.
Scale by (1, -1). That should flip it about the x axis.
I don't think the order of operations matters in this case.