यहां अंडाकार ड्राइंग का सरल उदाहरण है।स्विंग क्यों सरल घटक दो बार आकर्षित करता है?
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);
}
}
}
लेकिन डिबग मोड या कुछ जानकारी (उदाहरण के रूप में) निकालने से पहले सांत्वना देने जोड़ने में, आप देख सकते हैं कि स्विंग दो बार घटकों खींचता है।
paintComponent
पेंट सीमा
पेंट बच्चों
paintComponent
पेंट सीमा
पेंट बच्चों
मुझे समझ में नहीं आता कि ऐसा क्यों होता है, लेकिन मुझे लगता है कि यह एक कठिन जीयूआई में प्रदर्शन को प्रभावित कर सकता है।
http://stackoverflow.com/questions/4814289/why-is-my-code-executing-paintcomponentgraphics-page-twice –
संभव डुप्लिकेट [पेंटकंपोनेंट दो बार निष्पादित हो रहा है] के संभावित डुप्लिकेट (http: // stackoverflow। कॉम/प्रश्न/4800885/पेंटकंपोनेंट-ए-एक्जिकिंग-दो बार) –