简体   繁体   English

使用二维数组的矩形对象

[英]Rectangle object using two-dimensional array

If i have a two-dimensional array, is it possible to print out the result as a rectangle? 如果我有一个二维数组,是否可以将结果打印为矩形?

Here is what i have come up with; 这是我想出的。

int[][] anArrayRectangle = {{0,0},{-2,0},{1,-2},
                            {0,1},{2,1},{2,0}};

Each point represents one out of six points which together create a rectangle in the unit circle. 每个点代表六个点中的一个,它们一起在单位圆中创建一个矩形。 The question is if it is possible to display the rectangle in lines, not exclusively using the print method. 问题是,是否有可能以行显示矩形,而不是仅使用打印方法。 Perhaps in an applet? 也许在小程序中?

All tips are welcome. 欢迎所有提示。

Maybe you could use drawPolygon 也许您可以使用drawPolygon


import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Rectangle extends JPanel {

private int xPos[] = {100, 150, 200, 200, 150, 100};
private int yPos[] = {100, 100, 100, 150, 150, 150};

public void paint(Graphics g) {

    super.paint(g);

    int length = xPos.length;

    g.drawPolygon(xPos, yPos, length);

}

private static JFrame frame = null;

public static void main(String[] args) {

    frame = new JFrame("Graphics");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setVisible(true);

    Rectangle obj = new Rectangle();

    frame.add(obj);

}

} }

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM