框架
直接上代码
main 主类(启动类)
import javax.swing.*;
public class main {
public static void main(String[] args) {
JFrame frame = new JFrame("交通灯");
frame.setBounds(10,10,655,685);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new ShowPanel());
frame.setVisible(true);
}
}
Data 类(图片数据初始化)
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL upURL = Data.class.getResource("images/car_up.jpg");
public static ImageIcon up = new ImageIcon(upURL);
public static URL downURL = Data.class.getResource("images/car_down.jpg");
public static ImageIcon down = new ImageIcon(downURL);
public static URL rightURL = Data.class.getResource("images/car_left.jpg");
public static ImageIcon right = new ImageIcon(rightURL);
public static URL leftURL = Data.class.getResource("images/car_right.jpg");
public static ImageIcon left = new ImageIcon(leftURL);
public static URL wayURL = Data.class.getResource("images/way.jpg");
public static ImageIcon way = new ImageIcon(wayURL);
public static URL red1URL = Data.class.getResource("images/red_light1.jpg");
public static ImageIcon red1 = new ImageIcon(red1URL);
public static URL red2URL = Data.class.getResource("images/red_light2.jpg");
public static ImageIcon red2 = new ImageIcon(red2URL);
public static URL red3URL = Data.class.getResource("images/red_light3.jpg");
public static ImageIcon red3 = new ImageIcon(red3URL);
public static URL red4URL = Data.class.getResource("images/red_light4.jpg");
public static ImageIcon red4 = new ImageIcon(red4URL);
public static URL yellow1URL = Data.class.getResource("images/yellow_light1.jpg");
public static ImageIcon yellow1 = new ImageIcon(yellow1URL);
public static URL yellow2URL = Data.class.getResource("images/yellow_light2.jpg");
public static ImageIcon yellow2 = new ImageIcon(yellow2URL);
public static URL yellow3URL = Data.class.getResource("images/yellow_light3.jpg");
public static ImageIcon yellow3 = new ImageIcon(yellow3URL);
public static URL yellow4URL = Data.class.getResource("images/yellow_light4.jpg");
public static ImageIcon yellow4 = new ImageIcon(yellow4URL);
public static URL green1URL = Data.class.getResource("images/green_light1.jpg");
public static ImageIcon green1 = new ImageIcon(green1URL);
public static URL green2URL = Data.class.getResource("images/green_light2.jpg");
public static ImageIcon green2 = new ImageIcon(green2URL);
public static URL green3URL = Data.class.getResource("images/green_light3.jpg");
public static ImageIcon green3 = new ImageIcon(green3URL);
public static URL green4URL = Data.class.getResource("images/green_light4.jpg");
public static ImageIcon green4 = new ImageIcon(green4URL);
public static URL directionURL = Data.class.getResource("images/direction.jpg");
public static ImageIcon direction = new ImageIcon(directionURL);
}
ShowPanel类 (画板)
import com.sun.org.apache.bcel.internal.generic.SWITCH;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class ShowPanel extends JPanel implements ActionListener {
int[] carX = new int[4];
int[] carY = new int[4];
String[] direction = new String[4]; //汽车行驶的方向
//4个交通灯的位置
int[] lightX = new int[4];
int[] lightY = new int[4];
//方向图位置
int DirectionLocationX;
int DirectionLocationY;
//汽车的转向
String[] turn = new String[4];
//交通灯的颜色
String[] light = new String[4];
//计时
int time ;
//定时器 以毫秒为单位
Timer timer = new Timer(10, this);//10ms执行1次
JTextField jt;
public ShowPanel() {
init();
//获得焦点和键盘事件
//画时间
jt= new JTextField();
jt.setColumns(5);
this.add(jt,BorderLayout.CENTER);
this.setFocusable(true); //获取焦点事件
timer.start(); //游戏一开始时定时器启动
}
public void init() {
time = 0;
for (int i = 0; i < 4; i++) {
int a = new Random().nextInt(3);
switch(a) {
case 0:
turn[i] = "S";
break;
case 1:
turn[i] = "L";
break;
case 2:
turn[i] = "R";
break;
}
//System.out.print(a);
}
System.out.println("===S:直行,L:左转,R:右转==");
System.out.println("开往北边方向的车的转向:"+turn[0]);
System.out.println("开往南边方向的车的转向:"+turn[1]);
System.out.println("开往西边方向的车的转向:"+turn[2]);
System.out.println("开往东边方向的车的转向:"+turn[3]);
carX[0] = 320;
carY[0] = 590;
direction[0] = "N";
carX[1] = 270;
carY[1] = 10;
direction[1] = "S";
carX[2] = 10;
carY[2] = 320;
direction[2] = "E";
carX[3] = 590;
carY[3] = 270;
direction[3] = "W";
//配置红绿灯的位置
lightX[0] =430;
lightY[0] =170;
lightX[1] =110;
lightY[1] =430;
lightX[2] =430;
lightY[2] =430;
lightX[3] =170;
lightY[3] =110;
//初始化方向图的位置
DirectionLocationX = 460;
DirectionLocationY = 10;
//初始化红绿灯,南北通行
light[0] = "G";
light[1] = "G";
light[2] = "R";
light[3] = "R";
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //清屏
this.setBackground(Color.white);
//绘制道路显示面板
Data.way.paintIcon(this, g, 0, 0);
//根据车行驶的方向画车
for (int i = 0; i <4 ; i++) {
if(direction[i].equals("N")){
Data.up.paintIcon(this,g,carX[i],carY[i]);
}
if(direction[i].equals("S")){
Data.down.paintIcon(this,g,carX[i],carY[i]);
}
if(direction[i].equals("E")){
Data.left.paintIcon(this,g,carX[i],carY[i]);
}
if(direction[i].equals("W")){
Data.right.paintIcon(this,g,carX[i],carY[i]);
}
}
//画方向图
Data.direction.paintIcon(this,g,DirectionLocationX,DirectionLocationY);
/* Data.up.paintIcon(this,g,carX[0],carY[0]);
Data.down.paintIcon(this,g,carX[1],carY[1]);
Data.left.paintIcon(this,g,carX[2],carY[2]);
Data.right.paintIcon(this,g,carX[3],carY[3]);
*/
//画红绿灯
switch(light[0]){
case "R" : Data.red1.paintIcon(this,g,lightX[0],lightY[0]); break;
case "Y" : Data.yellow1.paintIcon(this,g,lightX[0],lightY[0]); break;
case "G" : Data.green1.paintIcon(this,g,lightX[0],lightY[0]); break;
}
switch(light[1]){
case "R" : Data.red2.paintIcon(this,g,lightX[1],lightY[1]); break;
case "Y" : Data.yellow2.paintIcon(this,g,lightX[1],lightY[1]); break;
case "G" : Data.green2.paintIcon(this,g,lightX[1],lightY[1]); break;
}
switch(light[2]){
case "R" : Data.red3.paintIcon(this,g,lightX[2],lightY[2]); break;
case "Y" : Data.yellow3.paintIcon(this,g,lightX[2],lightY[2]); break;
case "G" : Data.green3.paintIcon(this,g,lightX[2],lightY[2]); break;
}
switch(light[3]){
case "R" : Data.red4.paintIcon(this,g,lightX[3],lightY[3]); break;
case "Y" : Data.yellow4.paintIcon(this,g,lightX[3],lightY[3]); break;
case "G" : Data.green4.paintIcon(this,g,lightX[3],lightY[3]); break;
}
}
@Override
public void actionPerformed(ActionEvent e) {
//画时间标签
int times = time/100;
String s = ""+times+"秒";
jt.setText(s);
//汽车动起来
//交通灯控制汽车的运行
switch (light[0]) {
case "R":
case "Y":
if(turn[0].equals("R")){
if(carY[0] > 320){
carY[0] = carY[0] - 1;
}else {
direction[0] = "E";
carX[0] = carX[0] + 1;
}
}else {
if (carY[0] == 400) carY[0] = 400;
else carY[0] = carY[0] - 1;
}
break;
case "G":
if(turn[0].equals("S")){
carY[0] = carY[0] - 1;
}
if(turn[0].equals("L")) {
if (carY[0] > 270) {
carY[0] = carY[0] - 1;
} else {
direction[0] = "W";
carX[0] = carX[0] - 1;
}
}
if(turn[0].equals("R")){
if(carY[0] > 320){
carY[0] = carY[0] - 1;
}else {
direction[0] = "E";
carX[0] = carX[0] + 1;
}
}
break;
}
switch (light[1]) {
case "R":
case "Y":
if(turn[1].equals("R")){
if(carY[1] < 270){
carY[1] = carY[1] + 1;
}else {
direction[1] = "W";
carX[1] = carX[1] - 1;
}
}else {
if (carY[1] == 185) carY[1] = 185;
else carY[1] = carY[1] + 1;
}
break;
case "G":
if(turn[1].equals("S")){
carY[1] = carY[1] + 1;
}
if(turn[1].equals("L")) {
if (carY[1] < 320) {
carY[1] = carY[1] + 1;
} else {
direction[1] = "E";
carX[1] = carX[1] + 1;
}
}
if(turn[1].equals("R")){
if(carY[1] < 270){
carY[1] = carY[1] + 1;
}else {
direction[1] = "W";
carX[1] = carX[1] - 1;
}
}
break;
}
switch (light[2]) {
case "R":
case "Y":
if(turn[2].equals("R")){
if(carX[2] < 270){
carX[2] = carX[2] + 1;
}else{
direction[2] = "S";
carY[2] = carY[2] + 1;
}
}else {
if (carX[2] == 185) carX[2] = 185;
else carX[2] = carX[2] + 1;
}
break;
case "G":
if(turn[2].equals("S")){
carX[2] = carX[2] + 1;
}
if(turn[2].equals("L")){
if(carX[2] < 320){
carX[2] = carX[2] + 1;
}else{
direction[2] = "N";
carY[2] = carY[2] - 1;
}
}
if(turn[2].equals("R")){
if(carX[2] < 270){
carX[2] = carX[2] + 1;
}else{
direction[2] = "S";
carY[2] = carY[2] + 1;
}
}
break;
}
switch (light[3]) {
case "R":
case "Y":
if(turn[3].equals("R")){
if(carX[3] > 320){
carX[3] = carX[3] - 1;
}else{
direction[3] = "N";
carY[3] = carY[3] - 1;
}
}else {
if (carX[3] == 400 && !turn[3].equals("R")) carX[3] = 400;
else carX[3] = carX[3] - 1;
}
break;
case "G":
if(turn[3].equals("S")){
carX[3] = carX[3] - 1;
}
if(turn[3].equals("L")){
if(carX[3] > 270){
carX[3] = carX[3] - 1;
}else{
direction[3] = "S";
carY[3] = carY[3] + 1;
}
}
if(turn[3].equals("R")){
if(carX[3] > 320){
carX[3] = carX[3] - 1;
}else{
direction[3] = "N";
carY[3] = carY[3] - 1;
}
}
break;
}
time++;
if(time%1000==0){
if(light[0].equals("G")){
light[0] = "Y";
light[1] = "Y";
}
if(light[2].equals("G")){
light[2] = "Y";
light[3] = "Y";
}
}
if(time%1101==0){
if(light[0].equals("Y")){
light[0] = "R";
light[1] = "R";
light[2] = "G";
light[3] = "G";
}
if(light[2].equals("Y")){
light[0] = "G";
light[1] = "G";
light[2] = "R";
light[3] = "R";
}
}
if(time%2000==0){
init();
}
repaint();
}
}
程序运行结果