Reputation: 145
I have a question when i implement interface.
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
/**
An icon that has the shape of the planet Mars.
*/
public class MarsIcon implements Icon
{
/**
Constructs a Mars icon of a given size.
@param aSize the size of the icon
*/
public MarsIcon(int aSize)
{
size = aSize;
}
public int getIconWidth()
{
return size;
}
public int getIconHeight()
{
return size;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double planet = new Ellipse2D.Double(x, y,
size, size);
g2.setColor(Color.RED);
g2.fill(planet);
}
private int size;
}
import javax.swing.*;
public class IconTester
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(
null,
"Hello, Car!",
"Message",
JOptionPane.INFORMATION_MESSAGE,
new MarsIcon(100));
System.exit(0);
}
}
In the IconTester, i only create a MarsIcon(100). I have not call the method. But it seems that the paintIcon(;;;) is executed. How come? Are the methods called automatically?
Upvotes: 0
Views: 73
Reputation: 262504
You don't call the paintIcon
method directly, this happens by the display manager when your component is part of the visible UI.
And here it is, because you added it to a JOptionPane
.
Upvotes: 2