Java霓虹灯的循环显示
来源:互联网 发布:车载gps端口检测工具 编辑:程序博客网 时间:2024/04/29 16:44
第一种方法:
package com.lab2;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class NeonLight extends Canvas { //霓虹灯
private int x; //x坐标
private int y; //y坐标
private int flag;
private int radius; //半径
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public NeonLight(int x,int y,int radius,int flag){
this.x = x;
this.y = y;
this.radius = radius;
this.flag = flag;
this.setSize(radius,radius);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void paint(Graphics g){
if(flag == 0){
g.setColor(Color.red);
}
else if(flag == 1){
g.setColor(Color.blue);
}
else if(flag == 2){
g.setColor(Color.green);
}
else if(flag == 3){
g.setColor(Color.gray);
}
else if(flag == 4){
g.setColor( Color.pink);
}
else if(flag == 5){
g.setColor(Color.orange);
}
else if(flag == 6){
g.setColor(Color.magenta);
}
else if(flag == -1){
g.setColor( Color.black);
}
g.fillOval(0, 0, radius, radius);
}
}
package com.lab2;
import java.awt.Color;
import javax.swing.JApplet;
public class NeonLightTest extends JApplet implements Runnable{
private Thread t1,t2,t3,t4,t5,t6,t7;
private NeonLight neonLight[]; //霓虹灯
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
@Override
public void init() {
super.init();
neonLight = new NeonLight[7];
for(int i = 0;i<neonLight.length;i++){
neonLight[i] = new NeonLight(60,0,60,i);
neonLight[i].setLocation(60*(1+i), 0);
this.add(neonLight[i]);
}
this.setSize(1360,800);
repaint();
t1 = new Thread(this);
t1.setName("t1");
t1.start();
}
@Override
public void start() {
// TODO Auto-generated method stub
super.start();
}
@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
}
public synchronized void LoopShowNeonLight() { //循环显示霓虹灯
if(Thread.currentThread().getName().equals("t1")){ //表示点亮第二个灯
System.out.println("进入线程1");
int i = 0;
for(i = 0;i<1;i++){
neonLight[i].setFlag(1);
repaint();
}
for(i = 1;i<neonLight.length;i++){
neonLight[i].setFlag(-1);
repaint();
}
try {
this.setSize(1361,800);
Thread.sleep(500); //睡眠0.5秒
t2 = new Thread(this);
t2.setName("t2");
t2.start();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(Thread.currentThread().getName().equals("t2")){ //显示第三个灯
System.out.println("进入线程2");
int i = 0;
for(i = 0;i<1;i++){
neonLight[i].setFlag(-1);
repaint();
}
neonLight[i].setFlag(2);
repaint();
for(i = 2;i<neonLight.length;i++){
neonLight[i].setFlag(-1);
repaint();
}
try {
this.setSize(1362,800);
Thread.sleep(500); //睡眠0.5秒
t3 = new Thread(this);
t3.setName("t3");
t3.start();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(Thread.currentThread().getName().equals("t3")){
System.out.println("进入线程3");
int i = 0;
for(i = 0;i<2;i++){
neonLight[i].setFlag(-1);
repaint();
}
neonLight[i].setFlag(3);
repaint();
for(i = 3;i<neonLight.length;i++){
neonLight[i].setFlag(-1);
repaint();
}
try {
this.setSize(1363,800);
Thread.sleep(500); //睡眠0.5秒
t4 = new Thread(this);
t4.setName("t4");
t4.start();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(Thread.currentThread().getName().equals("t4")){
System.out.println("进入线程4");
int i = 0;
for(i = 0;i<3;i++){
neonLight[i].setFlag(-1);
repaint();
}
neonLight[i].setFlag(4);
repaint();
for(i = 4;i<neonLight.length;i++){
neonLight[i].setFlag(-1);
repaint();
}
try {
this.setSize(1364,800);
Thread.sleep(500); //睡眠0.5秒
t5 = new Thread(this);
t5.setName("t5");
t5.start();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(Thread.currentThread().getName().equals("t5")){
System.out.println("进入线程5");
int i = 0;
for(i = 0;i<4;i++){
neonLight[i].setFlag(-1);
repaint();
}
neonLight[i].setFlag(5);
repaint();
for(i = 5;i<neonLight.length;i++){
neonLight[i].setFlag(-1);
repaint();
}
try {
this.setSize(1359,800);
Thread.sleep(500); //睡眠0.5秒
t6 = new Thread(this);
t6.setName("t6");
t6.start();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(Thread.currentThread().getName().equals("t6")){
System.out.println("进入线程6");
int i = 0;
for(i = 0;i<5;i++){
neonLight[i].setFlag(-1);
repaint();
}
neonLight[i].setFlag(6);
repaint();
for(i = 6;i<neonLight.length;i++){
neonLight[i].setFlag(-1);
repaint();
}
try {
this.setSize(1358,800);
Thread.sleep(500); //睡眠0.5秒
t7 = new Thread(this);
t7.setName("t7");
t7.start();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(Thread.currentThread().getName().equals("t7")){
System.out.println("进入线程7");
int i = 0;
for(i = 0;i<6;i++){
neonLight[i].setFlag(-1);
repaint();
}
neonLight[i].setFlag(0);
repaint();
try {
this.setSize(1357,800);
Thread.sleep(500); //睡眠0.5秒
t1 = new Thread(this);
t1.setName("t1");
t1.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
LoopShowNeonLight();
}
}
第二种方法:
package pack3;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
public class Lamp extends Canvas implements Serializable{
int x;
int y;
int radius;
Color color;
public Lamp() {
for(int i=0; i<7; i++){
x=20;
y=20;
radius=20;
}
}
public Lamp(int x, int y, int radius, Color color) {
//super();
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.setSize(radius, radius);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void paint(Graphics g){
g.setColor(color);
g.fillOval(0, 0, radius, radius); //使用当前颜色填充外接指定矩形框的椭圆
System.out.println("Lamp:paint");
System.out.println("Lamp:paint:"+color.toString());
}
}
package pack3;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Display extends Applet implements Runnable{
Lamp lamp[]=new Lamp[7];
Thread t1,t2;
int i=0;
Color color[]={Color.red,Color.pink,Color.blue,Color.green,Color.yellow,Color.orange,Color.cyan};
Color color2;
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
System.out.println("destroy");
}
@Override
public void init() {
// TODO Auto-generated method stub
super.init();
t1 = new Thread(this);
for(int i=0; i<7; i++){
lamp[i] = new Lamp(20+20*i,20,15,Color.black);
this.add(lamp[i]);
}
this.setSize(300,200);
t1.start();
}
@Override
public void paint(Graphics g) {
System.out.println("paint");
}
@Override
public void start() {
// TODO Auto-generated method stub
super.start();
System.out.println("start");
}
@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
System.out.println("stop");
}
@Override
public void run() {
while (true) {
if(i>0){
lamp[i-1].setColor(Color.black); //将上一个圆设置回黑色
}
i=i%7;
lamp[i].setColor(color[i]);
i++;
repaint();
validate();
try {
this.setSize(300, 200);
Thread.sleep(500); //休眠500ms
this.setSize(300,201);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
- Java霓虹灯的循环显示
- Java 线程编码之霓虹灯的实现
- 布局--霓虹灯的实现
- 多彩的霓虹灯
- 多彩的霓虹灯
- 简单的霓虹灯效果
- 霓虹灯的字体
- 霓虹灯
- 霓虹灯
- 霓虹灯
- 霓虹灯
- 我收集的一些资源 II: 霓虹灯效果、截取指定区域的图片、ListView分页显示
- 简单的JQuery和js做出的霓虹灯(显示RGB值)
- 霓虹灯下失落的人群
- 循环显示的程序
- 循环显示上级目录(Java)
- 模拟霓虹灯滚动的c代码
- Android帧布局实现霓虹灯的效果
- struts处理中文乱码问题总结
- 第八次上机作业
- ZOJ 3168 Sort ZOJ7
- T4模版生成SpringMVC构造REST代码:第一篇 SpringMVC构造REST结构分析
- SI,DI
- Java霓虹灯的循环显示
- 网络安全热门资料 精品工具(转载)
- 【珍藏】 2012Android开发热门资料(110个)(转载)
- Qt堆栈窗口StackedLayout
- Codeforces Round #184 (Div. 2) D、E
- 我们要掌控什么——承认失败要趁早
- shell脚本echo的换行操作
- 实现二叉树
- 心情