起因:
很久之前就想做做这类的小程序练手,现在才开始动工哈哈哈。
从最简单的开始做起,这次是一个交通灯。
实现了交通灯的简单显示转换循环功能。
基于 java.swing 。
先上效果图。超级超级超级简陋哈哈哈
红灯 黄灯 绿灯
上代码
1 import java.awt.Color; 2 import java.awt.Font; 3 import java.awt.Graphics; 4 import java.util.Timer; 5 import java.util.TimerTask; 6 7 import javax.swing.JFrame; 8 import javax.swing.JLabel; 9 import javax.swing.JPanel; 10 11 /** 12 * @ClassName: TrafficLights 13 * @Description: 实现简单的交通灯转换功能 14 * @author Arrack 15 * @date 2018年1月30日 上午9:49:54 16 * 17 */ 18 public class TrafficLights extends JPanel { 19 /* 20 * serialVersionUID 21 * 相当于java类的身份证。主要用于版本控制。 22 * serialVersionUID作用是序列化时保持版本的兼容性, 23 * 即在版本升级时反序列化仍保持对象的唯一性。 24 * 摘自百度知道 https://zhidao.baidu.com/question/321447087.html 25 */ 26 private static final long serialVersionUID = 1L; 27 28 /* 29 * 定义一系列静态常量 30 * 程序窗口宽度高度,三种颜色亮灯,为空(都熄灭) 31 */ 32 private static final int winWidth = 500; 33 private static final int winHeight = 300; 34 private static final int BLANK = 0; 35 private static final int RED = 1; 36 private static final int YELLOW = 2; 37 private static final int GREEN = 3; 38 39 private static int color = BLANK; // 将初始状态设置为都熄灭状态 40 41 /* 42 * 计算灯的位置和大小(仅与窗口高度和宽度有关) 43 * ps:绘制椭圆时,drawOval(int x, int y, int width, int height) 44 * x,y 为椭圆外接矩形左上顶点的坐标,width,height为矩形两边长。 45 */ 46 private int ballWidth = winHeight / 3; 47 private int ballX = winWidth / 2 - ballWidth / 2 - winWidth / 50, 48 ballY = winHeight / 2 - ballWidth + winHeight / 15; 49 private int ballPadding = (winWidth - 3 * ballWidth) / 4; 50 private int yellowX = ballX; 51 private int redX = ballX - ballPadding - ballWidth; 52 private int greenX = ballX + ballPadding + ballWidth; 53 54 /* 55 * Label 倒计时指示 56 */ 57 private JLabel l = new JLabel(""); 58 59 public TrafficLights() { 60 l.setFont(new Font("Arial", Font.BOLD, 20)); 61 add(l); 62 } 63 64 /** 65 * @Title: changeLights 66 * @Description: 利用timer类,进行灯的变换和闪烁,并显示倒计时 67 * @param @param timeR 红灯持续时间 单位秒 68 * @param @param timeY 黄灯持续时间 69 * @param @param timeG 绿灯持续时间 70 * @return void 71 */ 72 public void changeLights(final int timeR, final int timeY, final int timeG) { 73 final Timer timer = new Timer(); 74 final int num = timeR + timeG + timeY; 75 timer.schedule(new TimerTask() { 76 int i = 1; 77 78 public void run() { 79 i %= num; 80 if (i > 0 && i <= timeR) { // 红灯的时间,红灯持续亮 81 color = RED; 82 l.setText(Integer.toString(timeR + 1 - i)); 83 } else if (i > timeR && i <= timeR + timeY) { // 黄灯的时间,黄灯闪烁,0.6s闪一次 84 color = YELLOW; 85 l.setText(Integer.toString(timeY + 1 86 - (i % timeY != 0 ? i % timeY : timeY))); 87 timer.schedule(new TimerTask() { 88 public void run() { 89 color = BLANK; 90 repaint(); 91 } 92 }, 600); 93 } else { // 绿灯的时间,持续亮 94 color = GREEN; 95 l.setText(Integer.toString(timeG + 1 96 - (i % timeG != 0 ? i % timeG : timeG))); 97 } 98 repaint(); 99 i++; 100 } 101 }, 0, 1000); 102 103 } 104 105 /* 106 * Title: paintComponent 107 * Description: 重写绘制内容函数,利用switch选取显示内容(遮挡方式) 108 * @param g 109 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) 110 */ 111 protected void paintComponent(Graphics g) { 112 113 super.paintComponent(g); 114 115 g.setColor(Color.RED); 116 g.fillOval(redX, ballY, ballWidth, ballWidth); 117 g.setColor(Color.YELLOW); 118 g.fillOval(yellowX, ballY, ballWidth, ballWidth); 119 g.setColor(Color.GREEN); 120 g.fillOval(greenX, ballY, ballWidth, ballWidth); 121 g.setColor(this.getBackground()); 122 switch (color) { 123 case RED: 124 g.fillRect(yellowX, ballY, greenX - yellowX + ballWidth, ballWidth); 125 g.setColor(Color.BLACK); 126 g.drawOval(yellowX, ballY, ballWidth, ballWidth); 127 g.drawOval(greenX, ballY, ballWidth, ballWidth); 128 break; 129 case YELLOW: 130 g.fillRect(redX, ballY, ballWidth, ballWidth); 131 g.fillRect(greenX, ballY, ballWidth, ballWidth); 132 g.setColor(Color.BLACK); 133 g.drawOval(redX, ballY, ballWidth, ballWidth); 134 g.drawOval(greenX, ballY, ballWidth, ballWidth); 135 break; 136 case GREEN: 137 g.fillRect(redX, ballY, yellowX - redX + ballWidth, ballWidth); 138 g.setColor(Color.BLACK); 139 g.drawOval(redX, ballY, ballWidth, ballWidth); 140 g.drawOval(yellowX, ballY, ballWidth, ballWidth); 141 break; 142 case BLANK: 143 g.fillRect(redX, ballY, greenX - redX + ballWidth, ballWidth); 144 g.setColor(Color.BLACK); 145 g.drawOval(redX, ballY, ballWidth, ballWidth); 146 g.drawOval(yellowX, ballY, ballWidth, ballWidth); 147 g.drawOval(greenX, ballY, ballWidth, ballWidth); 148 default: 149 break; 150 } 151 } 152 153 public static void main(String[] args) { 154 JPanel p = new TrafficLights(); 155 JFrame frame = new JFrame("交通灯"); 156 frame.getContentPane().add(p); 157 frame.setSize(winWidth, winHeight); 158 frame.setLocation(500, 200); 159 frame.setVisible(true); 160 frame.setResizable(false); 161 ((TrafficLights) p).changeLights(5, 5, 5); 162 } 163 }