2014-03-13 6 views
-1

거북이 및 토끼 프로젝트 작업. 이 방법은 거북이와들 토끼를 그려야하지만 오류 메시지가 나타납니다. 여기거북이와 토끼, 그리지 않는 방법

protected void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 

    // draw the finish line 
    finishX = getWidth() - 20; 
    g.setColor(Color.blue); 
    g.drawLine(finishX, 0, finishX, getHeight()); 

    if (raceIsOn) 
    { 
     /* loop through instance variable ArrayList racerList, 
     * which contains Racer object references, 
     * calling draw and move for each element. 
    */ 
     for (int i = 0; i < racerList.size(); i++) 
    { 
     move(); 
     draw (Graphics g); 
    } 

    } 
    else // display racers before race begins 
    { 

    /* loop through instance variable ArrayList racerList, 
    * which contains Racer object references, 
    * calling draw for each element. 
    */ 
     for (int i = 0; i < racerList.size(); i++) 
     { 
     draw(); 
     } 

    } 
} 

클래스의 일부가 여기

/** abstract method for Racer's move 
    */ 
    public abstract void move(); 
    /** abstract method for drawing Racer 
    * @param g Graphics context 
    */ 
    public abstract void draw(Graphics g); 

를 호출하는 전체 클래스입니다 노력하고있다

import java.awt.*; 
import javax.swing.*; 
import java.util.ArrayList; 

public class RacePoly extends JFrame 
{ 
    private ArrayList<Racer> racerList; // racers stored in ArrayList 
    private static RacePoly app; 
    private final int FIRST_RACER = 50; 
    private int finishX; // location of finish line, dependent on window width 
    private boolean raceIsOn = false; 
    private RacePanel racePanel; 

    /** Constructor 
    * instantiates list to track racers 
    * sets up GUI components 
    */ 
    public RacePoly() 
    { 
    super("The Tortoise & The Hare!"); 
    Container c = getContentPane(); 
    racePanel = new RacePanel(); 
    c.add(racePanel, BorderLayout.CENTER); 

    racerList = new ArrayList<Racer>(); 
    setSize(400, 400); 
    setVisible(true); 
    } 

    /** prepareToRace method 
    * uses a dialog box to prompt user for racer types 
    *  and to start the race 
    * racer types are 't' or 'T' for Tortoise, 
    *     'h' or 'H' for Hare 
    * 's' or 'S' will start the race 
    */ 
    private void prepareToRace() 
    { 
    int yPos = FIRST_RACER;  // y position of first racer 
    final int START_LINE = 40;  // x position of start of race 
    final int RACER_SPACE = 50; // spacing between racers 
    char input; 

    input = getRacer(); // get input from user 

    while (input != 's' && input != 'S') 
    { 
     switch (input) 
     { 
     case 'T': 
     case 't': 
      racerList.add(new Tortoise("Tortoise", START_LINE, yPos)); 
      yPos+=RACER_SPACE; 
     break; 
     case 'H': 
     case 'h': 
      racerList.add(new Hare("Hare", START_LINE, yPos)); 
      yPos+=RACER_SPACE; 
     break; 
     default: 
      JOptionPane.showMessageDialog(this, "That was not a T or H you bitch."); 
     break; 
     } 

     * If input is 'H' or 'h', 
     *  add a Hare object to the ArrayList named racerList 
     * The API of the Hare constructor is: 
     *   Hare(String ID, int startX, int startY) 
     *  a sample call to the constructor is 
     *   new Hare("Hare", START_LINE, yPos) 
     *   where START_LINE is a constant local variable 
     *   representing the starting x position for the race 
     *   and yPos is a local variable representing 
     *    the next racer's y position 
     * 
     * After adding a racer to the ArrayList racerList, 
     *   increment yPos by the value of 
     *   the constant local variable RACER_SPACE 
     * 
     * if input is anything other than 'T', 't', 
     *   'H' or 'h', pop up an error dialog box 
     *   a sample method call for the output dialog box is: 
     *   JOptionPane.showMessageDialog(this, "Message"); 
     */ 

     repaint(); 
     input = getRacer(); // get input from user 

    } // end while 
    } 
    private class RacePanel extends JPanel 
    { 
    /** paint method 
    * @param g Graphics context 
    * draws the finish line; 
    * moves and draws racers 
    */ 
    protected void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 

