2013-12-11 4 views
0

게임 메서드에서 paint 메서드를 어떻게 호출 할 수 있습니까?다른 메서드에서 Graphics2D 메서드를 호출하는 방법은 무엇입니까?

내가 명령을 호출하면 "new paint();" 그것은

// Main 
public Game() { 
    super(); 
    setTitle("Breakout"); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setSize(500, 500); 
    setLocationRelativeTo(null); 
    setIgnoreRepaint(true); 
    setResizable(false); 
    setVisible(true); 
} 

// Grafica 
class paint extends JPanel { 

    public paint(Graphics2D g) { 
     super.paint(g); 
     g.setColor(Color.black); 

    } 

} 
+0

당신은 그렇게 할 수 없습니다. 대신 페인트 할 때마다 전체 게임을 다시 그려야합니다. – SLaks

답변

0

대부분의 프로그래머가 익명으로 게임 값을 변경하고 구성 요소의 시각적 모양을 변경할 다시 그리기()를 호출하는 JPanel의 슈퍼 클래스의 보호 스윙 방법의 paintComponent (그래픽 g)를 사용하여 작동하지 않습니다. SwingTimer를 사용하면 미리 정해진 시간 동안 결과를 업데이트 할 수 있습니다.

public class game extends JPanel implements ActionListener{ 

     int camX, camY; 
     int update_time = 5;// in milliseconds... 
     Graphics2D g2d; 
     Image image; 
     Timer t; 

    public game(){ 

      t = new Timer(update_time, this);// given "this" is an ActionListener... 
      t.start();// calls actionPerformed method every 5ms... 

    } 


    public void actionPerformed(ActionEvent e){ 

      repaint();//calls paintComponent() method... 

    } 


     public void paintComponent(Graphics g){ 

      super.paintComponent(g); 
      g2d = (Graphics2D)g; 

      g2d.drawImage(image, camX, camY, this); 
      //Do graphical stuff 
     } 



} 
관련 문제