1

Suppose I've the following code for an Icon called SquareIcon:

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class SquareIcon implements Icon
{
    private int size;
    private Color color;

    public SquareIcon(int size, Color color) {
        this.size = size;
        this.color = color;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2d = (Graphics2D) g;
        // Rectangle2D.Double implements Shape
        // The Double class defines a rectangle specified in double coordinates.
        Rectangle2D.Double figure = new Rectangle2D.Double(x, y, size, size);
        g2d.setColor(color);
        // Fills the interior of a Shape using the settings of the Graphics2D context
        g2d.fill(figure);
    }

    @Override
    public int getIconWidth() {
        return size;
    }

    @Override
    public int getIconHeight() {
        return size;
    }
}

toghether with a corresponding test class called SquareIconTest:

public class SquareIconTest {
    public static void main(String[] args) {
        SquareIcon icon = new SquareIcon(50, Color.BLUE);
        JOptionPane.showMessageDialog(
                null,
                "Blue SquareIcon",
                "SquareIcon Test",
                JOptionPane.INFORMATION_MESSAGE,
                icon
        );
    }
}

How does the Rectangle2D.Double knows it should be displayed inside the JOptionPane ? I see that the x and y variables must be the position inside this pane, but where do the rectangle gets the other information?

I've read the JavaDoc, and it says that g2d.fill() fills the interior of a Shape, i.e. sets its color? This doesn't provide information about the pane?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • 1
    because you pass `icon` as a paramater to the `showMessageDialog` method? – SomeJavaGuy Nov 11 '15 at 08:12
  • the Rectangle2d.Double in your icon class does not know about where it will be displayed, the JOptionPane knows that it should be using your icon class (so this way round) – Emerson Cod Nov 11 '15 at 08:13
  • 2
    The `Graphics` context has been translated to within the context of the component been painted. – MadProgrammer Nov 11 '15 at 08:13
  • @EmersonCod, yes but the ´paintIcon()´ method doesn't return this rectangle? – Shuzheng Nov 11 '15 at 08:14
  • @NicolasLykkeIversen no - the paint method has `void` so it returns nothing. It gets the Graphics object to paint into. This is (shortly spoken) taken from the JOptionPane and therefore the icon appears in there – Emerson Cod Nov 11 '15 at 08:15
  • So, it is the Graphics object that tells the rectangle, that it should be displayed inside the JOptionPane? It gets this information using the fill() method? – Shuzheng Nov 11 '15 at 08:18

1 Answers1

2

The Graphics object tells the rectangle that it should be displayed inside the JOptionPane?

No. As noted in Painting in AWT and Swing, the system tells the icon to paint itself. When the JOptionPane is made visible, its content must be painted. Eventually, paintComponent() is called on the option pane label that holds the Icon. The geometry passed into paintIcon() is defined by the layout and insets of the enclosing container. In this related example, note how setHorizontalAlignment() defines the relationship between the icon and text in the label. One easy way to see the chain of events is to set a break-point on the call to fill() and examine the call chain in a debugger. Your example adds an Icon directly the JOptionPane. As an experiment, try creating a Label with text/icon and adding it to the JOptionPane.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045