据我了解,Swing 将决定何时需要重新绘制,这可以解释为什么paintComponent()
要执行两次。但我已经制作了一个睡眠 16 毫秒、重绘、睡眠 16 毫秒、重绘、睡眠 16 毫秒等的应用程序:
while(true)
{
frame.repaint();
try{Thread.sleep(16)}catch(Exception e){}
}
它应该以 60fps 的速度工作。但是,FPS 测量程序(如 FRAPS)显示应用程序以 120fps 运行。repaint()
所以基本上,应用程序正在做的是:并条框,并条框,睡眠,并条框,并条框,睡眠......我如何告诉swing为每次调用绘制一帧?(哦,我试过用 aTimer
而不是sleep()
,结果是一样的)。
例如,在 Oracle 教程中找到的 SwingPaintDemo。我添加了一个 while 循环,它将每 16 毫秒重新绘制一次。我还将 undecorated 设置为 true (这是 FRAPS 向我显示每秒实际帧数的唯一方法)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
***************************************************************
* Silly Sample program which demonstrates the basic paint
* mechanism for Swing components.
***************************************************************
*/
public class SwingPaintDemo {
public static void main(String[] args) {
JFrame f = new JFrame("Aim For the Center");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container panel = new BullsEyePanel();
panel.add(new JLabel("BullsEye!", SwingConstants.CENTER), BorderLayout.CENTER);
f.setUndecorated(true);
f.setSize(200, 200);
f.getContentPane().add(panel, BorderLayout.CENTER);
f.show();
while(true)
{
f.repaint();
try{Thread.sleep(16);}catch(Exception e){}
}
}
}
/**
* A Swing container that renders a bullseye background
* where the area around the bullseye is transparent.
*/
class BullsEyePanel extends JPanel {
public BullsEyePanel() {
super();
setOpaque(false); // we don't paint all our bits
setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.black));
}
public Dimension getPreferredSize() {
// Figure out what the layout manager needs and
// then add 100 to the largest of the dimensions
// in order to enforce a 'round' bullseye
Dimension layoutSize = super.getPreferredSize();
int max = Math.max(layoutSize.width,layoutSize.height);
return new Dimension(max+100,max+100);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = getSize();
int x = 0;
int y = 0;
int i = 0;
while(x < size.width && y < size.height) {
g.setColor(i%2==0? Color.red : Color.white);
g.fillOval(x,y,size.width-(2*x),size.height-(2*y));
x+=10; y+=10; i++;
}
}
}