2015-11-15 65 views
1

我有問題讓我的馬位置更新我的揮杆應用程序內。我已經嘗試了多種方法來更新馬匹的xPosition,因爲它們在代表賽道的擺動面板上刷新。Java搖擺馬模擬

public class HorseModel{ 

    /*Horse dimensions*/ 
    private int x; 
    private int y; 
    private final int horsePerimeter = 20; 

    public HorseModel(int xT, int yT){ 
    /*Constructor*/ 
     x = xT; 
     y = yT; 
    } 
    public void setDimensions(int dimX, int dimY){ 
    /*Sets program dimensions*/ 
     x = dimX; 
     y = dimY; 
    } 
    public void createHorse(Graphics2D h){ 
    /*Paints HorseModel on screen as 2 dimensional object*/ 

     Ellipse2D.Double horseModel = new Ellipse2D.Double(x, y, horsePerimeter, horsePerimeter); 
     h.setColor(Color.RED); 
     h.fill(horseModel); 
     h.setColor(Color.YELLOW); 
     h.draw(horseModel); 
    } 
} 
public class HorseMovement implements Runnable{ 

    public final int xStartPos = 10; //change 
    public final int yStartPos = 20; 

    private RaceTrack hRaceTrack; 
    private HorseModel Horse2D; 
    private int xPos, yPos; 

    public HorseMovement(RaceTrack r, int yPos_Spacing){ 
    /*Constructor*/ 
     xPos = xStartPos; 
     yPos = yStartPos * yPos_Spacing; 
     Horse2D = new HorseModel(xPos, yPos); 
     hRaceTrack = r; 
    } 
    public HorseModel moveHorse(HorseModel horseObject){ 
    /*Updates horse positon*/ 
     horseObject = new HorseModel(xPos++, yPos); 
     return horseObject; 
    } 
    public void paintComponent(Graphics h){ 
    /*paints the new horse after movement*/ 
     this.Horse2D = moveHorse(Horse2D); 
     Graphics2D hMod = (Graphics2D) h; 
     Horse2D.createHorse(hMod); 
    } 
    public void run(){ 
    /*Repaints the horse models as they increment movement across the screen*/ 
     hRaceTrack.repaint(); 
     hRaceTrack.revalidate(); 
    } 
} 
public class RacePanel extends JFrame{ 

    /*Frame Buttons*/ 
    private JPanel mPanel; 
    private JButton startRace = new JButton("Start Race"); 
    private JButton stopRace = new JButton("Stop Race"); 
    private JButton startOver = new JButton("Start Over"); 


    /*Panel to fill with HorseModels for race*/ 
    private RaceTrack rTrack; 

    /*Window dimensions*/ 
    public int Window_Height = 1024; 
    public int Window_Width = 768; 

    public RacePanel(){ 
    /*Constructor*/ 

     initGui(); 
     initRace(); 
     initQuit(); 

     setSize(Window_Width, Window_Height); 
    } 
    public void initGui(){ 
    /*Initializes the main race panel and sets button positions and layouts*/ 
     mPanel = new JPanel(new BorderLayout()); 
     rTrack = new RaceTrack(); 

     JPanel horsePanel = new JPanel(); //panel to house horse objects before running across screen 

     horsePanel.setLayout(new GridLayout(1, 3)); 

     positionJPanels(horsePanel, mPanel); 
    } 
    public void initRace(){ 
    /*implements action listener for start race button*/ 
     class StartRace implements ActionListener{ 
      public void actionPerformed(ActionEvent e){ 
       startRace.setEnabled(true); 
       rTrack.initTrack(); 
      } 
     } 
     ActionListener event = new StartRace(); 
     stopRace.addActionListener(event); 
    } 
    public void initQuit(){ 
    /*Implements the action listener for stop race button*/ 
     class StopRace implements ActionListener{ 
      public void actionPerformed(ActionEvent e){ 
       System.exit(0);//exits program if race is stopped 
      } 
     } 
     ActionListener event = new StopRace(); 
     stopRace.addActionListener(event); 
    } 
    public void positionJPanels(JPanel h, JPanel p){ 
    /*Handles adding buttons to a JPanel*/ 
     h.add(startRace); 
     h.add(startOver); 
     h.add(stopRace); 

     p.add(h, BorderLayout.NORTH); //sets the horse panel buttons to the top of the layout 
     p.add(rTrack, BorderLayout.CENTER); //sets 

     add(p); 
    } 
} 

public class RaceController { 

    public static void main(String[] args){ 
     new RaceController(); 
    } 
    public RaceController(){ 
    /*Constructor*/ 
     JFrame mFrame = new RacePanel(); 
     mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     mFrame.setVisible(true); 
    } 

} 
public class RaceTrack extends JPanel{ 

    /*Sets horses within race track*/ 
    private int numOfHorseObjects = 5;// change this to a dynamic 
    private int numOfThreads = 25; 

    /*Holds horse thread from HorseObject class*/ 
    private ArrayList<HorseMovement> horses = new ArrayList<>(); 
    private ArrayList<Thread> threads = new ArrayList(numOfThreads); 

    public RaceTrack(){ 
    /*Constructor*/ 
     setBackground(Color.black); 
     reset(); 
    } 
    public void initTrack(){ 
    /*Starts the RaceTrack simulation*/ 
     threads.clear(); //clears the thread arraylist still residing 

     for(int i = 0; i < horses.size(); i++){ 
      Thread T = new Thread(horses.get(i)); 
      T.start(); 
      threads.add(T); 
     } 
    } 
    public void reset(){ 
    /*resets horse position within screen*/ 
     horses.clear(); 
     for(int i = 0; i < numOfHorseObjects; i++){ 
      horses.add(new HorseMovement(this, i + 1)); 
     } 
    } 
    public void paintComponent(Graphics g){ 
    /*overrides graphics paint method in order to paint the horse movements 
    * through the arraylist of HorseMovements*/ 
     super.paintComponent(g); 
     for(HorseMovement h : horses){ 
      h.paintComponent(g); 
     } 
    } 
} 

回答

3

問題與您的代碼:

  • 你從來沒有給startRace JButton的一個ActionListener,所以如何按鈕將會有什麼影響,以及如何在比賽中曾經打算開始?請注意,您將StartRace ActionListener對象添加到stopRace JButton,並且我猜測這是錯誤的。
  • 即使您將偵聽器添加到startRace按鈕,動作偵聽器也只會將所有馬匹推進一個「步驟」,而不會再執行 - 在後臺線程中沒有循環可以重複執行動作。
  • 您似乎在不必要地創建新的Horse2D對象。爲什麼不簡單地推進現有Horse2D對象的位置?
  • 我自己,我會使用一個單一的Swing Timer而不是一堆線程來簡化代碼。