Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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...");
            }
        }
    }
}

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);
           }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top