本文主要是介绍Java霓虹灯的循环显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一种方法:
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霓虹灯的循环显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!