     // draw the finish line 
     finishX = getWidth() - 20; 
     g.setColor(Color.blue); 
     g.drawLine(finishX, 0, finishX, getHeight()); 

     if (raceIsOn) 
     { 
      /* loop through instance variable ArrayList racerList, 
      * which contains Racer object references, 
      * calling draw and move for each element. 
     */ 
      for (int i = 0; i < racerList.size(); i++) 
     { 
      move(); 
      draw (g); 
     } 

     } 
     else // display racers before race begins 
     { 

     /* loop through instance variable ArrayList racerList, 
     * which contains Racer object references, 
     * calling draw for each element. 
     */ 
      for (int i = 0; i < racerList.size(); i++) 
      { 
      draw(); 
      } 

     } 
    } 
    } 

    /** runRace method 
    * checks whether any racers have been added to racerList 
    * if no racers, exits with message 
    * otherwise, runs race, calls repaint to move & draw racers 
    * calls reportRaceResults to identify winners(s) 
    * calls reset to set up for next race 
    */ 
    public void runRace() 
    { 
     if (racerList.size() == 0) 
     { 
      JOptionPane.showMessageDialog(this, 
        "The race has no racers. exiting", 
        "No Racers", JOptionPane.ERROR_MESSAGE); 
      System.exit(0); 
     } 
     raceIsOn = true; 
     while (! findWinner()) 
     { 
      Pause.wait(.03); 
      repaint(); 
     } // end while 

     reportRaceResults(); 
     reset(); 
    } 

    /** gets racer selection from user 
    * @return first character of user entry 
    *   if user presses cancel, exits the program 
    */ 
    private char getRacer() 
    { 
     String input = JOptionPane.showInputDialog(this, "Enter a racer:" 
              + "\nt for Tortoise, h for hare," 
              + "\nor s to start the race"); 
     if (input == null) 
     { 
     System.out.println("Exiting"); 
     System.exit(0); 
     } 
     if (input.length() == 0) 
     return 'n'; 
     else 
     return input.charAt(0); 
    } 

    /** findWinners: 
    * checks for any racer whose x position is past the finish line 
    * @return true if any racer's x position is past the finish line 
    *    or false if no racer's x position is past the finish line 
    */ 
    private boolean findWinner() 
    { 
    for (Racer r : racerList) 
    { 
     if (r.getX() > finishX ) 
     return true; 
    } 
    return false; 
    } 

    /** reportRaceResults : compiles winner names and prints message 
    * winners are all racers whose x position is past the finish line 
    */ 
    private void reportRaceResults() 
    { 
    raceIsOn = false; 
    String results = "Racer "; 
    for (int i = 0; i < racerList.size(); i ++) 
    { 
     if (racerList.get(i).getX() > finishX ) 
     { 
     results += (i + 1) + ", a " + racerList.get(i).getID() + ", "; 
     } 
    } 

    JOptionPane.showMessageDialog(this, results + " win(s) the race "); 

    } 

    /** reset: sets up for next race: 
    *  sets raceIsOn flag to false 
    *  clears the list of racers 
    *  resets racer position to FIRST_RACER 
    *  enables checkboxes and radio buttons 
    */ 
    private void reset() 
    { 
     char answer; 
     String input = JOptionPane.showInputDialog(this, "Another race? (y, n)"); 
     if (input == null || input.length() == 0) 
     { 
      System.out.println("Exiting"); 
      System.exit(0); 
     } 

     answer = input.charAt(0); 
     if (answer == 'y' || answer == 'Y') 
     { 
      raceIsOn = false; 
      racerList.clear(); 
      app.prepareToRace(); 
      app.runRace(); 
     } 
     else 
      System.exit(0); 
    } 

    /** main 
    * instantiates the RacePoly object app 
    * calls runRace method 
    */ 
    public static void main(String [] args) 
    { 
    app = new RacePoly(); 
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    app.prepareToRace(); 
    app.runRace(); 
    } 
    } 

