Mac OS不支持Java页面翻转?
我希望以前有人碰巧发现了以下问题 我的Java应用程序在Mac上有图形性能问题,所以我制作了一个简单的测试应用程序(代码如下)。在Windows上运行时,控制台会告诉我: 图形配置翻转?正确Mac OS不支持Java页面翻转?,java,macos,double,buffering,Java,Macos,Double,Buffering,我希望以前有人碰巧发现了以下问题 我的Java应用程序在Mac上有图形性能问题,所以我制作了一个简单的测试应用程序(代码如下)。在Windows上运行时,控制台会告诉我: 图形配置翻转?正确 缓冲策略翻转?真的 当我在Mac OS上运行完全相同的代码时,我得到: 图形配置翻转?正确 缓冲策略翻转?假的 这是否意味着在Mac OS上,窗口应用程序根本不支持翻页?有没有什么技巧可以让翻页在Mac OS上工作而不用全屏 非常欢迎所有的指针, 马提斯 在WindowsXP和MacOS10.5上使用JDK
缓冲策略翻转?真的 当我在Mac OS上运行完全相同的代码时,我得到: 图形配置翻转?正确
缓冲策略翻转?假的 这是否意味着在Mac OS上,窗口应用程序根本不支持翻页?有没有什么技巧可以让翻页在Mac OS上工作而不用全屏 非常欢迎所有的指针,
马提斯 在WindowsXP和MacOS10.5上使用JDK1.6 守则:
import java.awt.image.BufferStrategy;
import javax.swing.*;
import java.awt.*;
public class Test {
int width = 640;
int height = 480;
GraphicsEnvironment graphEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphDevice = graphEnv.getDefaultScreenDevice();
GraphicsConfiguration graphicConf = graphDevice.getDefaultConfiguration();
public Test() {
JFrame jFrame = new JFrame(graphicConf);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setTitle("double buffer test");
jFrame.setResizable(false);
jFrame.setFocusTraversalKeysEnabled(false);
Canvas canvas = new Canvas();
canvas.setSize(width, height);
canvas.setIgnoreRepaint(true);
jFrame.getContentPane().add(canvas);
jFrame.pack();
jFrame.setVisible(true);
System.out.println("GraphicsConfiguration flipping? " + graphicConf.getBufferCapabilities().isPageFlipping());
canvas.createBufferStrategy(2);
BufferStrategy bufferStrategy = canvas.getBufferStrategy();
System.out.println("BufferStrategy flipping? " + bufferStrategy.getCapabilities().isPageFlipping());
while(true) {
Graphics g = bufferStrategy.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0,0,width,height);
g.setColor(Color.RED);
g.drawLine((int)(Math.random()*width),(int)(Math.random()*height),
(int)(Math.random()*width),(int)(Math.random()*height));
bufferStrategy.show();
g.dispose();
}
}
public static void main(String[] args) {
new Test();
}
}
坏消息是:我在相同的MacOSX配置上得到了相同的结果。好消息是:
isAccelerated()
是真的
System.out.println("BufferStrategy accelerated? " + bufferStrategy
.getCapabilities().getFrontBufferCapabilities().isAccelerated());
我使用的不是Canvas
和BufferStrategy
,而是
增编:例如
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class NewTest extends JPanel implements ActionListener, Runnable {
private Random r = new Random();
private Timer t = new Timer(10, this);
public static void main(String[] args) {
EventQueue.invokeLater(new NewTest());
}
@Override
public void run() {
JFrame f = new JFrame("NewTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
t.start();
}
public NewTest() {
super(true);
this.setPreferredSize(new Dimension(640, 480));
}
@Override
protected void paintComponent(Graphics g) {
int width = this.getWidth();
int height = this.getHeight();
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
g.setColor(Color.RED);
g.drawLine(r.nextInt(width), r.nextInt(height),
r.nextInt(width), r.nextInt(height));
}
@Override
public void actionPerformed(ActionEvent e) {
this.repaint();
}
}
在Linux上,当我设置命令行参数-Dsun.java2d.opengl=true时,就会发生这种情况
因此,可能值得一试相反的做法——Dsun.java2d.opengl=false这是很久以前的事了,但我已经成功地在Mac上的JOGL中使用了翻页(而不是全屏)。尽管我不知道为什么,基于您的代码示例实现我的主应用程序确实解决了Mac上的cpu饥饿问题。当我在渲染内容的顶部添加文本字段时,仍然有一些工件需要解决,但到目前为止,这似乎是向前迈出的一大步!谢谢@Mattijs:我喜欢
javax.swing.Timer
的方便性和可靠性。如果已将组件添加到JPanel
,请尝试super.paintComponent(g)
而不是paintComponent()
中的fillRect()
。我通常只是在附近添加其他组件。更新:工件(文本字段周围的小范围像素没有正确重新绘制)是通过使用JTextField来解决的。到目前为止,我没有使用计时器,而是使用Thread.sleep()的传统while循环,但我可能会尝试使用计时器方法,看看是否注意到任何差异。顺便说一句,按照您的建议添加super.paintComponent(g)似乎可以清除渲染内容。我保留了一些标志来指示需要重新绘制的位置,这些标志在重新绘制完成后设置为false。不管怎样,没有这个额外的功能,我似乎一切都运行得很好。再次感谢。还有一些问题。。要继续讨论这个话题,请看