2015-01-15 1 views
0

저는 Java Graphics의 완벽한 놈입니다.캔버스에 그림 그리기 할 때 화면 깜박임 유지 (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 및 스윙의 페인팅] (http://www.oracle.com/technetwork/java/painting-140037.html) 및 [사용자 정의 페인팅 수행] (http://docs.oracle.com/javase/tutorial/)을 참조하십시오. 유령/그림 /) – MadProgrammer

답변

0

캔버스는 이중 버퍼링이 아니므로이 방법을 사용하지 않는 것이 좋습니다. 대신 JPanel을 사용하고 paintComponent 메서드를 재정의하면 무료로 이중 버퍼링이 가능합니다.

당신이 때 캔버스 제어셔서, 당신은 당신이 이중 버퍼링 전략을 소유 정의 할 것이다, BufferStrategy를 사용뿐만 아니라 도장 공정을 완벽하게 제어 할 수 있습니다,

또는 일부 deatils에 대한 Painting in AWT and SwingPerforming Custom Painting보기 (AKA 활성 그림)

+0

BufferStrategy는 어떻게 구현합니까? – ThunderCrust

+0

'BuffferStrategy'에 대한 링크를 보시고, 거기에 예제가 있습니다. 또한 [전체 화면 독점 모드 API] (http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html)에서 여러 가지 렌더링 모드를 구분할 수 있습니다 – MadProgrammer