When trying to draw an arrowhead using Java Graphics2D by using points and inputting the coordinates.
g2.drawLine(xLeft+290, yTop + 80, xLeft+290,yTop + 100); //first arrow
Polygon arrowHead = new Polygon();
arrowHead.addPoint(xLeft+290, yTop+140);//firstpoint
arrowHead.addPoint(xLeft+285, yTop+120);//secondpoint
arrowHead.addPoint(xLeft+295, yTop+120);//thirdpoint
g2.fillPolygon(arrowHead);
This is my code for drawing the line and the arrowhead, I'm not sure at all of the coordinates are correct but I can't see the shape if it will not appear.
Related
I have a board of Hexagons that were each a bigger rectangle.
g.setColor(getBackground());
g.fillPolygon(hexagon);
g.setColor(getForeground());
g.drawPolygon(hexagon);
Whenever each hexagon is hovered over, the entire rectangle is brought to the front, resulting in a white rectangle appearing around the edge overlapping other hexagons.
http://prntscr.com/bfiixt <-- example of the problem.
How can I make this background transparent so I don't have thew problem, or how could I stop the button being brought to the front?
Thanks.
FontMetrics fm = getFontMetrics(getFont());
Rectangle viewR = getBounds();
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
SwingUtilities.layoutCompoundLabel(this, fm, getText(), null, SwingUtilities.CENTER,
SwingUtilities.CENTER, SwingUtilities.BOTTOM, SwingUtilities.CENTER, viewR, iconR, textR, 0);
Point location = getLocation();
g.drawString(getText(), textR.x - location.x, textR.y - location.y + fm.getAscent());
Fixed by adding this code:
btn.setContentAreaFilled(false);
btn.setFocusPainted(false);
btn.setBorderPainted(false);
For others having this problem, this fixed it by changing anything outside of the hexagon inside the object to transparent.
I was not able to figure out how to fill the drawn rectangle based on (x,y) points. Basically inside (x,y) points. There can be a array of (x,y) points.
Anyone can help on this.....
Rectangle r = new Rectange(length, width, x, y);
r.fill(Color.yourColor);
Graphics g = getGraphics();
g.draw(r);
I'm currently trying to rotate a polygon using the Affine Transform class. Using the rotate method, the polygon's graphical representation updates, but the bounding box of the polygon doesn't update. How can I rotate the polygon in addition to updating it's coordinates?
Create a new Shape instead of just rotating the polygon as your paint it. For example:
Polygon shape = new Polygon();
shape.addPoint(...);
....
Rectangle bounds = shape.getBounds();
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(angle), bounds.width / 2, bounds.height / 2);
Path2D path = (shape instanceof Path2D) ? (Path2D)shape : new GeneralPath(shape);
Shape rotated = path.createTransformedShape( transform );
System.out.println(rotated.getBounds());
I have a problem with rotating image in a fixed position with Graphcis2D and AffineTransform.
The idea is to rotate an image according to body's rotation.
The rotation is happening correctly as the rotation angle of the image matches the angle of the body's rotation. However, as the rotation takes place, the image is not drawn to the same position as the body should be drawn. The code of the method painting the picture is the following:
public void paintPicture(Graphics g, Body body) {
Graphics2D g2 = (Graphics2D) g;
Vector2f[] vertices = ((Box) body.getShape()).getPoints(body.getPosition(), body.getRotation());
Vector2f topLeftCorner = vertices[0];
AffineTransform oldTransform = g2.getTransform();
AffineTransform at = new AffineTransform();
at.rotate(body.getRotation());
g2.setTransform(at);
g2.drawImage(this.img, (int) topLeftCorner.x, (int) topLeftCorner.y, null);
g2.setTransform(oldTransform);
}
Any ideas what might cause the movement of the image instead of drawing it according to the coordinates (topLeftCorner.x, topLeftCorner.y)?
You need to first translate you object so the anchor point (the point around which you want it to rotate) is at the origin, perform your rotation, then translate it back. So if you want to rotate around the point (50, 75), you'd do the following:
at.translate (-50, -75);
at.rotate (body.getRotation());
at.translate (50, 75);
I'm assuming that your AffineTransform class can accumulate transformations. If not, you'll need 3 different transforms.
Ok when a user draws a Rectangle on top of an image, I want to find all the rotated Rectangles ahead of time for all image rotation angles (90,180,270,360).
According to Java API I can just keep calling the Graphics2D rotate() method. I can then use this Graphics2D transformer to get the rotated Rectangle.
This works for the very first rotation(1.5708) call. I get the correct Rectangle Point. All other calls after that return the wrong Rectangle Point after using Transformer.
I think my problem is the Graphics2D translate(x,y). I don't understand how to use it.
Anyone knows how to fix my code so that it will return the correct Rectangle after every rotation?
Thank you.
public void rotateRectangles(BufferedImage bim,int width,int height,Rectangle rect){
BufferedImage bim = new BufferedImage(height, width,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) (bufferedImage.createGraphics());
g2d.translate(bufferedImage.getHeight(),0);
//Get Rectangle for 90 degree image rotation. This always good.
g2d.rotate(1.5708);
Shape shape = g2d.getTransform().createTransformedShape(rect);
Rectangle rotatedRect = shape.getBounds();
System.out.println("rotated rectangle at 90 degrees. Point x="+rotatedRect.x+" y="+rotatedRect.y);
//Get Rectangle for 180 degree image rotation. Getting wrong rotatedRect.
g2d.rotate(1.5708);
shape = g2d.getTransform().createTransformedShape(rect);
rotatedRect = shape.getBounds();
System.out.println("rotated rectangle at 180 degrees. Point x="+rotatedRect.x+" y="+rotatedRect.y);
//Get Rectangle for 270 degree image rotation. Getting wrong rotatedRect.
g2d.rotate(1.5708);
shape = g2d.getTransform().createTransformedShape(rect);
rotatedRect = shape.getBounds();
System.out.println("rotated rectangle at 270 degrees. Point x="+rotatedRect.x+" y="+rotatedRect.y);
//Get Rectangle for 360 degree image rotation.Getting wrong rotatedRect.
g2d.rotate(1.5708);
shape = g2d.getTransform().createTransformedShape(rect);
rotatedRect = shape.getBounds();
System.out.println("rotated rectangle at 360 degrees. Point x="+rotatedRect.x+" y="+rotatedRect.y);
}
Thank you.
Instead of rotating the graphics context's affine transform via g2d.rotate(), consider using createTransformedShape() as suggested in this example.
Addendum: Note in particular that the baseline of the example Polygon is initially centered on the origin. As a result, the initial transformation is rotation around the origin. In your case, you can use the rotate() method that includes an anchor point, which would be the center of your rectangle.