📄 lotsofcircles.java
字号:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import javax.swing.*;
public class LotsOfCircles
extends JPanel
implements Runnable
{
private Image offImage = null;
private Graphics offGraphics = null;
private Thread thread = null;
private int counter = 0;
private int width = 0, height = 0;
public LotsOfCircles()
{
super();
setDoubleBuffered(false);
}
public void run()
{
Thread currentThread = Thread.currentThread();
while ( (thread == currentThread) && (counter < 10000))
{
offPaint(offGraphics);
counter++;
if ( (counter % 100) == 0)
{
repaint();
}
thread.yield();
}
}
public void offPaint(Graphics g)
{
Color color = new Color( (int) (255 * Math.random()),
(int) (255 * Math.random()),
(int) (255 * Math.random()));
int x = (int) (getSize().width * Math.random());
int y = (int) (getSize().height * Math.random());
int r = 10;
g.setColor(color);
g.fillOval(x - r, y - r, 2 * r, 2 * r);
}
public void initOffImage()
{
thread = null;
width = getSize().width;
height = getSize().height;
offImage = createImage(width, height);
offGraphics = offImage.getGraphics();
counter = 0;
thread = new Thread(this);
thread.start();
}
public void paintComponent(Graphics g)
{
if ( (width != getSize().width) || (height != getSize().height))
{
initOffImage();
}
if (offImage != null)
{
g.drawImage(offImage, 0, 0, null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -