2014-04-25 6 views
0

나는 이것에 상당히 익숙하다. 그래서 이것이 내가 놓치고있는 매우 쉬운 무엇인가라면 나의 사과!그래픽 문제 다시 그리기

저는 사용자가 지정한 명령을 기반으로 화면에서 움직이는 곤충을 만들기로되어 있습니다. 내가 가지고있는 문제는 JButton을 사용하여 move() 메서드를 사용할 때입니다. 매번 위치를 인쇄하면 move() 메서드가 올바르게 호출된다는 것을 알지만 매번 다시 칠할 수는 없습니다. 그래서, 근본적으로, 나는 나의 이동 버튼을 클릭하고있다. 그리고 나의 벌레는 단지 거기에 앉아있다. 누군가가 올바른 방향으로 나를 가리켜 서 다시 칠할 수 있기를 바랍니다.

이 첫 번째 클래스 (BugTester는) 내 주를 가지고 있으며, 전체 프로그램의 실행 것입니다 : 여기

public class BugTester { 

public static void main(String[] args) { 
    final JFrame frame = new JFrame(); 
    frame.setSize(700, 700); 
    frame.setTitle("Final Project - Grant Cooper"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    final Bug testBug = new Bug(10); 
    final JComponent i = new JComponent() { 

     public void paintComponent(final Graphics g) { 
      testBug.paintComponent(g); 
      JPanel panel = new JPanel(); 
      JButton moveButton = new JButton("Move"); 
      JButton turnButton = new JButton("Turn"); 
      panel.add(turnButton); 
      panel.add(moveButton); 
      frame.add(panel); 
      class ClickListenerMove implements ActionListener { 

       public void actionPerformed(ActionEvent event) { 
        testBug.move(); 

        System.out.println(testBug.getPosition()); 

       } 
      } 
      class ClickListenerTurn implements ActionListener { 

       public void actionPerformed(ActionEvent event) { 
        testBug.turn(); 

       } 
      } 
     } 

    }; 

    frame.add(i); 
    frame.setVisible(true); 

} 

그리고 프로그램의 나머지 부분을 : 패키지 SecondAttempt;

import java.awt.*; 
import java.awt.geom.*; 
public class Bug { 

public int leftRight; 
public int direction; 

//Bug constructor 
public Bug(int initialPosition) { 
    leftRight = initialPosition; 
    direction = 1; 

} 

public void turn() //Turn Bug around - currently only moves in 2 dimensions, need to add up and down 
{ 
    switch (direction) { 
     case 1: 
      direction = 1; 
     case 2: 
      direction = -1; 
    } 

} 

public void move() { 
    getPosition(); 
    getDirection(); 
    if (direction > 0) { 
     leftRight++; //move Bug right 
     System.out.println(getPosition()); 
    } 
    if (direction < 0) { 
     leftRight--; //move Bug left 
     System.out.println(getPosition()); 
    } 
} 

public int getPosition() { 

    return leftRight; 
} 

public int getDirection() { 

    return direction; 
} 

//create Bug 
public void paintComponent(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 

    //create Bug body 
    //(X Coord of body, Y Coord of body, left/right stretch, up/down stretch) 
    Ellipse2D.Double body = new Ellipse2D.Double(300 + getPosition(), 310, 85, 25); 
    g2.draw(body); //draw Bug body 
    g2.setColor(Color.RED); 
    g2.fill(body); //color Bug body 
    g2.setColor(Color.BLACK); //reset color 

    //create Bug head 
    //(X coord head, y coord head, left/right stretch, up/down stretch) 
    Ellipse2D.Double head = new Ellipse2D.Double(380 + getPosition(), 312, 25, 20); 
    g2.draw(head); //draw Bug head 
    g2.setColor(Color.GREEN); 
    g2.fill(head); //color Bug head 
    g2.setColor(Color.BLACK); //reset color 

    //create Bug legs 
    //First set of legs 
    //(Top part of leg x position, top y position, bottom x position, bottom y position) 
    g2.setColor(Color.YELLOW); 
    Line2D.Double leg1_left = new Line2D.Double(365 + getPosition(), 295, 365 + getPosition(), 310); 
    g2.draw(leg1_left); 
    g2.setColor(Color.YELLOW); 
    Line2D.Double leg1_right = new Line2D.Double(365 + getPosition(), 333, 365 + getPosition(), 349); 
    g2.draw(leg1_right); 
    g2.setColor(Color.YELLOW); 
    //Second set of legs 
    Line2D.Double leg2_left = new Line2D.Double(341 + getPosition(), 292, 341 + getPosition(), 310); 
    g2.draw(leg2_left); 
    g2.setColor(Color.YELLOW); 
    Line2D.Double leg2_right = new Line2D.Double(341 + getPosition(), 336, 341 + getPosition(), 354); 
    g2.draw(leg2_right); 
    g2.setColor(Color.YELLOW); 
    //Third set of legs 
    Line2D.Double leg3_left = new Line2D.Double(320 + getPosition(), 295, 320 + getPosition(), 310); 
    g2.draw(leg3_left); 
    g2.setColor(Color.YELLOW); 
    Line2D.Double leg3_right = new Line2D.Double(320 + getPosition(), 333, 320 + getPosition(), 349); 
    g2.draw(leg3_right); 
    g2.setColor(Color.YELLOW); 

    //create Bug antennae 
    //(left x, left y, right x, right y) 
    Line2D.Double antenna1 = new Line2D.Double(403 + getPosition(), 315, 410 + getPosition(), 315); 
    g2.draw(antenna1); 
    g2.setColor(Color.YELLOW); 
    Line2D.Double antenna2 = new Line2D.Double(403 + getPosition(), 329, 410 + getPosition(), 329); 
    g2.draw(antenna2); 
    g2.setColor(Color.YELLOW); 

} 
} 

누구에게 내가 다시 칠할 수 있는지에 대한 통찰력이 있습니까? 나는 프로그램 자체가 아직 완전히 기능적이지는 않지만, 일단이 부분을 이해하면 끝낼 수 있다고 확신한다.

답변

0

코드에 두 가지 문제점이 있다고 생각합니다. 처음에는 Component의 두 내부 클래스가 사용되지 않습니다. 따라서 버튼을 클릭하면 아무 코드와도 연결되지 않으므로 아무 일도 일어나지 않습니다. 나는 당신의 버튼에 ActionListener를 추가 제안 : 이미 repaint()을 추가

   JButton moveButton = new JButton("Move"); 
        moveButton.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
         testBug.move(); 

         repaint(); 
        } 
       }); 

      JButton turnButton = new JButton("Turn"); 
       turnButton.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
         testBug.turn(); 
         repaint(); 
        } 
       }); 

. 그러면 버그가 움직이기 시작합니다.

+0

업데이트는 이벤트 수신기를 기반으로하므로 버그를 타이머를 사용하여 업데이트해야합니다. –

+0

정말 고마워, 이건 정말 도움이 !! 그것은 나를 위해 움직이고 있습니다! – user3574245

1

먼저 swing 인터페이스의 스윙을 확장하지 않으므로 paintComponent 함수를 재정의하지 않으므로 해당 함수의 이름을 지정할 필요가 없습니다. 물론 적절한 Graphics 핸들러를 사용하여 Bug을 그리면 앱의 기본 Swing 인터페이스에서 가져올 수 있습니다.

업데이트 타이머 또는 루프가 있어야하며 그 안에 장소와 위치를 업데이트해야합니다. Bug을 그립니다.