Search code examples
javaswingglasspane

How is it possible to paint more than one rectangle on a glass pane?


I am trying to paint a series of rectangles on the glass pane as described in here. the thing is that only the last element from my list is being displayed on the pane.

Does anyone how to be able to paint more then one rectangle on the same pane?

The following is the code being used:

paint method in the pane's class , extending JComponent

protected void paintComponent(Graphics g) {
        if (point != null) {

            int value = this.getGradient();


            Color myColour = new Color(255, value, 0, 175);
            g.setColor(myColour);
            g.fillRect(point.x - 13, point.y - 15, this.width, this.height);

        }
    }

Solution

  • There's no intrinsic limit on painting on the glass pane, other than the clipping boundary. For example, try the following in MyGlassPane.

    glass pane demo

    protected void paintComponent(Graphics g) {
        if (point != null) {
            g.setColor(Color.red);
            g.drawRect(point.x, point.y, 60, 20);
            g.setColor(Color.blue);
            g.drawRect(point.x, point.y, 20, 60);
        }
    }