欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

有趣的java彩蛋程序 博客分类: 有趣的程序 java彩蛋动态 

程序员文章站 2024-03-25 14:56:58
...

有趣的java彩蛋程序

帮别人做课程设计,无意中看到一道彩蛋程序的题。觉得挺有意思的,这也是我第一次接触到动态显示的程序,感觉很有趣,很乐意在这里跟大家分享一下。

题目的要求是从界面输入行数和列数后,按下确定按钮后,在界面正*出现一系列颜色不断变化闪烁的椭圆形彩蛋。

实现的思路也很简单,界面就不说了,彩蛋的绘制思路就是通过随机数使椭圆颜色的三个参数值不断变化,再通过不断重绘实现图形的“闪烁”。注意为了使程序灵活性更高,要把重绘放到一个线程中运行,防止整个窗体陷入死循环。

废话不多说了直接贴代码。

PS:我后来想了想,其实稍稍一改还可以做出颜色由浅到深类似呼吸灯的变化效果,或者同时实现彩蛋的位置不断移动,即完全的动画效果,大家可以一起试试!

 

主窗体:

package 彩蛋;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MyJFrame extends JFrame{
	public DrawComponent dc = new DrawComponent();

	public static void main(String[] args) {
		MyJFrame jmf= new MyJFrame();
		jmf.initUI();
	}

	private void initUI() {
		
		this.setSize(600,500);
		this.setDefaultCloseOperation(3);
		this.setTitle("我的彩蛋");
		JPanel jp = new JPanel();
		jp.setSize(50, 0);
		jp.setLayout(new FlowLayout());
		this.add(BorderLayout.SOUTH,jp);
		JLabel j1 = new JLabel("行数");
		JTextField jtf1 = new JTextField();
		jtf1.setPreferredSize(new Dimension(20,30));
		JLabel j2 = new JLabel("列数");
		JTextField jtf2 = new JTextField();
		jtf2.setPreferredSize(new Dimension(20,30));
		JButton jb = new JButton("确定");
		
		MyActionListener ma = new MyActionListener(dc,jtf1,jtf2);
		jb.addActionListener(ma);
		jp.add(j1);
		jp.add(jtf1);
		jp.add(j2);
		jp.add(jtf2);
		jp.add(jb);
		this.add(dc);
		
		this.setVisible(true);
		
	}
	
}

 

绘制彩蛋的组件

package 彩蛋;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JComponent;

public class DrawComponent extends JComponent {
	private int row =0,column = 0;
	
	//重绘方法
	public void paintComponent(Graphics g) {
		this.setPreferredSize(new Dimension(400,400));
		Random rand = new Random();
		for(int i = 0;i<this.row;i++){
			for(int j = 0;j<this.column;j++){
				int r1 = rand.nextInt(256);
				int g1 = rand.nextInt(256);
				int b1 = rand.nextInt(256);
				Color c = new Color(r1,g1,b1);
				g.setColor(c);
				g.fillOval(j*100+100, i*50+50, 100, 50);
			}
		}	
	}
	
	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getColumn() {
		return column;
	}

	public void setColumn(int column) {
		this.column = column;
	}

}

 监听器:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

public class MyActionListener implements ActionListener {

	public JTextField jtf1,jtf2;
	public DrawComponent dc;
	private MyThread m1;
	
	public MyActionListener(DrawComponent dc,JTextField jtf1, JTextField jtf2) {
		this.dc = dc;
		this.jtf1 = jtf1;
		this.jtf2 = jtf2;
	}

	public void actionPerformed(ActionEvent e) {
		//得到文本框的文本值
		int row = Integer.parseInt(this.jtf1.getText());
		int column = Integer.parseInt(this.jtf2.getText());
		
		//启动线程
		if(m1 == null){
			m1 = new MyThread(this.dc);
			m1.start();
		}
		this.dc.setColumn(column);
		this.dc.setRow(row);
	}

}

 

线程:

package 彩蛋;

class MyThread extends Thread{
    public DrawComponent dc;
    
	public MyThread(DrawComponent dc){
		this.dc = dc;
	}
	
	public void run(){
		while (true){
		this.dc.repaint();
		}
	}
}