2014-04-19 4 views
0

할당은 1 (붉은 색 직사각형) 2 (파란색 직사각형)로 표현 된 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