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);
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 am currently trying to draw and fill a Polygon which has a hole in it in Java. Normally this would not be a big problem, since I would draw the exterior ring and then draw the interior ring with the color of the background.
But the problem is, that the polygon is displayed above a image which should be "seen" through the hole.
I am writing the code in Java and am using JTS Topology Suite for my geometry data.
This is my current code, which just paints the border and fills the polygon with a color.
private void drawPolygon(com.vividsolutions.jts.geom.Polygon gpoly, Color color, Graphics2D g2d){
java.awt.Polygon poly = (java.awt.Polygon)gpoly;
for(Coordinate co : gpoly.getExteriorRing().getCoordinates() {
poly.addPoint(co.x, co.y);
}
g2d.setColor(col);
g2d.fill(poly);
g2d.setColor(Color.BLACK);
g2d.draw(poly);
}
Sadly java.awt.Polygon does not support Polygons with holes.
Use the Polygon as the basis for an Area (e.g. called polygonShape).
Create an Ellipse2D for the 'hole', then establish an Area for it (ellipseShape).
Use Area.subtract(Area) something like:
Area polygonWithHole = polygonShape.subtract(ellipseShape);
There are some ways to draw shapes or areas that are more complex than a simple polygon (another answer already mentioned Area).
Besides those, you could try to tessellate your final polygon. There are lots of algorithms to do this. For more complex shapes, the algorithms get a bit more complex as well. Basically, you're dividing your final shape into little polygons (usually triangles, but it also can be something else) and then draw those polygons.
You can take a look at your possibilities by searching for "Tessellation Algorithm", there are also some already implemented libraries for Java.
You can use java.awt.geom.Path2D to render a "compound shape" with a hole in it:
If you have java.awt.Shape objects defining the outside & inside edges of the shape, use append(shape, false) to add each shape.
If you have a set of path points for the outside edge and a set of path points for the inside edge, use lineTo() to add the first set of points - creating a closed loop by either ending with the same point you started with, or calling closePath() to automatically close the loop. Then use moveTo() to create a break before adding the inner set of points via more lineTo() calls.
In either case, you must create the path passing Path.WIND_NON_ZERO to the constructor - otherwise the hole won't be left unfilled.
See How to create shape with a hole? for a longer code example.
You could fill the polygon first, and then draw the holes on top, giving the illusion that it filled everything but the holes.
I have an assignment to draw a certain number of circles using java.awt.Graphics.
Drawing the circles is fairly simple, but I am only supposed to draw the circle if it appears within the visible area. I know I can call method getClipBounds() to determine the drawing area, but I'm having trouble finding a java implementation of a way to determine if a circle intersects a Rectangle.
Is that the right way to go about determining if the circle I want to draw will be completely visible or is there a simpler way?
Don't use the Graphics.fillOval(...) method to do the painting.
Instead you can use the Graphics2D.fill(Shape) method. You can create oval Shape objects using the Ellipse2D class.
but I'm having trouble finding a java implementation of a way to determine if a circle intersects a Rectangle.
The Shape object has a method that will allow you to get the rectangular bounds of the Shape. Then you can use the Rectangle.contains(...) method of the your Graphics area to determine if the Shape is fully contained within your panel.
Check out Playing With Shapes for more information and ideas.
use Ellipse2D.Float to instanciate an object for example:
Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
basically the parameters,from left to right, are: Height, Width, X of the Top left and Y of the top left, and by keeping the X and Y both greater or equal to zero, your circle will always be visible.
the parameters of the Float(...) are documented for the Ellipse2D.Float in Java SE 7 in
http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Ellipse2D.Float.html
Hello I am making a game in java. I am using a collection of lines to represent a shape to detect collisions. I need to be able to rotate this shape by degrees or radians
As you can see from the diagram above the shape is a collection of line segments with 2 points a and b. I need to know how would I rotate all of lines together and still retain the shape.
Sounds like a job for AffineTransform (assuming you're doing 2D)
Something to this effect:
Point2D rotatedPoints = new Point2D[yourPoints.length];
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(yourDegreeRotation), xToRotateAround, yToRotateAround);
at.transform(yourPoints, 0, rotatedPoints, 0, yourPoints.length);
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