2013-06-24 5 views
0

현재 2D 공간에서 움직이고 상호 작용하는 다양한 객체가있는 프로그램에 애니메이션을 추가하려고합니다. 나는 타이머가 작동하는 곳으로 마침내 갔고, 프로그램은 객체가 움직이고 원하는 방식으로 상호 작용할 수 있습니다 ...하지만 열리는 디스플레이 패널은 좌절하게 남아 있습니다. 나는 온라인으로 볼 예제에서 코드를 사용하려고했습니다,하지만 난 여전히 뭔가가 있어야합니다 ... 궁극적으로자바 스윙이 표시되지 않습니다.

public class Populus2 extends JPanel 
{ 
    /** 
    * @param args 
    */ 

     static float[] xCoordinates; 
     static float[] yCoordinates; 
     static int duration; 
     static int iteration = 1; 
     static int graphSize = 500; 
     .............. 

     static JFrame frame; 
     static JLabel lbl; 
     JPanel panel; 
     static Timer timer; 

     public static void main(String[] args) throws IOException { 
      final Populus2 pop = new Populus2(); 

      frame = new JFrame("Animation Frame"); 
      lbl = new JLabel(); 
      Panel panel = new Panel(); 
      panel.add(lbl); 
      frame.add(panel, BorderLayout.CENTER); 
      frame.setSize(graphSize, graphSize); 
      frame.setVisible(true); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      ActionListener timeStep = new ActionListener() { 
       public void actionPerformed(ActionEvent evt) { 
        System.out.println("at time " + iteration); 
        spendTime(); 
        pop.repaint(); 
        if(iteration>duration) 
         timer.stop(); 
       } 
      }; 

     timer = new Timer(100, timeStep); 
     timer.setInitialDelay(0); 
     timer.start();  
    } 

    @Override 
    public void paint(Graphics g) 
    { 
     super.paint(g); 
     System.out.println("******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n"); 
     Graphics2D g2d = (Graphics2D) g; 
     paintCell(g, 1); //eventually, I want to call this method for every "cell" in the program 
     g.drawLine(30, 30, 80, 80); //this line's basically a test to see if I can display anything at all 
     Toolkit.getDefaultToolkit().sync(); 
     g.dispose(); 
    } 


     public void actionPerformed(ActionEvent e) { 
     repaint(); 
    } 

    public void paintCell(Graphics graphics, int cellNumber) 
    { 
     graphics.setColor(Color.black); 
     graphics.fillOval((int)(graphSize*xCoordinates[cellNumber]/size), (int)(graphSize*yCoordinates[cellNumber]/size), 5, 5); 
    } 

    @Override 
    protected void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     for(int i = 0; i < types.length; i++) 
      paintCell(g, i); 
    } 


    public static void spendTime() 
    { 
     //advances the iteration counter and recalculates the coordinates of all the cells 
    } 

} 

, 나는 프로그램이 타이머의 각 반복에 대한 paintCell()를 호출 할 시뮬레이션에서 각 셀은 움직이는 물체를 나타냅니다. 지금은 화면이 비어 있습니다. System.out.println()을 통해 paint()를 호출 한 메시지는 레코드에 대해 표시되지 않습니다. 생각 하시겠습니까?

+0

타이머 내에서 repaint()를 호출합니까? – jonhopkins

+0

음, 나는 timeStep ActonListener 내에서 pop.repaint()를 호출합니다. – user1209014

답변

2

것은 나는 온라인으로 볼 예제 코드를 사용하기 위해 노력했습니다,하지만, 난 여전히 뭔가 ...

는 스윙 프로그램에 AWT 제안을 사용하지 마십시오 누락해야합니다. 스윙은 AWT와는 다릅니다. 패널을 사용하지 마십시오. Swing에서는 JPanel을 사용합니다. Swing에서 paint() 메소드가 아닌 paintComponent() 메소드를 덮어 씁니다. 지금 당신의 코드는 두 방법 모두를 오버라이드하려고합니다.

자세한 내용과 적절한 예는 Custom Painting의 스윙 튜토리얼을 참조하십시오.

frame.setSize(graphSize, graphSize); 

는 프레임 크기 때문에 그래프가 원하는 크기되지 않습니다 경계 및 제목 표시 줄을 포함() frame.setSize를 사용하지 마십시오. 대신 사용자 정의 페인팅을 수행하는 JPanel의 getPreferredSize()을 재정 의하여 패널의 크기를 반환하십시오. 그렇다면 frame.pack()을 할 것입니다.

하지만 패널이 표시되지 않는 이유는 패널을 프레임에 추가하지 않았기 때문입니다.

//frame.add(panel, BorderLayout.CENTER); 
frame.add(pop, BorderLayout.CENTER); 

레이블에 값이 없으므로 아래 코드는 아무런 성과가 없습니다. JPanel을 사용해야하지만 프레임의 내용 창에 직접 레이블을 추가 할 수 있으므로 패널이 필요하지 않습니다.

lbl = new JLabel(); 
Panel panel = new Panel(); 
panel.add(lbl); 
+0

그 트릭을 할 것으로 보인다. 조언 해주셔서 감사합니다! – user1209014

관련 문제