Java霓虹灯的循环显示

2024-05-11 18:18
文章标签 java 显示 循环 霓虹灯

本文主要是介绍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霓虹灯的循环显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980290

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过