레이서 클래스

import java.awt.Graphics; 

public abstract class Racer 
{ 
    private String ID; // racer ID 
    private int x;  // x position 
    private int y;  // y position 

    /** default constructor 
    * Sets ID to blank 
    */ 
    public Racer() 
    { 
    ID = ""; 
    } 

    /** Constructor 
    * @param rID racer ID 
    * @param rX x position 
    * @param rY y position 
    */ 
    public Racer(String rID, int rX, int rY) 
    { 
    ID = rID; 
    x = rX; 
    y = rY; 
    } 

    /** accessor for ID 
    * @return ID 
    */ 
    public String getID() 
    { 
    return ID; 
    } 

    /** accessor for x 
    * @return current x value 
    */ 
    public int getX() 
    { 
    return x; 
    } 

    /** accessor for y 
    * @return current y value 
    */ 
    public int getY() 
    { 
    return y; 
    } 

    /** mutator for x 
    * @param newX new value for x 
    */ 
    public void setX(int newX) 
    { 
    x = newX; 
    } 

    /** mutator for y 
    * @param newY new value for y 
    */ 
    public void setY(int newY) 
    { 
    y = newY; 
    } 

    /** abstract method for Racer's move 
    */ 
    public abstract void move(); 

    /** abstract method for drawing Racer 
    * @param g Graphics context 
    */ 
    public abstract void draw(Graphics g); 
} 

거북이 클래스

/** Tortoise class 
* inherits from abstract Racer class 
*/ 

import java.awt.Graphics; 
import java.awt.Color; 
import java.util.Random; 

public class Tortoise extends Racer 
{ 
    private int speed; 
    private Random rand; 

    /** Default Constructor: calls Racer default constructor 
    */ 
    public Tortoise() 
    { 
    super(); 

    // percentage of time (between 90 - 99%) that this tortoise moves each turn 
     rand = new Random(); 
    speed = rand.nextInt(10) + 90; 
    } 

    /** Constructor 
    * @param rID racer Id, passed to Racer constructor 
    * @param rX x position, passed to Racer constructor 
    * @param rY y position, passed to Racer constructor 
    */ 
    public Tortoise(String rID, int rX, int rY) 
    { 
    super(rID, rX, rY); 

    // percentage of time (between 90 - 99%) that this tortoise moves each turn 
     rand = new Random(); 
    speed = rand.nextInt(10) + 90; 
    } 

    /** move: calculates the new x position for the racer 
    * Tortoise move characteristics: "slow & steady wins the race" 
    *  increment x by 1 most of the time 
    */ 
    public void move() 
    { 
    int move = rand.nextInt(100) + 1; 
    if (move < speed) 
     setX(getX() + 1); 
    } 

    /** draw: draws the Tortoise at current (x, y) coordinate 
    *  @param g Graphics context 
    */ 
    public void draw(Graphics g) 
    { 
    int startX = getX(); 
    int startY = getY(); 

    g.setColor(new Color(34, 139, 34)); // dark green 

    //body 
    g.fillOval(startX - 30, startY, 25, 15); 

    //head 
    g.fillOval(startX - 10, startY + 5, 15, 10); 

    //flatten bottom 
     g.clearRect(startX - 30, startY + 11, 35, 4); 

    //feet 
    g.setColor(new Color(34, 139, 34)); // brown 
    g.fillOval(startX - 27, startY + 10, 5, 5); 
    g.fillOval(startX - 13, startY + 10, 5, 5); 
    } 
} 

헤어 클래스

/** Hare class 
* inherits from abstract Racer class 
*/ 

import java.awt.Graphics; 
import java.awt.Color; 
import java.util.Random; 

public class Hare extends Racer 
{ 
    /** Default Constructor: calls Racer default constructor 
    */ 
    public Hare() 
    { 
    super(); 
    } 

