0

这是我的代码。

    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"));
    }
}

在此处输入图像描述

您可以看到上面的输出图像。

现在,由于正方形看起来非常小(我尝试改变宽度和高度),我需要以编程方式将其放大。(因为我需要显示机器人经过的路径)。我该怎么做?请帮忙。

请注意,这里的形状比尺寸更重要。

4

1 回答 1

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.

于 2013-01-11T09:25:06.890 回答