2 hejiero hejiero 于 2013.08.09 22:39 提问

paintComponent(Graphics g)会连续调用两次,想问各位大神什么原理?

在paintComponent()里面加多一个输出语句,可以发现发现protected void

paintComponent(Graphics g)会连续调用两次,想问各位大神什么原理?

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;

public class DrawArcs extends JFrame {
  public DrawArcs() {
    setTitle("DrawArcs");
    add(new ArcsPanel());
  }

  /** Main method */
  public static void main(String[] args) {
    DrawArcs frame = new DrawArcs();
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(250, 300);

  }
}

// The class for drawing arcs on a panel
class ArcsPanel extends JPanel {
  // Draw four blazes of a fan
    int i=0;
 protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    System.out.println(getWidth()+"+"+getHeight()+"+"+i); 
    i++;
    int xCenter = getWidth() / 2;
    int yCenter = getHeight() / 2;
    int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
    int x = xCenter - radius;
    int y = yCenter - radius;
    g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
  }

}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!