0

Here is my code.

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.geom.Line2D;
    import java.awt.image.BufferedImage;
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import javax.imageio.ImageIO;

public class Test {

    public static void main(String args[]) throws IOException{

        int width = 400, height = 400;
        Test plot = new Test();

        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics2D g2d = bi.createGraphics();
        g2d.setPaint(Color.red);


        g2d.draw(new Line2D.Double(0,0,0,50));
        g2d.draw(new Line2D.Double(0,50,50,50));
        g2d.draw(new Line2D.Double(50,50,50,0));
        g2d.draw(new Line2D.Double(50,0,0,0));


        ImageIO.write(bi, "PNG", new File("d:\\sample.PNG"));
    }
}

enter image description here

You can see the output image above.

Now, Since the square looks very small(I tried varying the width and height), I need to scale this up programmatically. (As I need to show the path traveled by the robot). How can I do it? Please help.

Please note that shape is more important here not the dimension.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
shriguru nayak
  • 310
  • 1
  • 3
  • 21
  • 2
    I can't undestand: your square is actually 50x50 size according to your program. – Andremoniy Jan 11 '13 at 09:25
  • 2
    Size of image is 400x400, and square is 50x50 all works as expected. What result you want to achieve? – mishadoff Jan 11 '13 at 09:26
  • Ahh. You're right. Sorry. My questions is not clear. How can I automatically change the size of the image. Can I change the dimension of the window to look equally big even if the co-ordinates are small by changing the dimension. – shriguru nayak Jan 11 '13 at 09:32
  • 1
    You probably need an [`AffineTransform`](http://stackoverflow.com/questions/13440201/how-to-resize-text-in-java/13440543#13440543) or (more simply) to [set a scale](http://stackoverflow.com/questions/7198903/how-to-resize-image-iconimage-in-jlabel/7199269#7199269) on the `Graphics2D` instance. I say 'probably' since I still do not understand your question. – Andrew Thompson Jan 11 '13 at 09:35

1 Answers1

1

I dont know if I get the question right, but

    g2d.draw(new Line2D.Double(0,0,0,50));
    g2d.draw(new Line2D.Double(0,50,50,50));
    g2d.draw(new Line2D.Double(50,50,50,0));
    g2d.draw(new Line2D.Double(50,0,0,0));

gives a 50x50 pixels rectangle since you have not defined any transformations. Try something like

    g2d.draw(new Line2D.Double(0,0,0,150));
    g2d.draw(new Line2D.Double(0,150,150,150));
    g2d.draw(new Line2D.Double(150,150,150,0));
    g2d.draw(new Line2D.Double(150,0,0,0));

which renders a larger rectangle.

Alternatively, you can also define a scaling transformation like

    g2d.scale(3.0, 3.0);

Note that this also scales the line width, so that the result is not completely the same as using different coordinates in the Line2D.Double() calls.

See also http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html for more information on coordinate systems and the Graphics2D rendering process.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Ahh. You're right. Sorry. My questions is not clear. How can I automatically change the size of the image. Can I change the dimension of the window to look equally big even if the co-ordinates are small by changing the dimension. – shriguru nayak Jan 11 '13 at 09:31
  • 1
    Do you want to adjust the size of the image to your rectangle, or do you want to change the size of the rectangle to the size of your image? The question is still not completely clear to me ... – Andreas Fester Jan 11 '13 at 09:38