导航:首页 > 编程语言 > java红绿灯

java红绿灯

发布时间:2022-12-09 03:01:13

java 线程实现一个红绿灯问题

关键是启动一个线程控制颜色。代码如下。

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheledExecutorService;
import java.util.concurrent.TimeUnit;

public class Signal extends Applet {
int width = 200, height = 240;
int w = 50, h = 50;
int x = (width - w) / 2, y1 = (height - h * 3) / 3, y2 = y1 + h, y3 = y2 + h;
Color c = Color.RED;

@Override
public void init() {
ScheledExecutorService exec = Executors.newScheledThreadPool(1);
exec.scheleAtFixedRate(new Runnable() {
@Override
public void run() {
if (c == Color.RED) {
c = Color.YELLOW;
} else if (c == Color.YELLOW) {
c = Color.GREEN;
} else if (c == Color.GREEN) {
c = Color.RED;
}
repaint();
}
}, 5, 5, TimeUnit.SECONDS);
}

@Override
public void paint(Graphics g) {
setBackground(Color.white);

// all gray
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x, y1, w, h);
g.fillOval(x, y2, w, h);
g.fillOval(x, y3, w, h);

if (c == Color.RED) {
g.setColor(Color.RED);
g.fillOval(x, y1, w, h);
} else if (c == Color.YELLOW) {
g.setColor(Color.YELLOW);
g.fillOval(x, y2, w, h);
} else if (c == Color.GREEN) {
g.setColor(Color.GREEN);
g.fillOval(x, y3, w, h);
}
}
}

Ⅱ 使用java多线程和图形编程,完成红绿灯模拟软件

boolean flag = true;
while(flag){
//绿灯显示

//sleep(3000);

//黄灯显示

sleep(3000);

//红灯显示

sleep(3000);
}

如果点击结束,退出while

Ⅲ Java红绿灯 求大神!!急啊

importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.FlowLayout;
importjava.awt.Graphics;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassWayextendsJPanel{
JButtonred;
publicWay(){
red=newJButton("换灯");
setBackground(Color.yellow);
setSize(newDimension(320,260));
setPreferredSize(newDimension(320,260));
JPanelbtnPanel=newJPanel();
btnPanel.setLayout(newFlowLayout());
red.setLayout(newFlowLayout());//将单选按钮加入按钮面板
btnPanel.add(red);
add(red);
}
privatevoidlightRed(Graphicsg){
g.setColor(Color.red);
g.fillOval(getWidth()/2,30,15,15);
g.setColor(Color.black);
g.fillOval(getWidth()/2,50,15,15);
g.fillOval(getWidth()/2,70,15,15);
}
privatevoidlightYellow(Graphicsg){
g.setColor(Color.yellow);
g.fillOval(getWidth()/2,50,15,15);
g.setColor(Color.black);
g.fillOval(getWidth()/2,30,15,15);
g.fillOval(getWidth()/2,70,15,15);
}
privatevoidlightGreen(Graphicsg){
g.setColor(Color.green);
g.fillOval(getWidth()/2,70,15,15);
g.setColor(Color.black);
g.fillOval(getWidth()/2,30,15,15);
g.fillOval(getWidth()/2,50,15,15);
}
protectedvoidpaintComponent(Graphicsg){
super.paintComponents(g);
Waya=newWay();
g.clearRect(0,0,getWidth(),getHeight());
g.drawRect(getWidth()/2,30,30,80);
red.addActionListener(newActionListener(){
intf1=0;
publicvoidactionPerformed(ActionEvente){
Graphicsg=getGraphics();
switch(f1){
case0:
a.lightRed(g);
break;
case1:
a.lightYellow(g);
break;
case2:
a.lightGreen(g);
break;
}
f1++;
if(f1>2)f1=0;
};
});
}
publicstaticvoidmain(String[]args){
JFramefr=newJFrame("邮件界面模拟");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击x结束程序
ContainercontentPane=fr.getContentPane();
//得到窗口内容面板
contentPane.add(newWay());
fr.pack();
fr.setVisible(true);//设置窗口可见
}
}
大致帮你改了下,不知道符合不符合你的要求,有问题请追问

Ⅳ 【急求】高手帮忙编写红绿灯的JAVA程序,有图形界面灯会跳的那种!

看一下这个行不行?
--------------------------------------------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class App extends JFrame {
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(107, 252);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout(0, 0));
MyColorPanel panel = new MyColorPanel();
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
new Thread(panel).start();
}
public static void main(String[] args) {
new App();
}
}
class MyColorPanel extends JPanel implements Runnable {
private Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
private int[] y = { 10, 70, 130 };
private int[] time = { 3, 1, 5 };
private int index = 2;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = 50;
int x = 30;
for (int i = 0; i < y.length; i++) {
g.fillOval(x, y[i], width, width);
}
g.setColor(colors[index]);
g.fillOval(x, y[index], width, width);
}
public void run() {
while (true) {
try {
Thread.sleep(time[index] * 1000);
index--;
repaint();
if (index < 0) {
index = 2;
}
} catch (Exception e) {
}
}
}
}

Ⅳ 求一个简易的JAVA程序---红绿灯,有两个部分,用下拉表控制灯的颜色,选择红,画布中的灯就变红的那种

