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

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd