博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【 Java 】 简易交通灯
阅读量:6977 次
发布时间:2019-06-27

本文共 6054 字,大约阅读时间需要 20 分钟。

起因:

很久之前就想做做这类的小程序练手,现在才开始动工哈哈哈。

从最简单的开始做起,这次是一个交通灯。

实现了交通灯的简单显示转换循环功能。

 

基于 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: paintComponent107      * 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 }

 

转载于:https://www.cnblogs.com/forfriendforfun/p/8383951.html

你可能感兴趣的文章
Android程序完全退出的三种方法
查看>>
融合应用11.1.8安装,一步一步的引导
查看>>
Spring mvc Data Redis—Pub/Sub(附Web项目源码)
查看>>
【Stage3D学习笔记续】山寨Starling(八):核心优化(批处理)的实现
查看>>
自定义数字格式字符串输出示例
查看>>
一步一步写算法(之图结构)
查看>>
JDBC实例--工具类升级,使用Apache DBCP连接池重构DBUtility,让连接数据库更有效,更安全...
查看>>
【Away3D代码解读】(四):主要模块简介
查看>>
struts2 select 默认选中
查看>>
ormlite 多表联合查询
查看>>
基于Sentinel的Redis3.2高可用方案
查看>>
.NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper
查看>>
用XCA(X Certificate and key management)可视化程序管理SSL 证书(3)--创建自己定义的凭证管理中心(Certificate Authority)...
查看>>
ASP.NET将Session保存到数据库中
查看>>
BeautifulSoup的成员结构
查看>>
Fckeditor PHP/ASP File Upload Vul
查看>>
Android中设置TextView的颜色setTextColor
查看>>
后端码农谈前端(CSS篇)第一课:CSS概述
查看>>
Excel
查看>>
HttpUnit学习笔记
查看>>