    /** Constructor 
    * @param rID racer Id, passed to Racer constructor 
    * @param rX x position, passed to Racer constructor 
    * @param rY y position, passed to Racer constructor 
    */ 
    public Hare(String rID, int rX, int rY) 
    { 
    super(rID, rX, rY); 
    } 

    /** move: calculates the new x position for the racer 
    * Hare move characteristics: 30% of the time, Hare jumps 5 pixels 
    *        70% of the time, Hare sleeps (no move) 
    * generates random number between 1 & 10 
    *   for 1 - 7, no change to x position 
    *   for 8 - 10, x position is incremented by 5 
    */ 
    public void move() 
    { 
     Random rand = new Random(); 
    int move = rand.nextInt(10) + 1 ; 

    if (getX() < 100) 
    { 
     if (move > 6) 
     setX(getX() + 4); 
    } 
    else 
    { 
     if (move > 8) 
     setX(getX() + 4); 
    } 
    } 

    /** draw: draws the Hare at current (x, y) coordinate 
    * @param g Graphics context 
    */ 
    public void draw(Graphics g) 
    { 
    int startY = getY(); 
    int startX = getX(); 

    // tail 
    g.setColor(Color.LIGHT_GRAY); 
    g.fillOval(startX - 37, startY + 8, 12, 12) ; 

    //body 
    g.setColor(Color.GRAY); 
    g.fillOval(startX - 30, startY, 20, 20); 

    //head 
    g.fillOval(startX - 13, startY + 2, 13, 8); 
    g.fillOval(startX - 13, startY - 8, 8, 28); 

    //flatten bottom 
    g.clearRect(startX - 37, startY + 15, 32, 5); 
    } 
} 
+1

실행 가능한 예제를 전혀 제공하지 않았다는 사실을 알려주십시오. 아마도 당신이 얻고있는 오류 메시지를 공유하고 싶을 것입니다 ... – MadProgrammer

+0

'draw (Graphics g);'틀린 것 같습니다 ... shouldn ... 'draw (g)'가 아니고'draw();로 확신하지 못했지만, 다시 결론을 이끌어 낼 수있는 컨텍스트가 없습니다 ... – MadProgrammer

+0

추상화 된 것이라면 메소드, 그들은 어딘가에 어떤 종류의 구현이 필요합니까? 그리고 MadProgrammer가 말했듯이 첫 번째 for 루프에서 Graphics를 메서드 호출에 사용하지 말아야합니다. g. 그래서, 'draw (g);' – Tristan

답변

0

원래 그리기 방법을 보면, 당신은 실제로 레이서 객체를 확장 뭔가 그릴 호출 할 필요는

/** 
    * paint method 
    * 
    * @param g 
    *   Graphics context draws the finish line; moves and draws 
    *   racers 
    */ 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     // draw the finish line 
     finishX = getWidth() - 20; 
     g.setColor(Color.blue); 
     g.drawLine(finishX, 0, finishX, getHeight()); 

     if (raceIsOn) { 
      /* 
      * loop through instance variable ArrayList racerList, which 
      * contains Racer object references, calling draw and move for 
      * each element. 
      */ 
      for (int i = 0; i < racerList.size(); i++) { 
       racerList.get(i).move(); 
       racerList.get(i).draw(g); 
      } 

     } else // display racers before race begins 
     { 

      /* 
      * loop through instance variable ArrayList racerList, which 
      * contains Racer object references, calling draw for each 
      * element. 
      */ 
      for (int i = 0; i < racerList.size(); i++) { 
       racerList.get(i).draw(g); 
      } 

     } 
    } 

나는 또한 당신이 뭔가 이상한을 사용주의 일시 중지? 그냥 Thread.sleep(30); 대신 사용하십시오. 이러한 변경을 한 후에 프로그램을 잘 실행할 수있었습니다.

+0

Dang, 나는 이것을 일찍 시도했다고 생각했습니다! 내 구문이 생각이났다. 정말 고마워!!! – jbeaux

관련 문제