Java 油漆组件油漆两次
据我所知,Swing将决定何时需要重新绘制,这将解释为什么Java 油漆组件油漆两次,java,swing,graphics,paintcomponent,frame-rate,Java,Swing,Graphics,Paintcomponent,Frame Rate,据我所知,Swing将决定何时需要重新绘制,这将解释为什么paintComponent()执行两次。但我已经制作了一个应用程序,可以休眠16毫秒、重新绘制、休眠16毫秒、重新绘制、休眠16毫秒,等等: while(true) { frame.repaint(); try{Thread.sleep(16)}catch(Exception e){} } 它应该以每秒60帧的速度工作。但是,FPS测量程序(如FRAP)显示应用程序以120fps的速度运行。所以基本上,应用程序所做的是
paintComponent()
执行两次。但我已经制作了一个应用程序,可以休眠16毫秒、重新绘制、休眠16毫秒、重新绘制、休眠16毫秒,等等:
while(true)
{
frame.repaint();
try{Thread.sleep(16)}catch(Exception e){}
}
它应该以每秒60帧的速度工作。但是,FPS测量程序(如FRAP)显示应用程序以120fps的速度运行。所以基本上,应用程序所做的是:画框,画框,睡眠,画框,画框,睡眠。。。如何让swing为每个repaint()
调用绘制一帧?(哦,我试着用定时器
代替睡眠()
,结果是一样的)
下面是在Oracle教程中找到的SwingPaintDemo示例。我添加了一个while循环,它将每16毫秒重新绘制一次。我还将undecorated设置为true(这是FRAPS显示每秒实际帧数的唯一方法)
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
/*
***************************************************************
*演示基本绘图的愚蠢示例程序
*摆动部件的机械装置。
***************************************************************
*/
公共类SwingPaintDemo{
公共静态void main(字符串[]args){
JFrame f=新JFrame(“瞄准中心”);
f、 addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
集装箱面板=新的斗牛士面板();
添加(新JLabel(“BullsEye!”,SwingConstants.CENTER),BorderLayout.CENTER);
f、 未装饰的设置(真实);
f、 设置大小(200200);
f、 getContentPane().add(面板,BorderLayout.CENTER);
f、 show();
while(true)
{
f、 重新油漆();
试试{Thread.sleep(16);}catch(异常e){}
}
}
}
/**
*渲染靶心背景的摆动容器
*靶心周围的区域是透明的。
*/
BullsEyePanel类扩展JPanel{
公共斗牛场(){
超级();
setOpaque(false);//我们没有把所有的部分都画出来
setLayout(新的BorderLayout());
setboorder(BorderFactory.createLineBorder(Color.black));
}
公共维度getPreferredSize(){
//了解布局管理器需要什么,并
//然后在最大尺寸上加100
//为了实施“圆形”斗牛
维度layoutSize=super.getPreferredSize();
int max=Math.max(layoutSize.width,layoutSize.height);
返回新尺寸(最大+100,最大+100);
}
受保护组件(图形g){
超级组件(g);
维度大小=getSize();
int x=0;
int y=0;
int i=0;
而(x
1)基本上,paintComponent()中的方法不需要调用repaint()
2) 当需要重新绘制时,Swing GUI会自动调用paintComponent()
3) 对于今天使用图形(2D)
的Swing GUI,默认情况下使用Swing计时器
4) frame.repaint()的代码代码>调用重复的重新绘制
,删除该
5) frame.repaint()如果您添加
/删除
/修改
已经可见,则code>是有用的方法
6) 你是说
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingPaintDemo {
public static void main(String[] args) {
JFrame f = new JFrame("Aim For the Center");
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container panel = new BullsEyePanel();
panel.add(new JLabel("BullsEye!", SwingConstants.CENTER), BorderLayout.CENTER);
f.getContentPane().add(panel, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
/**
* 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));
}
@Override
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);
}
@Override
protected void paintComponent(Graphics 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++;
}
}
}
1) 基本上,paintComponent()
中的方法不需要调用repaint()
2) 当需要重新绘制时,Swing GUI会自动调用paintComponent()
3) 对于今天使用图形(2D)
的Swing GUI,默认情况下使用Swing计时器
4) frame.repaint()的代码代码>调用重复的重新绘制
,删除该
5) frame.repaint()如果您添加
/删除
/修改
已经可见,则code>是有用的方法
6) 你是说
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingPaintDemo {
public static void main(String[] args) {
JFrame f = new JFrame("Aim For the Center");
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container panel = new BullsEyePanel();
panel.add(new JLabel("BullsEye!", SwingConstants.CENTER), BorderLayout.CENTER);
f.getContentPane().add(panel, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
/**
* 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));
}
@Override
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);
}
@Override
protected void paintComponent(Graphics 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++;
}
}
}
为了更快地获得更好的帮助,请提供一个演示所述问题的副本。同一个问题不要问两次!哈哈,我知道这是一样的,但在这个问题上,我使用了你说的计时器将FPS设置为60,但它看起来好像是30,因为它每帧绘制两次。所以问题仍然存在,计时器不是解决方案。现在我想知道如何将paintComponent设置为只绘制一次,而不是两次。然后您应该编辑该问题,并通过注释让感兴趣的各方知道更新。“计时器不是解决方案。”当然不是在调用Thread.sleep(n)
时。不要那样做!但正如我所说,这不是它的问题。前面的问题是。好的,我将编辑另一个问题,稍后删除此问题。为了更快地获得更好的帮助,请包含一个演示所述问题。的精确副本。同一个问题不要问两次!哈哈,我知道这是一样的,但在这个问题上,我使用了你说的计时器将FPS设置为60,但它看起来好像是30,因为它每帧绘制两次。所以问题仍然存在,计时器不是解决方案。现在我想知道如何将paintComponent设置为只绘制一次,而不是两次。然后您应该编辑该问题,并通过注释让感兴趣的各方知道更新。“计时器不是解决方案。”当然不是在调用Thread.sleep(n)
时。不要那样做!但正如我所说,这不是它的问题。前面的问题是。好的,我会编辑另一个问题,稍后删除这个问题。这个程序会绘制一个静止图像,所以实际上不需要重新绘制,但我只是把它作为一个例子。我正在做的应用程序
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
/** @see http://stackoverflow.com/questions/3256941 */
public class AnimationTest extends JPanel implements ActionListener {
private static final int WIDE = 640;
private static final int HIGH = 480;
private static final int RADIUS = 25;
private static final int FRAMES = 24;
private static final long serialVersionUID = 1L;
private final Timer timer = new Timer(20, this);
private final Rectangle rect = new Rectangle();
private BufferedImage background;
private int index;
private long totalTime;
private long averageTime;
private int frameCount;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new AnimationTest().create();
}
});
}
private void create() {
JFrame f = new JFrame("AnimationTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
timer.start();
}
public AnimationTest() {
super(true);
this.setOpaque(false);
this.setPreferredSize(new Dimension(WIDE, HIGH));
this.addMouseListener(new MouseHandler());
this.addComponentListener(new ComponentHandler());
}
@Override
protected void paintComponent(Graphics g) {
long start = System.nanoTime();
super.paintComponent(g);
int w = this.getWidth();
int h = this.getHeight();
g.drawImage(background, 0, 0, this);
double theta = 2 * Math.PI * index++ / 64;
g.setColor(Color.blue);
rect.setRect((int) (Math.sin(theta) * w / 3 + w / 2 - RADIUS),
(int) (Math.cos(theta) * h / 3 + h / 2 - RADIUS), 2 * RADIUS, 2 * RADIUS);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
g.setColor(Color.white);
if (frameCount == FRAMES) {
averageTime = totalTime / FRAMES;
totalTime = 0;
frameCount = 0;
} else {
totalTime += System.nanoTime() - start;
frameCount++;
}
String s = String.format("%1$5.3f", averageTime / 1000000d);
g.drawString(s, 5, 16);
}
@Override
public void actionPerformed(ActionEvent e) {
this.repaint();
}
private class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
JTextField field = new JTextField("test");
Dimension d = field.getPreferredSize();
field.setBounds(e.getX(), e.getY(), d.width, d.height);
add(field);
}
}
private class ComponentHandler extends ComponentAdapter {
private final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
private final GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
private final Random r = new Random();
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
int w = getWidth();
int h = getHeight();
background = gc.createCompatibleImage(w, h, Transparency.OPAQUE);
Graphics2D g = background.createGraphics();
g.clearRect(0, 0, w, h);
g.setColor(Color.green.darker());
for (int i = 0; i < 128; i++) {
g.drawLine(w / 2, h / 2, r.nextInt(w), r.nextInt(h));
}
g.dispose();
System.out.println("Resized to " + w + " x " + h);
}
}
}