JPanel 用特定颜色绘图

发布于 2024-12-10 08:09:01 字数 614 浏览 0 评论 0原文

我发现这个类可以用不同的颜色绘制圆圈。每个圆圈的颜色是根据特定的颜色顺序确定的,该颜色顺序在结束时会迭代(已使用一次所有颜色)。我想修改它,使我能够单独确定每个圆圈的颜色(在 g.setColor 上)。换句话说,我希望能够将颜色部署为参数,并从另一个类中的另一个方法调用该方法。

public class DrawingPane extends JPanel {

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Rectangle rect;
        for (int i = 0; i < circles.size(); i++) {
            rect = circles.elementAt(i);
            g.setColor(colors[(i % color_n)]);
            g.fillOval(rect.x, rect.y, rect.width, rect.height);
        }
    }
}

如果您发现我的问题很愚蠢,我想让您知道,令我担心的是该方法是从 JPanel 继承的,并且我不确定如何有效地覆盖它。

I have found this class that draws circles with different colors. The color of each circle is determined according to a specific order of colors which iterates as it comes to the end (having used all colors by one time). I want to modify this on a way that grants me the potential to determine individually the color (on g.setColor) for each circle. In other words, I want to be able to deploy the color as a parameter and to invoke the method from another method in another class.

public class DrawingPane extends JPanel {

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Rectangle rect;
        for (int i = 0; i < circles.size(); i++) {
            rect = circles.elementAt(i);
            g.setColor(colors[(i % color_n)]);
            g.fillOval(rect.x, rect.y, rect.width, rect.height);
        }
    }
}

If you find my question stupid I would like to let you know that what worries me is the fact that the method is inherited from JPanel and I am not sure how to override it effectively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小镇女孩 2024-12-17 08:09:01

您的DrawingPane似乎有一个名为circles(原文如此)的矩形列表。我不知道 Rectangle 是您的类之一还是标准的 java.awt.Rectangle

如果它是您的类之一,则只需向该类添加一个 color 属性,并在迭代期间从中获取该属性即可。

如果是标准的java.awt.Rectangle,则引入一个Circle类,包含一个矩形和一个颜色,并使用一个Circle列表而不是矩形列表。

Your DrawingPane seems to have a list of Rectangle named circles (sic). I don't know if Rectangle is one of your classes or the standard java.awt.Rectangle.

If it's one of your class, then simply add a color attribute to this class, and get this attribute from it during your iteration.

If it's the standard java.awt.Rectangle, then introduce a Circle class, containing a Rectangle and a color, and use a list of Circle rather than a list of Rectangle.

强辩 2024-12-17 08:09:01

我希望能够将颜色部署为参数,并从另一个类中的另一个方法调用该方法

然后您需要将颜色和形状存储为自定义类的属性。

自定义绘画方法显示了如何执行此操作的示例。我将使用 DrawOnComponent 示例作为起点。您的代码将变得更加简单,因为您不需要处理拖动。您需要做的就是创建一个 addCircle(...) 方法,它将圆的大小/位置/颜色作为参数。

I want to be able to deploy the color as a parameter and to invoke the method from another method in another class

Then you need to store the Color and shape as properties of a custom class.

Custom Painting Approaches shows an example of how to do this. I would use the DrawOnComponent example as a starting point. Your code will be much simpler since you don't need to handle dragging. All you need to do is create an addCircle(...) method which will take the size/location/color of the circle as parameters.

萌逼全场 2024-12-17 08:09:01

您在找这个吗?

您可以在单独的 .Java 文件中自由声明 MyCircleDrawingPane 类。

我确信这会回答“我希望能够将颜色部署为参数并从另一个类中的另一个方法调用该方法。”

public class TestingX12 {
    public static void main(String args[]) {
        new TestingX12();
    }

