2014-04-19 5 views
0

L'affectation consistait à simuler une «course» avec deux coureurs représentés par 1 (rectangle rouge) 2 (rectangle bleu). quand je tape sur le bouton de démarrage, je suis censé utiliser des fils pour incrémenter la largeur du rectangle et le premier pour atteindre la largeur de l'écran est le gagnant. Tout fonctionne correctement sauf que pour une raison inconnue, je ne peux pas obtenir ces rectangles à apparaître.Fils et applets Java

HTML:

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

panneau

//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"); 
      } 
      } 

     } 
} 
} 

et enfin la classe qui étend 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(); 
    } 
} 
} 

Répondre

0

attention à l'orthographe et à la capitalisation. Notez que vous avez:

public void PainComponent(Graphics g) 

devrait être:

public void paintComponent(Graphics g) 
+0

wow ... Merci. Parfois, cela aide quelqu'un d'autre à regarder par-dessus – user3474876