これは、楕円を描く簡単な例です。
public class SwingPainter extends JFrame{
public SwingPainter() {
super("Swing Painter");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(new MySwingComponent());
setSize(200, 200);
setVisible(true);
}
public static void main(String[] args) {
new SwingPainter();
}
class MySwingComponent extends JComponent {
public void paintComponent(Graphics g) {
System.out.println("paintComponent");
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(10, 10, 50, 50);
}
@Override
protected void paintBorder(Graphics g) {
System.out.println("Paint border");
super.paintBorder(g);
}
@Override
protected void paintChildren(Graphics g) {
System.out.println("Paint children");
super.paintChildren(g);
}
}
}
しかし、(例のように) 描画する前にデバッグ モードまたはコンソールに情報を追加すると、swing がコンポーネントを 2 回描画することがわかります。
paintComponent
境界線をペイント
子供たちを描く
paintComponent
境界線をペイント
子供たちを描く
なぜそうなるのかはわかりませんが、難しい GUI でのパフォーマンスに影響を与える可能性があると思います。