任务是模拟一场"比赛",两名选手用1(红色矩形)2(蓝色矩形)表示。当我按下开始按钮时,我应该使用线程来增加矩形的宽度,第一个达到屏幕宽度的线程是赢家。一切都正常工作,除了由于某种未知的原因,我无法让这些矩形出现。
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();
}
}
}
注意拼写和大写。注意:
public void PainComponent(Graphics g)
应为:
public void paintComponent(Graphics g)