Я хочу преобразовать рисованный круг в координаты x, y и сохранить в массиве или строке. Я должен использовать этот массив для хранения файла изображения из моего фрейма.Преобразование круга drawOval в координаты 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);
Как вы определяете, что было нарисовано? – ergonaut
см. Мой отредактированный вопрос. – Mitesh
не уверен, что вы имеете в виду. не можете ли вы просто list.add (circleValues)? – ergonaut