📄 starfield2.java
字号:
// StarField2.java
import java.awt.*;
import java.applet.Applet;
public class StarField2 extends Applet implements Runnable
{
Thread t;
int counter;
Image buffer;
Graphics bkContext;
Color starColors [] = { Color.white, Color.darkGray, Color.lightGray,
Color.blue, Color.yellow };
public void init ()
{
buffer = createImage (getSize ().width, getSize ().height);
bkContext = buffer.getGraphics ();
}
public void start ()
{
if (t == null)
{
t = new Thread (this);
counter = 0;
t.start ();
}
}
public void stop ()
{
t = null;
}
public void run ()
{
Thread current = Thread.currentThread ();
while (t == current)
{
// Prepare the frame.
// When there are enough stars, redraw the background
// and erase visible stars.
if (counter == 0)
{
bkContext.setColor (Color.black);
bkContext.fillRect (0, 0, getSize ().width, getSize ().height);
}
// Set the next star's color and draw this star.
bkContext.setColor (starColors [rnd (starColors.length)]);
int x = rnd (getSize ().width);
int y = rnd (getSize ().height);
bkContext.drawLine (x, y, x, y);
// Draw frame
repaint (); // This method causes the update method to be called.
// Pause
try
{
Thread.sleep (50);
}
catch (InterruptedException e) {}
// Manipulate counter
if (++counter > 500)
counter = 0;
}
}
public void paint (Graphics g)
{
g.drawImage (buffer, 0, 0, this);
}
// Return a random integer from 0 through limit - 1.
int rnd (int limit)
{
return (int) (Math.random () * limit);
}
public void update (Graphics g)
{
paint (g);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -