2014-10-23 2 views
3

그리드를 그릴 필요가있는 게임을 개발하려고합니다. 그 때문에 repaint() 메서드에 의해 호출되고있는 paintComponent(Graphics g) 메서드를 사용하고 있습니다.paintComponent() 최소화 최소화 화면을 때 호출됩니다

문제는 repaint 메서드가 무한 While 루프 안에 있고 화면을 최소화하고 최대화하지 않는 한 paintComponent() 메서드를 호출하지 않는다는 것입니다. 그 후 잘 작동하고 while 루프에서 paintComponent()을 완벽하게 호출합니다.

간단히 말해, 최소화 (Minimizing) - 화면 최대화로 트리거해야합니다.

아무도 도와 줄 수 있습니까?

코드에서 3 개의 클래스, 즉 Frame.java, MenuHandler.java & Screen.java를 볼 수 있습니다. 코드는 Frame 클래스의 main 메소드에서 시작하여 Screen이 JPanel을 확장 할 때 Screen 클래스를 자체에 추가합니다. 그러나 사용자가 메뉴에서 "지도 만들기"를 선택하는 경우에만 컨트롤이 Screen 클래스로 이동합니다. 그런 다음 MenuHandler 클래스는 createMap 메서드에서 run 메서드가 호출되고이 메서드에서 repaint 메서드를 호출하는 Screen 클래스에 컨트롤을 전달합니다. 패키지 so; 실행 방법에

public class Screen extends JPanel implements Runnable { 
    Frame frame; 

    public Screen(Frame frame) { 
     this.frame = frame; 
    } 

    public void createMap() { 

     thread.start(); 
    } 

    public void paintComponent(Graphics g) { 
     g.setColor(Color.BLUE); 

    } 

    @Override 
    public void run() { 

     System.out.println("Success******"); 

     long lastFrame = System.currentTimeMillis(); 

     int frames = 0; 
     running = true; 
     scene = 0; 

     // the map grid would be refreshed every 2 ms so that we don't get the 
     // flickering effect 
     while (running) { 

      frames++; 

      if (System.currentTimeMillis() - 1000 >= lastFrame) { 
       fps = frames; 
       frames = 0; 
       lastFrame = System.currentTimeMillis(); 
      } 

      // to draw stuff all the time on the screen : goes around 2 millions 
      // frames per second. Which is of no use. 
      repaint(); 

      try { 
       Thread.sleep(2); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     System.exit(0); 
    } 
} 

타이머 코드 :

public void run() { 

    System.out.println("Success"); 

    long lastFrame = System.currentTimeMillis(); 

    int frames = 0; 
    running = true; 
    scene = 0; 

    // the map grid would be refreshed every 2 ms so that we don't get the 
    // flickering effect 
    while (running) { 

     frames++; 

     if (System.currentTimeMillis() - 1000 >= lastFrame) { 
      fps = frames; 
      frames = 0; 
      lastFrame = System.currentTimeMillis(); 
     } 
     System.out.println("before repaint"); 
     // to draw stuff all the time on the screen : goes around 2 millions 
     // frames per second. Which is of no use. 
     ActionListener taskPerformer = new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       repaint(); 
      } 
     }; 

     new Timer(200, taskPerformer).start(); 
     System.out.println("after repaint"); 

     try { 
      Thread.sleep(2); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
    System.exit(0); 
} 
+7

* "repaint() 메소드는 무한 While 루프 안에 있습니다."*'repaint()'는 Swing 'Timer'에 의해 트리거되어야합니다. 이 코드가 EDT를 차단하고있는 것 같습니다. 그러나 더 빨리 도움을 받으려면 [MCVE] (http://stackoverflow.com/help/mcve) (최소한의 완전하고 검증 가능한 예)를 게시하십시오. –

+0

코드와 설명으로 질문을 업데이트했습니다. – Manpreet

+0

다른 스레드를 사용하므로 EDT를 차단하지 않기 때문에 OK입니다. 그런 다음 EDT를 GUI에서 업데이트하려고 시도합니다. EDT는 또 다른 아니오입니다. Timer를 사용하면 EDT에서 업데이트가 수행됩니다. –

답변

2

나는이 문제를 알아 냈어. paintComponent 메서드를 최소화하기 위해 추가하는 대신 하나의 선을 추가하여 창을 최대화했습니다.

프레임이 최상위 컨테이너였으며 프레임에 Screen 구성 요소 (JPanel을 확장하고 paintComponent 구현이 있음)를 추가하고있었습니다. 나를 위해 그것을 한 추가 한 후 validate 메소드를 호출

frame.getContentPane().add(screen); 
frame.getContentPane().validate(); 

: 추가하는 동안 그래서, 이전에 내가

frame.add(screen); 

을하고 있던 나는이 변경되었습니다. 그것이 의미가 있는지는 모르겠지만 그래, 그것이 나를 위해 일한 유일한 라인이었다.

희망이 있습니다.