I am trying to create a simple rectangle using an AffineTransform. Here is kinda what I am wanting to do...
AffineTransform at = new AffineTransform();
at.transform(width/2, height/2);
switch(direction){
case 1:
return new Rectangle(at, width, height);
case 2:
return new Rectangle(at, width*2, height*2);
}
I'm not sure how, or if you can, create a rectangle using an AffineTransform. If anyone knows how to do this please share your information.
After applying an AffineTransform, a Rectangle is no longer necessarily an ordinary Rectangle. A Rectangle however derives from Rectangle2D which implements Shape.
And an AffineTransform can transform a Shape into a new Shape with its createTransformedShape method.
That's a best way to go about it, if you want the full power of AffineTransform. You can draw a Shape, test whether points fall inside or outside it, turn it into an Area, etc.
Related
I am creating a small java 2D game and i want to know if there is any way to rotate AWT rectangle
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform) origXform.clone();
newXform.rotate(angle, pivotX, pivotY); // pivotX ,pivotY is the starting
point of the hand image
g2d.setTransform(newXform);
Rectangle arm = new Rectangle(bowX + 5, bowY + 55, 60, 5);
g2d.drawImage(playerBowReadyImg, bowX, bowY, null); //hand image
on the above code i simply draw the hand image which rotates based on the mouse position, i also set the rectangle on the hand but the problem is rectangle is not rotating along with the hand image.
also i am not using rectangle for any drawing purpose but to detect the collision.
however using g2d.draw(arm); draws the rotated rectangle but it not actually rotate the rectangle it just draws the rotated one.
any suggestion is appreciated.
Ok my question is marked as duplicate so i tried the answers i find there but the code i get only rotate my rectangle for the draw purpose only.
Image to depict the problem
now to be more specific the arrow in the image can only detect the collision for the blue rectangle (the original position) instead of the red one (rotated rectangle).
again i don't want to actually draw the rectangle but want to detect the collision when the arrow collide with the rectangle.
See AffineTransform.createTransformedShape(Shape) which:
Returns a new Shape object defined by the geometry of the specified Shape after it has been transformed by this transform.
I'm wondering if there is a way to create a shape fixture in Java 2D.
More specifically, I'm trying to be able to draw a predefined shape at different locations with different colors.
I know you can use the fill(shape) methods to draw shapes.
However, this seems to require creating a new shape at the coordinate that I want to draw it.
Is there any way to reuse the same shape each time? Or do I have to create a new shape for each location.
You can do this by translating the transformation matrix of the graphics object.
Let's say you have a Shape called shape whose coordinates are relative to the center of the Shape. You also have an instance of Graphics2D called g2.
Now you code could look something like this:
// Set the color of the Shape.
g2.setColor(Color.BLACK);
// Backup the transformation matrix so we can restore it later.
AffineTransform backupTransform = new AffineTransform(g2.getTransform());
// Translate everything that is drawn afterwards by the given coordinates.
// (This will be the new position of the center of the Shape)
g2.translate(53, 27);
// Draw the Shape.
g2.draw(shape);
// Restore the old transform, so that things drawn after this line
// are not affected by the translation.
g2.setTransform(backupTransform);
I'm attempting to rotate a Rectangle (the Java class) created with
Rectangle rect = new Rectangle(x, y, width, height);
I cannot appear to find anything in the API for it. If I google "Java rotate a rectangle", then I can only find threads telling me how to draw it rotated, and not just rotate the object.
AffineTransform.getRotateInstance(angle).createTransformedShape(rect) creates a Shape representing the rotated rectangle. (It can't return another Rectangle because Rectangle expects to just be straight along the x/y axes.)
I have a Graphics2D object which I use to draw on my Canvas. I draw multiple shapes on the Canvas and want to transform only one (or part) of them.
I'll try to keep this simple:
void render(Graphics2D g) {
... // Draw shape 1
... // Draw shape 2
... // Draw shape 3
}
How would I go about rotating shape 2 while leaving shape 1 and 3 intact? By "rotate" I mean rotating around its center point, which we can define as x and y for example.
I've been looking for a way to do this for a while now, but couldn't find anything that works the way I want it to.
Is there any simple way to do this?
Rather than rotating the shape around it's centre point, rotate and then translate the canvas. To rotate around the centre of the shape at (x, y), first translate the canvas by (-x, -y) and then rotate the canvas -d degrees and draw the shape as normal at (0,0).
When you're done, rotate back then translate back (note that with these geometric transformations the order is important, translating and then rotating will give you a completely different outcome).
This means that you can still draw an object at any rotation without having to recalculate the coordinates yourself.
AffineTransform afx = new AffineTransform();
afx.rotate(angleRad, s.getCenter().x, s.getCenter().y);
//afx.rotate(angleRad);
java.awt.Shape ss = afx.createTransformedShape(s.getPrimativeShape());
return ss;
s is my wrapper class for java.awt.Shape and does some stuff with it.... But what you want is in line 2.
afx.rotate(Angle,xAnchorPoint,yAnchorPoint);
afx.rotate rotates the object about the point (xAnchorPoint;yAnchorPoint).
Hope this is what you wanted
In order to rotate a shape, use one of the Graphics2D.rotate methods.
Cache the transform used for both shape 1 and shape 3. Before drawing shape 3, ensure that you reset the transform to the cached one, since using rotate for shape 2 will alter the current transform coordinates.
Steps:
Cache current transform
Draw shape 1
Rotate
Draw shape 2
Set transform to cached one
Draw shape 3
Are there any in-built methods in the Java API which would allow me to resize a polygon?
Would it be quite a simple task to write my own method, or are there any others already made?
No, nothing built in, altho, when you draw the polygon, you can draw the polygon with a transformation matrix applied which could scale the polygon for you. (or rotate, skew, etc, etc).
see
Graphics2D.setTransform(transform);
Lets assume you are drawing the polygon in a JPanel, and you have overridden the paintComponent method of JPanel. Cast the Graphics object to a Graphics2D object, and use transforms to scale it as appropriate:
public void paintComponent(Graphic g) {
Graphics2D g2d = (Graphics2D) g;
AffineTransform saveTransform = g2d.getTransform();
try {
AffineTransform scaleMatrix = new AffineTransform();
scaleMatrix.scale(1.5, 1.5);
//or whatever you want
g2d.setTransform(scaleMatrix);
g2d.drawPolygon(myPolygon);
} finally {
g2d.setTransform(saveTransform);
}
}
Chances are you can set up the transformation matrix elsewhere (once) instead of each time in the paintComponent method, but i did here to show how to do it.
Also note, that this will move the polygon, you would probably want apply this to the transform:
add a translate to move the polygon to the origin
add a scale
add a translate to move the polygon back to the original position
in this way, the object doesn't move it just scales.
Yes, AffineTransform.createTransformedShape(Shape) will create a transformed copy of any Shape (which may be a Polygon or Path2D.)