1.package com.hechao;
import java.awt.Color;
import java.awt.Graphics;
public class Car implements Runnable{
private String name;
private int speed;
private int x = 50;
public Car(String name) {
this.name = name;
this.speed = (int) (Math.random() * 8 + 1);
}
public void move(){
x += speed;
}
public void draw(Graphics g, int y) {
Color color = g.getColor();
g.setColor(Color.BLUE);
g.fillRect(x, y, 50, 40);
g.setColor(Color.BLACK);
g.drawString(name, x + 15, y + 25);
g.setColor(color);
}
@Override
public void run() {
this.move();
}
public int getX() {
return x;
}
public String getName() {
return name;
}
}
package com.hechao;
import java.awt.Color;
import java.awt.Graphics;
public class Car implements Runnable{
private String name;
private int speed;
private int x = 50;
public Car(String name) {
this.name = name;
this.speed = (int) (Math.random() * 8 + 1);
}
public void move(){
x += speed;
}
public void draw(Graphics g, int y) {
Color color = g.getColor();
g.setColor(Color.BLUE);
g.fillRect(x, y, 50, 40);
g.setColor(Color.BLACK);
g.drawString(name, x + 15, y + 25);
g.setColor(color);
}
@Override
public void run() {
this.move();
}
public int getX() {
return x;
}
public String getName() {
return name;
}
}
2.package com.hechao;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class CarGameFrame extends JFrame {
private JButton startButton, stopButton;
private List<Car> list = new ArrayList<Car>();
private Image offImage = new BufferedImage(1000, 600, 1);
private Track myTrack = new Track();
private Timer timer = null;
private ExecutorService service = Executors.newFixedThreadPool(5);
private JLabel isOverLabel;
public CarGameFrame() {
this.setTitle("赛车游戏");
this.setSize(1000, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initialize();
}
private void initialize(){
//标签初始化
isOverLabel = new JLabel();
isOverLabel.setBounds(200, 530, 100, 30);
isOverLabel.setFont(new Font("微软雅黑", 1, 14));
this.add(isOverLabel);
//初始化按钮
startButton = new JButton("开始");
startButton.setBounds(400, 530, 60, 30);
startButton.setEnabled(true);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
});
this.add(startButton);
stopButton = new JButton("暂停");
stopButton.setBounds(500, 530, 60, 30);
stopButton.setEnabled(true);
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timer.stop();
stopButton.setEnabled(false);
startButton.setEnabled(true);
}
});
this.add(stopButton);
//初始化车队
list.add(new Car("01"));
list.add(new Car("02"));
list.add(new Car("03"));
list.add(new Car("04"));
list.add(new Car("05"));
//添加时钟
timer = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < list.size(); i++) {
service.execute(list.get(i));
if (list.get(i).getX() >= 850) {
isOverLabel.setText(list.get(i).getName() + "号选手获胜!");
timer.stop();
}
}
repaint();
}
});
}
@Override
public void paint(Graphics g) {
Graphics myG = offImage.getGraphics();
super.paint(myG);
myTrack.draw(myG);
for(int i = 0; i < list.size(); i++){
list.get(i).draw(myG, 80 + i * 100);
}
g.drawImage(offImage, 0, 0, null);
}
}
package com.hechao;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class CarGameFrame extends JFrame {
private JButton startButton, stopButton;
private List<Car> list = new ArrayList<Car>();
private Image offImage = new BufferedImage(1000, 600, 1);
private Track myTrack = new Track();
private Timer timer = null;
private ExecutorService service = Executors.newFixedThreadPool(5);
private JLabel isOverLabel;
public CarGameFrame() {
this.setTitle("赛车游戏");
this.setSize(1000, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initialize();
}
private void initialize(){
//标签初始化
isOverLabel = new JLabel();
isOverLabel.setBounds(200, 530, 100, 30);
isOverLabel.setFont(new Font("微软雅黑", 1, 14));
this.add(isOverLabel);
//初始化按钮
startButton = new JButton("开始");
startButton.setBounds(400, 530, 60, 30);
startButton.setEnabled(true);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
});
this.add(startButton);
stopButton = new JButton("暂停");
stopButton.setBounds(500, 530, 60, 30);
stopButton.setEnabled(true);
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timer.stop();
stopButton.setEnabled(false);
startButton.setEnabled(true);
}
});
this.add(stopButton);
//初始化车队
list.add(new Car("01"));
list.add(new Car("02"));
list.add(new Car("03"));
list.add(new Car("04"));
list.add(new Car("05"));
//添加时钟
timer = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < list.size(); i++) {
service.execute(list.get(i));
if (list.get(i).getX() >= 850) {
isOverLabel.setText(list.get(i).getName() + "号选手获胜!");
timer.stop();
}
}
repaint();
}
});
}
@Override
public void paint(Graphics g) {
Graphics myG = offImage.getGraphics();
super.paint(myG);
myTrack.draw(myG);
for(int i = 0; i < list.size(); i++){
list.get(i).draw(myG, 80 + i * 100);
}
g.drawImage(offImage, 0, 0, null);
}
}
3.package com.hechao;
import java.awt.Color;
import java.awt.Graphics;
/**
* 赛道
* @author hechao
*
*/
public class Track {
public Track(){
}
public void draw(Graphics g){
Color color = g.getColor();
g.setColor(Color.GREEN);
g.fillRect(50, 50, 50, 500);
g.drawRect(50, 50, 900, 500);
g.setColor(Color.RED);
g.fillRect(900, 50, 50, 500);
g.setColor(color);
}
}
package com.hechao;
import java.awt.Color;
import java.awt.Graphics;
/**
* 赛道
* @author hechao
*
*/
public class Track {
public Track(){
}
public void draw(Graphics g){
Color color = g.getColor();
g.setColor(Color.GREEN);
g.fillRect(50, 50, 50, 500);
g.drawRect(50, 50, 900, 500);
g.setColor(Color.RED);
g.fillRect(900, 50, 50, 500);
g.setColor(color);
}
}
4.package com.hechao;
import javax.swing.UIManager;
public class Test01 {
public static void main(String[] args) {
new CarGameFrame().setVisible(true);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.hechao;
import javax.swing.UIManager;
public class Test01 {
public static void main(String[] args) {
new CarGameFrame().setVisible(true);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
}