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 Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避