2010-06-25 63 views
2

我希望有人碰巧遇到了以下問題。在Mac OS上不支持Java頁面翻轉?

我的Java應用程序在Mac上有圖形性能問題,所以我做了一個簡單的測試應用程序(下面的代碼)。當我在Windows上運行此,控制檯告訴我:

的GraphicsConfiguration翻轉?真
BufferStrategy翻轉?真正

當我運行在Mac OS完全相同的代碼,我得到:

的GraphicsConfiguration翻轉?真
BufferStrategy翻轉?假

這是否意味着在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(); 
} 
} 
+0

這是前一段時間,但我已經成功地使用頁面翻轉(不是全屏)在JOGL在Mac上。 – finnw 2010-06-25 19:36:32

回答

2

壞消息是:我得到同樣的Mac OS X配置了相同的結果。好消息是:isAccelerated()是真的。

System.out.println("BufferStrategy accelerated? " + bufferStrategy 
    .getCapabilities().getFrontBufferCapabilities().isAccelerated()); 

代替CanvasBufferStrategy,我只是用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(); 
    } 
} 
+0

儘管我不知道爲什麼,但是基於您的代碼示例實現我的主應用程序確實解決了Mac上的cpu飢餓問題。當我在渲染內容的頂部添加TextField時,仍然會出現一些工件來解決這個問題,但到目前爲止,這似乎是一個很大的進步!謝謝! – Mattijs 2010-06-28 12:47:56

+0

@Mattijs:我喜歡'javax.swing.Timer'的方便性和可靠性。如果您已將組件添加到'JPanel'中,請在'paintComponent()'中嘗試'super.paintComponent(g)'而不是'fillRect()'。我通常只是在附近添加其他組件。 – trashgod 2010-06-28 13:28:09

+0

更新:通過使用JTextField來解決工件(TextField周圍的一小範圍像素不能正確重新繪製)。 – Mattijs 2010-06-28 13:49:44