2014-04-19 97 views
0

這個任務是模擬兩個用1(紅色矩形)2(藍色矩形)表示的跑步者的'比賽'。當我點擊開始按鈕時,我應該使用線程來增加矩形的寬度,第一個到達屏幕寬度的是贏家。一切正常,除了一些未知的原因,我不能讓這些矩形出現。Java線程和小應用程序

HTML:

<HTML> 
<BODY> 
<APPLET CODE="AppletGame.class" WIDTH="300" HEIGHT="400"> 
</APPLET> 
</BODY> 
</HTML> 

面板

//extends thread 
//  thread contains the run(); method and sets rectangle specifics 
import java.util.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class ThreadPanel extends JPanel 
{ 
//private Thread blueRunner = 
private JLabel winnerLabel, gamelabel; 
private int redWidth = 2, blueWidth = 2, x, y; 
private JPanel board; 
private Random generator = new Random(); 
private Thread RedRunner = new RedRunner(); 
private Thread BlueRunner = new BlueRunner(); 
    //Create a panel for the button and label 
    //then create a pannel for the Rectangles(runners) to race 
public ThreadPanel() 
{ 
    winnerLabel = new JLabel ("Winner: "); 
    winnerLabel.setBackground(Color.green); 
    add(winnerLabel); 

    setPreferredSize(new Dimension(300,360)); 
    setBackground (Color.white); 
} 

//graphics method to paint the shapes 
public void PainComponent(Graphics g) 
{ 
    //draws 2 seperate rectangles 
    super.paintComponent(g); 

    this.setBackground(Color.WHITE); 

    g.setColor(Color.RED); 
    g.fillRect(25, 100,redWidth, 2); 

    g.setColor(Color.BLUE); 
    g.fillRect(25, 100, blueWidth, 2); 
} 
    //begins game and starts two threads 
    public void startGame() 
    { 
    RedRunner.start(); 
    BlueRunner.start(); 
    } 

//create the threads that contain the (run method) 
//x is the red runner, y for blue (to get random numbers) 
class RedRunner extends Thread 
{ 
    public RedRunner(){ 
    } 

    public void run() 
    { 
     while(redWidth<=300) 
     { 
     x = generator.nextInt(6); 
     redWidth+=x; 
      if(redWidth==300 && blueWidth <300) 
      { 
      winnerLabel.setText("Winner: Red"); 
      } 

     } 
    } 
} 

//creates the second runner 
class BlueRunner extends Thread 
{ 
     public BlueRunner(){ 
     } 

     public void run() 
     { 
      while(redWidth<300) 
      { 
      y = generator.nextInt(6); 
      blueWidth+=y; 
      if(redWidth<300 && blueWidth<300) 
      { 
       winnerLabel.setText("Winner: Blue"); 
      } 
      } 

     } 
} 
} 

終於類,它擴展JApplet的:

//#2 
    //java file that extends JAPPLET which implements action listener 
    //containts a button to start the game 
    //containts instantiation of panel which contain the threads/runners 
//makes a rectangle object 
    //IE constructs the objects 
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; 

    public class AppletGame extends JApplet implements ActionListener 
    { 
//applet that simulates 2 runners 
    private final int BOARDWIDTH = 300, BOARDHEIGHT =400; 

    private JButton startPlay; 
    private ThreadPanel game; 
    private String red = "Red", blue = "Blue"; 
    private int hits=0; 

    private JPanel appletPanel, buttons; 

    //----------------------------------------------------------------- 
    // Set up the components for the applet 
    //----------------------------------------------------------------- 
public void init() 
{ 
    buttons = new JPanel(); 
    buttons.setOpaque(true); 
    buttons.setPreferredSize(new Dimension(BOARDWIDTH, 40)); 
    buttons.setBackground(Color.white); 

    startPlay = new JButton("Start Race"); 
    startPlay.setBackground(Color.cyan); 

    startPlay.addActionListener(this); //adds button listener for starting 

    buttons.add(startPlay); 

    game = new ThreadPanel(); //panel for the race portion of the applet 

    appletPanel = new JPanel(); 
    appletPanel.add(buttons);//ands buttons and game panel to primary panel 
    appletPanel.add(game); 

    getContentPane().add(appletPanel); 
     setSize(BOARDWIDTH, BOARDHEIGHT);  //sets size of primary panel 

} 

public void actionPerformed (ActionEvent event) 
{ 
    String msg = "Race over Click to start again"; 
    if(event.getSource() == startPlay) 
    { 
     startPlay.setEnabled(false); 
     game.startGame(); 
     repaint(); 
    } 
} 
} 

回答

0

注重拼寫和大小寫。請注意,你必須:

public void PainComponent(Graphics g) 

應該是:

public void paintComponent(Graphics g) 
+0

哇...謝謝你。有時它可以幫助別人查看它 – user3474876