我希望有人碰巧以前遇到过以下问题。
我的Java应用程序在Mac上有图形性能问题,所以我做了一个简单的测试应用程序(代码如下)。当我在Windows上运行这个程序时,控制台告诉我:"GraphicsConfiguration flipping?
GraphicsConfiguration flipping?true BufferStrategy flipping?
当我在Mac OS上运行完全相同的代码时,我得到的是
GraphicsConfiguration flipping?true BufferStrategy flipping?
这是否意味着在Mac OS上,翻页在窗口式应用中根本不支持?有什么技巧可以让页面翻转在Mac OS上工作而不至于全屏?
欢迎大家提供建议,Mattijs。
在Windows XP和Mac OS 10.5上使用JDK 1.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();
}
}
坏消息:我在相同的Mac OS X配置上得到相同的结果。好消息是:我的Java应用程序在Mac上出现了图形性能问题。isAccelerated()
是真的。
System.out.println("BufferStrategy accelerated? " + bufferStrategy
.getCapabilities().getFrontBufferCapabilities().isAccelerated());
而不是 Canvas
和 BufferStrategy
,我只用 new JPanel(true)
.
增编。例如:
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上,当我设置命令行参数-Dun.java2d.opengl=true时,这种情况就会发生,所以不妨试试做相反的设置-Dun.java2d.opengl=false