【发布时间】:2015-12-31 02:24:44
【问题描述】:
我想将绘制的圆转换为 x,y 坐标并存储在数组或字符串中。我必须使用这个数组来存储我框架中的图像文件。
private final DrawingPanel panel = new DrawingPanel();
private static int[] generateRandomValues(int maxX, int maxY,
int minSize, int maxSize) {
Random random = new Random();
int[] values = new int[3];
values[0] = random.nextInt(maxX);
values[1] = random.nextInt(maxY);
values[2] = Math.min(random.nextInt(maxSize) + minSize, maxSize);
return values;
}
static class Circle {
int x, y, width, height;
public Circle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void draw(Graphics g) {
g.drawOval(x, y, width, height);
}
}
static class DrawingPanel extends JPanel {
List<Circle> circles = new ArrayList<>();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Circle circle : circles) {
circle.draw(g);
}
}
public void addCircle(Circle circle) {
circles.add(circle);
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
在我的主要功能中,我编写了以下代码:
DrawingPanel drawPane = new DrawingPanel();
int[] circleValues = generateRandomValues(300, 300, 50, 150);
int x = circleValues[0];
int y = circleValues[1];
int width = circleValues[2];
int height = width;
Circle circle = new Circle(x, y, width, height);
drawPane.addCircle(circle);
【问题讨论】:
-
如何检测绘制的内容?
-
请看我编辑的问题。
-
不确定您的意思。你不能只 list.add(circleValues) 吗?
-
I want to convert the drawn circle into x,y coordinates- 你是什么意思?您的圆类中有 x/y 坐标。您确实需要学习如何提出我们可以理解的问题。 -
它只会给我圆心的 x 和 y 坐标,而不是整个圆。我希望将圆表示为一组带有 x、y 坐标列表的小线,例如半径为 (1,1,5,5) "1" list =[(x,y 坐标), ( etc.), (etc.), (5,5)] 位于圆圈上。
标签: java arrays drawing store geometry