按照你的要求,写出的程序如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TrafficLight {
JFrame jf=new JFrame("Traffic Light");
JPanel jp=new JPanel();
JComboBox jcb=new JComboBox();
public TrafficLight(){
jcb.addItem("红灯");
jcb.addItem("黄灯");
jcb.addItem("绿灯");
jcb.addItemListener(new JComboBoxListener());
jf.add(jcb,BorderLayout.NORTH);
jf.add(jp,BorderLayout.CENTER);

jf.setSize(400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TrafficLight();
}
class JComboBoxListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent ie) {
Graphics g=jf.getGraphics();
if(ie.getItem().equals("红灯")){
g.setColor(Color.RED);
g.fillOval(200, 200, 100, 100);
}else if(ie.getItem().equals("黄灯")){
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 100, 100);
}else if (ie.getItem().equals("绿灯")){
g.setColor(Color.GREEN);
g.fillOval(200, 200, 100, 100);
}
}
}
}

Ⅵ java 红绿灯 代码

给程序一个变量tag 用来记录按键次数~~

tag%2==0时为绿
tag%2==1时为红
tag%2==2时为黄

Ⅶ 请帮我看下这个JAVA的程序(简易红绿灯)

发现你的程序窗体大小每改变一次,都会加上三个RadioButtons。

Ⅷ 用java编写交通信号灯

按照你的要求编写的红绿灯程序,你看看吧,比较简单。
完整的程序如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
public class TrafficLight extends JFrame{
JRadioButton jrbYellow,jrbGreen,jrbRed;
int flag=0;
jpNewPanel jpNewPanel;
public static void main(String[] args){
TrafficLight frame=new TrafficLight();
frame.setSize(500,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("TrafficLight");
frame.setVisible(true);
}
public TrafficLight(){
jpNewPanel=new jpNewPanel();
add(jpNewPanel,BorderLayout.CENTER);
JPanel jpRadioButtons=new JPanel();
jpRadioButtons.setLayout(new GridLayout(1,3));
jpRadioButtons.add(jrbYellow=new JRadioButton("Yellow"));
jpRadioButtons.add(jrbGreen=new JRadioButton("Green"));
jpRadioButtons.add(jrbRed=new JRadioButton("Red"));
add(jpRadioButtons,BorderLayout.SOUTH);
ButtonGroup group=new ButtonGroup();
group.add(jrbYellow);
group.add(jrbGreen);
group.add(jrbRed);

jrbYellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=2;
jpNewPanel.repaint();
}
});
jrbGreen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=1;
jpNewPanel.repaint();
}
});
jrbRed.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=3;
jpNewPanel.repaint();
}
});
}
class jpNewPanel extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(0,0,40,100);
g.drawOval(10,10,20,20);
g.drawOval(10,40,20,20);
g.drawOval(10,70,20,20);
if(flag==1){
g.setColor(Color.GREEN);
g.fillOval(10, 70, 20, 20);
}
else if(flag==2){
g.setColor(Color.YELLOW);
g.fillOval(10, 40, 20, 20);
}
else if(flag==3){
g.setColor(Color.RED);
g.fillOval(10, 10, 20, 20);
}
}
}
}

Ⅸ java 红绿灯 Thread 要求 运行时 下边出现 红灯亮 黄灯暗 绿灯暗 文字 按照红黄绿顺序 循环出现10次即可!

如果是使用多线程来做分别有 红 \黄 \ 绿 三个线程,是无法控制输出的顺序的,多线程的概念里有CPU随机执行的.
使用单线程控制吧 用System.println.out() 打印还差不多
Code:
public class TrafficLightThread implements Runnable {

final static String RED = "红灯亮 ";
final static String YELLOW = "黄灯暗";
final static String GREEN = "绿灯暗";

public void run() {
for (int i = 10; i > 0; i--) {
System.out.println("---第"+i+"次---");
System.out.println(RED);
System.out.println(YELLOW);
System.out.println(GREEN);
}
}

public static void main(String[] args) {
new Thread(new TrafficLightThread()).start();
}
}

Ⅹ java控制红绿灯及模拟车辆运动

写两个程序分别模拟红绿灯和汽车:
1)红绿灯程序以报文形式通知汽车程序;
2)汽车程序需要用多线程来实现。
3)红绿灯程序用个循环,每隔n分钟红绿灯转换,同时通知汽车程序;

阅读全文

与java红绿灯相关的资料

热点内容
openldaplinux安装 浏览:23
java取月的最后一天 浏览:10
腾讯云服务器多久退款 浏览:949
微信广告植入系统源码 浏览:922
一年级语文上册pdf 浏览:315
好久不见app干什么用的 浏览:143
压缩包解压码对方可以更改吗 浏览:256
pdf电子书制作软件 浏览:888
数控三通编程 浏览:300
linux多终端 浏览:811
法律写作pdf 浏览:144
国货哪个品牌最好app 浏览:951
看哪个app给钱最多 浏览:178
编程靠经验吗 浏览:759
c教程pdf下载地址 浏览:573
制作视频哪个app有瘦脸功能 浏览:649
linux查看线程内存 浏览:509
命令行签名apk 浏览:92
网页照片旋转源码 浏览:842
QQ会员头像源码 浏览:263