2015-01-15 125 views
0

我是Java Graphics的完全noob。在畫布上繪製圖形時屏幕會閃爍(Java)

我寫了一個簡單的「遊戲」中,你控制用箭頭鍵的盒子

這裏是源代碼:

package com.thundercrust.graphics; 

public class Drawings extends Canvas implements KeyListener, Runnable { 

public static Thread thread; 

public static Drawings draw; 

private static final long serialVersionUID = 1L; 

public static boolean running = false; 

public static int x = 640; 
public static int y = 320; 

public static int bulletX = 0; 
public static int bulletY = 0; 

public static int direction = 2; 

public static boolean fired = false; 
public static boolean show = false; 

public static String colorCode; 

public static final int WIDTH = 1366; 
public static final int HEIGHT = WIDTH/16 * 9; 
public static final String title = "A Moving Box!"; 

JFrame frame = new JFrame(); 

public void paint(Graphics g) { 
    Graphics2D g2D = (Graphics2D) g; 
    g2D.setColor(Color.black); 
    g2D.fillRect(0, 0, 1366, 768); 
    g2D.setColor(Color.pink); 
    g2D.fillRect(50, 50, 1266, 668); 
    if (colorCode.equals("red")) g2D.setColor(Color.red); 
    if (colorCode.equals("orange")) g2D.setColor(Color.orange); 
    if (colorCode.equals("yellow")) g2D.setColor(Color.yellow); 
    if (colorCode.equals("green")) g2D.setColor(Color.green); 
    if (colorCode.equals("blue")) g2D.setColor(Color.blue); 
    if (colorCode.equals("cyan")) g2D.setColor(Color.cyan); 
    if (colorCode.equals("gray")) g2D.setColor(Color.gray); 
    g2D.fillRect(x, y, 50, 50); 

} 

public Drawings() { 

    frame.addKeyListener(this); 

    frame.setTitle(title); 
    frame.setSize(WIDTH, HEIGHT); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
    frame.setResizable(false); 
    frame.setVisible(true); 

    frame.add(this); 
} 

public void display() { 
    while (running = true) { 
     repaint(); 
     try { 
      Thread.sleep(30); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

public static void main(String args[]) { 
    colorCode = JOptionPane.showInputDialog("Enter the color of the box: "); 
    running = true; 
    draw = new Drawings(); 
    draw.start(); 
} 

public void keyPressed(KeyEvent e) { 
    int keyCode = e.getKeyCode(); 
    if (keyCode == KeyEvent.VK_UP) { y-= 5; direction = 0; } 
    if (keyCode == KeyEvent.VK_DOWN) { y+= 5; direction = 2; } 
    if (keyCode == KeyEvent.VK_LEFT) {x-= 5; direction = 3;} 
    if (keyCode == KeyEvent.VK_RIGHT) {x+= 5; direction = 1;} 
    if (keyCode == KeyEvent.VK_Z) System.out.println("You pressed z"); 
} 

public void keyReleased(KeyEvent e) { 

} 

public void keyTyped(KeyEvent e) { 


} 

public synchronized void start() { 
    running = true; 
    thread = new Thread(this, "Display"); 
    thread.start(); 
} 

public synchronized void stop() { 
    running = false; 
    try { 
     thread.join(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

public void run() { 
    while (running = true) { 
     System.out.println("The Game is Running!"); 
     repaint(); 
     try { 
      Thread.sleep(60); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

}

爲什麼我要求的原因幫助是因爲應用程序總是閃爍並且變得非常煩人。

有什麼辦法解決這個問題嗎?

任何幫助,將不勝感激。

+1

畫布不是雙緩衝,我建議這樣使用它。相反,考慮使用'JPanel'並覆蓋它的'paintComponent'方法,這會給你免費的雙緩衝。請參閱[在AWT和Swing中繪畫](http://www.oracle.com/technetwork/java/painting-140037.html)和[執行自定義繪畫](http://docs.oracle.com/javase/tutorial/) uiswing/painting /)進行一些修改 – MadProgrammer

回答

0

畫布不是雙緩衝的,我建議不要這樣使用它。相反,考慮使用JPanel並覆蓋它的paintComponent方法,這會給你免費的雙緩衝。

見一些deatils

或者Painting in AWT and SwingPerforming Custom Painting,你可以使用BufferStrategy,這將允許你定義你自己的雙緩衝策略,以及採取完全控制的噴漆工藝,讓您控制當畫布被繪(AKA活動繪畫)

+0

我將如何實現BufferStrategy? – ThunderCrust

+0

查看「BuffferStrategy」的鏈接,從那裏開始,有一個示例。你也可以看看[全屏獨佔模式API](http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html),它討論了不同的渲染模式 – MadProgrammer