    public TestingX12() {
        //create list of circles
        List<MyCircle> circList = new ArrayList<MyCircle>();
        circList.add(new MyCircle(new Rectangle(100, 20, 120, 30), Color.red));
        circList.add(new MyCircle(new Rectangle(150, 50, 80, 50), Color.yellow));
        circList.add(new MyCircle(new Rectangle(30, 90, 30, 110), Color.blue));

        DrawingPane dp = new DrawingPane(circList);
        JFrame frame = new JFrame("JToolTip Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(dp);
        frame.setSize(400, 450);
        frame.setVisible(true);
    }

    class MyCircle {
        Rectangle rectangle;
        Color color;
        public MyCircle(Rectangle r, Color c) {
            this.rectangle = r;
            this.color = c;
        }
    }
    public class DrawingPane extends JPanel {
        List<MyCircle> circles;
        public DrawingPane(List<MyCircle> circles) {
            this.circles = circles;
        }
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle rect;
            for (int i = 0; i < circles.size(); i++) {
                rect = circles.get(i).rectangle;
                g.setColor(circles.get(i).color);
                g.fillOval(rect.x, rect.y, rect.width, rect.height);
                System.out.println("Drawing...");
            }
        }
    }
}

Are you looking for this ?

You are free to declare the classes MyCircle and DrawingPane in separate .Java files.

I am sure that this will give answer to " I want to be able to deploy the color as a parameter and to invoke the method from another method in another class."

public class TestingX12 {
    public static void main(String args[]) {
        new TestingX12();
    }

    public TestingX12() {
        //create list of circles
        List<MyCircle> circList = new ArrayList<MyCircle>();
        circList.add(new MyCircle(new Rectangle(100, 20, 120, 30), Color.red));
        circList.add(new MyCircle(new Rectangle(150, 50, 80, 50), Color.yellow));
        circList.add(new MyCircle(new Rectangle(30, 90, 30, 110), Color.blue));

        DrawingPane dp = new DrawingPane(circList);
        JFrame frame = new JFrame("JToolTip Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(dp);
        frame.setSize(400, 450);
        frame.setVisible(true);
    }

    class MyCircle {
        Rectangle rectangle;
        Color color;
        public MyCircle(Rectangle r, Color c) {
            this.rectangle = r;
            this.color = c;
        }
    }
    public class DrawingPane extends JPanel {
        List<MyCircle> circles;
        public DrawingPane(List<MyCircle> circles) {
            this.circles = circles;
        }
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle rect;
            for (int i = 0; i < circles.size(); i++) {
                rect = circles.get(i).rectangle;
                g.setColor(circles.get(i).color);
                g.fillOval(rect.x, rect.y, rect.width, rect.height);
                System.out.println("Drawing...");
            }
        }
    }
}
や三分注定 2024-12-17 08:09:01

我不确定你的意思,但如果你试图从外部类设置圆圈颜色,那么使用 setter 和(如果需要)getter 使数组成为该类的属性:

public class DrawingPane extends JPanel {
    private Color[] colors;

    public void setCircles(Color[] colors) {
       this.colors = colors;
       repaint();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Rectangle rect;
        if (colors != null) {
           for (int i = 0; i < colors.size(); i++) {
               rect = circles.elementAt(i);
               g.setColor(colors[(i % color_n)]);
               g.fillOval(rect.x, rect.y, rect.width, rect.height);
           }
        }
    }
}

I'm not sure what you mean, but if you're trying to set the circle colors from an outside class, then make the array a property of the class with a setter and (if needed) a getter:

public class DrawingPane extends JPanel {
    private Color[] colors;

    public void setCircles(Color[] colors) {
       this.colors = colors;
       repaint();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Rectangle rect;
        if (colors != null) {
           for (int i = 0; i < colors.size(); i++) {
               rect = circles.elementAt(i);
               g.setColor(colors[(i % color_n)]);
               g.fillOval(rect.x, rect.y, rect.width, rect.height);
           }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文