【问题标题】:Convert drawOval circle into x,y coordinates将 drawOval 圆转换为 x,y 坐标
【发布时间】: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


【解决方案1】:

稍后我必须以图像格式存储数组

这是什么意思?点数组不是图像。

如果你想知道屏幕上画的是什么,那么你可以:

  1. 为您的面板创建一个 BufferedImage。请参阅 Screen Image 了解执行此操作的简单方法。

  2. 然后您可以一次遍历 BufferedImage 一个像素。如果像素的颜色不等于面板背景的颜色,那么这意味着你在那里画了一些东西。因此,您将该像素的 x/y 值保存在 ArrayList 中。

【讨论】:

    猜你喜欢
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多