How to resize an AWT Polygon in Java - java

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.)

Related

Using AffineTransform on a Rectangle

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.

Java Affine Transformations

I have a simple question to ask.
Is it possible to do an affine transform outside the paint/paintComponent context?
For instance, let's say i want to create a Shape made of a GeneralPath and then rotate it 45°.
Is it possible to create that object and then rotate it always in the class constructor instead of creating the object and then rotate it in the paint/paintComponent method?
Thank you very much.
UPDATE
Thank you very much for the info guys.
So today i have made a simple test as you suggested and it works.
This is with the Affine transform inside the paintComponent method, commented:
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(new Color(230, 230, 230));
g2.fill(enne.getNuvola());//enne.getNuvola(): code from an omitted class. returns a Shape of a cloud
g2.setColor(new Color(20, 20, 20));
/*
AffineTransform t = AffineTransform.getTranslateInstance(400,400);
g2.transform(t);
*/
g2.fill(rock.getRocket());
}//paintComponent
and this is the affine transform inside the class constructor of a GeneralPath
public class Rocket {
GeneralPath rocket;
public Rocket(){
rocket = new GeneralPath();
rocket.moveTo(10,10);
rocket.lineTo(15,15);
rocket.lineTo(15,50);
rocket.lineTo(5,50);
rocket.lineTo(5,15);
rocket.lineTo(10,10);
rocket.closePath();
AffineTransform t = AffineTransform.getTranslateInstance(400,400);
rocket.transform(t);
}//Rocket Costruttore
public GeneralPath getRocket(){
return this.rocket;
}
}//Rocket
But now i have another question:
Do i have to protect the current state of the current trasform also in the Rocket class like it is suggested to do for the paintComponent method in the java transforming tutorial?
Use the getTransform method to get the current transform.
Use transform, translate, scale, shear, or rotate to concatenate a transform.
Perform the rendering.
Restore the original transform using the setTransform method.
Again, thank you very much for your answers
No, the transform should be reset only to restore the state of the Graphics object, because that Graphics object could be reused by the system for other drawings. If you do the transform without a Graphics object, you don't need to worry about that.
Note for the future that you should not ask new questions by editing old questions, because this is confusing. You should post a completely new question (possibly linking your old question).

Rotating Image (getting it back to original position)

Using "Affine Transformation" I can rotate imageA easily. As well, imageA will move along with imageB. However, I cannot seem to find a way to move imageA back to its original position after I have rotated it.
(I have done some research on some sites and apparently the best method is to move the image back to its original position so that it looks like its rotating from an anchor point.)
Heres my code so far:
public void paintComponent(Graphics g) {
super.paintComponent(g);
AffineTransform af = new AffineTransform();
Graphics2D g2d = (Graphics2D) g;
af.translate(imageBx, imageBy); // moves ImageA to imageb's position
af.rotate(Math.toRadians(angle), imageB.getHeight(this) / 2, imageB.getWidth(this) / 2);
g2d.drawImage(imageA, af, null);
g2d.drawImage(imageB, imageBx, imageBy, null);
}
If anyone can help me move imageA back to its original position (which is right on imageB) that would extremely helpful!
I looked that over, but the code rotates the entire panel; I just want to rotate one Image on a fixed rotate point.
Two things may help guide your understanding:
The example cited uses rotate(double theta); it is preceeded by a translation to the origin and succeeded by a translation the panel's center. Note that the operations are performed in the apparent reverse of the declared order. Your example (may have meant to) invoke rotate(double theta, double anchorx, double anchory). The two effect the same result, the latter being a convenient alternative to the former.
This example contrasts how one can transform the graphics context (g2d) or the image itself. Your example invokes drawImage(Image img, AffineTransform xform, ImageObserver obs), which concatenates xform to the existing graphics transform; this affects all subsequent drawing. It may be easier to keep them separate.

Print label of line with regard to line using Java graphics

I am doing a project in which I need to print the label/description of the line (drawn using graphics) with respect to orientation of the line.
Does anyone know how to do it?
Look to the Graphics2D methods such as rotate(), scale() & translate() - as well as the more general translate(AffineTransform) method.
See Transforming Shapes, Text, and Images in the Java tutorial for more details and working examples, especially of using an AffineTransform (which can concatenate scale, rotate, transform & shear operations).
You do not mention how you obtain the Graphics object. The Graphics object passed to Swing components in paintComponent(Graphics) will generally be a Graphics2D instance, and can be cast to one. To get a Graphics2D instance from a BufferedImage, call createGraphics().
Make a class called "Labelled line" and make it something like this
class LabeledLine {
private int x1, y1, x2, y2;
private String label;
public void drawOn(Graphics g) {
// need more features? thickness, etc? add it
g.drawLine(x1,y1,x2,y2);
// compute size of text, position of text, angle of text
// draw that text
}
}
A quick google for drawing angled text gave me a couple results, so that should be easy to do.

Cannot get image to move where I want it to (and update continuously)?

I'm Basically programming a simple game engine but I'm having problems with my sprites/images not appearing when they should... or at all!
I'll try and keep this as simple as possible. I have a Sprite, GameEngine and Display class. In the gameloop, I have a method that sets the new position of my Sprite (so it just sets the x and y variables). Next I call a transform method which does the following:
public void transform() {
affineTransform.setToIdentity();
affineTransform.translate(x, y);
}
Following that, I then call a draw method in the Sprite:
public void draw() {
graphics2D.drawImage(image, affineTransform, jFrame);
}
Finally, in my thread I then call repaint() on the JFrame (the Display class). My paint method for that class is as follows:
public void paint(Graphics g) {
g.drawImage(backbuffer, insets.left, insets.top, this);
}
But nothing is appearing, apart from a black screen!
I'm also getting confused between Graphics g, and Graphics2D and when to use either. (The overridden paint method uses Graphics g). For the record, I do have a Graphics2D variable in the class that is created by calling backbuffer.createGraphics();
Another thing that is confusing me is this AffineTransform... I've read the documentation but I'm still utterly confused on how and when to use it - and what exactly it seems to do. Is there any explanation in relatively simple terms?
Surely this should be working... am I missing something out here?
To answer part of your question:
From the Graphics2D JavaDoc
This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform.
Essentially, with Graphics2D you can do much more than you can with Graphics. And with a Sun JVM 1.5+, it should be safe to cast the Graphics object you get in paint() to Graphics2D
I just noticed this: For the record, I do have a Graphics2D variable in the class that is created by calling backbuffer.createGraphics();
You should make sure you're not drawing on a Graphics[2D] canvas (I'll use this term to refer to the drawable area that the Graphics[2D] object provides) that you later throw away. If you're drawing your image on a separate canvas, you should ensure that you then draw that image onto your actual display canvas.
I don't really have a good explanation of AffineTransform but maybe these will help?
http://www.javalobby.org/java/forums/t19387.html
https://secure.wikimedia.org/wikipedia/en/wiki/Affine_transformation
From Wikipedia - In general, an affine transformation is composed of linear transformations (rotation, scaling or shear) and a translation (or "shift"). Several linear transformations can be combined into a single one. Basically, you use this class to perform operations such as rotation, translation, zoom etc.

Categories