paintframe.java

来自「面板中有36个大小相同的椭圆」· Java 代码 · 共 75 行

JAVA
75
字号
package 彩蛋;

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

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaintFrame extends JFrame {
	
	 private int eor , vor; 
	 JPanel panel;
	 int num= 6;
	
	public PaintFrame() {

		this.setSize(500, 335);
		this.setTitle("彩蛋程序");
		this.setVisible(true);
		this.setLocation(200,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		MyPanel p =  new MyPanel();
		this.getContentPane().add(p);

		
	}
	

	
	
	public class MyPanel extends JPanel {
		public MyPanel() {
			this.setSize(500, 300);
			eor = this.getWidth()/num;
			vor = this.getHeight()/num;	
			
			SleepThread s = new SleepThread();
			Thread t = new Thread(s);
			t.start();
		}
		public void paintComponent(Graphics g) {

			for(int i = 0;i<num;i++) {
				for(int j =0;j<num;j++) {
					Random r = new Random();
					Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
					g.setColor(c);
					g.fillOval(i*eor, j*vor, eor, vor);
				}
			}
		}		
	}
	
	public class SleepThread implements Runnable {

		public void run() {
			while(true) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}
				repaint();
				
			}
			
		}
		
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?