Reputation: 310
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"));
}
}
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.
Upvotes: 0
Views: 99
Reputation: 36630
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.
Upvotes: 1