📄 linearballet.java
字号:
import java.awt.*;
import java.applet.*;
// The basic idea is that we have two points bouncing around
// the screen and we draw a line between. But, we want a trail of lines,
// so we keep the last numLines end points. Pretty simple idea,
// but gives cool results.
public class LinearBallet extends java.applet.Applet implements Runnable
{
int numLines = 20;
Image im;
Graphics offscreen;
// We keep track of the last numLines endpoints of the
// lines we're drawing.
// Okay, should have used Point here...
int ToPoints[][] = new int[numLines][2];
int FromPoints[][] = new int[numLines][2];
int ToVelocity[] = new int[2];
int FromVelocity[] = new int[2];
Thread mainThread = null;
int width = 400;
int height = 200;
// You can tweek with these parameters as desired...
double randomChangeVelocity = 9.0;
int randomChangeVelocityModifier = 5;
double initialVelocityRange = 7.0;
int maxVelocity = 10;
public void init() {
int i;
resize(width, height);
try {
im = createImage(width, height);
offscreen = im.getGraphics();
} catch (Exception e) {
offscreen = null;
}
// Initial velocity is random, start point is fixed and the
// history of end points is filled using the velocity.
ToVelocity[0] = (int) (Math.random() * initialVelocityRange) + 2;
ToVelocity[1] = (int) (Math.random() * initialVelocityRange) + 2;
FromVelocity[0] = (int) (Math.random() * initialVelocityRange) + 2;
FromVelocity[1] = (int) (Math.random() * initialVelocityRange) + 2;
ToPoints[0][0] = 10;
ToPoints[0][1] = 10;
FromPoints[0][0] = 80;
FromPoints[0][1] = 10;
for (i=1; i<numLines; i++) {
ToPoints[i][0] = ToPoints[i-1][0] + ToVelocity[0];
ToPoints[i][1] = ToPoints[i-1][1] + ToVelocity[1];
FromPoints[i][0] = FromPoints[i-1][0] + FromVelocity[0];
FromPoints[i][1] = FromPoints[i-1][1] + FromVelocity[1];
}
}
public void paintApplet (Graphics g) {
int i;
// Erase the oldest line
g.setColor(Color.white);
g.drawLine(ToPoints[0][0], ToPoints[0][1], FromPoints[0][0],
FromPoints[0][1]);
g.setColor(Color.blue);
// All the lines are one iteration older now. Copy them down
// the array (okay, should have used a circular array here,
// but I'm not going to change it, since we couldn't care less
// about efficiency).
for (i=0; i<numLines-1; i++) {
ToPoints[i][0] = ToPoints[i+1][0];
ToPoints[i][1] = ToPoints[i+1][1];
FromPoints[i][0] = FromPoints[i+1][0];
FromPoints[i][1] = FromPoints[i+1][1];
}
// Move our current end point (the last one in the array).
// All the nasty if statements afterward are checking if
// we've gone off the end of the window, in which case we
// bounce off by inverting the velocity (and adding in
// a random compotent).
ToPoints[numLines-1][0] += ToVelocity[0];
ToPoints[numLines-1][1] += ToVelocity[1];
FromPoints[numLines-1][0] += FromVelocity[0];
FromPoints[numLines-1][1] += FromVelocity[1];
if (ToPoints[numLines-1][0]<0) {
ToPoints[numLines-1][0]=0;
ToVelocity[0] = -ToVelocity[0] + ((int) (Math.random() *
randomChangeVelocity)) - randomChangeVelocityModifier;
}
else if (ToPoints[numLines-1][0]>width-1) {
ToPoints[numLines-1][0]=width-1;
ToVelocity[0] = -ToVelocity[0] + ((int) (Math.random() *
randomChangeVelocity)) - randomChangeVelocityModifier;
}
if (ToPoints[numLines-1][1]<0) {
ToPoints[numLines-1][1]=0;
ToVelocity[1] = -ToVelocity[1] + ((int) (Math.random() *
randomChangeVelocity)) - randomChangeVelocityModifier;
}
else if (ToPoints[numLines-1][1]>height-1) {
ToPoints[numLines-1][1]=height-1;
ToVelocity[1] = -ToVelocity[1] + ((int) (Math.random() *
randomChangeVelocity)) - randomChangeVelocityModifier;
}
if (FromPoints[numLines-1][0]<0) {
FromPoints[numLines-1][0]=0;
FromVelocity[0] = -FromVelocity[0] + ((int) (Math.random() *
randomChangeVelocity)) - randomChangeVelocityModifier;
}
else if (FromPoints[numLines-1][0]>width-1) {
FromPoints[numLines-1][0]=width-1;
FromVelocity[0] = -FromVelocity[0] + ((int) (Math.random() *
randomChangeVelocity)) - randomChangeVelocityModifier;
}
if (FromPoints[numLines-1][1]<0) {
FromPoints[numLines-1][1]=0;
FromVelocity[1] = -FromVelocity[1] + ((int) (Math.random() *
randomChangeVelocity)) - randomChangeVelocityModifier;
}
else if (FromPoints[numLines-1][1]>height-1) {
FromPoints[numLines-1][1]=height-1;
FromVelocity[1] = -FromVelocity[1] + ((int) (Math.random() *
randomChangeVelocity)) - randomChangeVelocityModifier;
}
// Silly hack I'd rather not talk about...
while (ToVelocity[0]>=-1 && ToVelocity[0] <=1) {
ToVelocity[0] += ((int) (Math.random() * randomChangeVelocity)) -
randomChangeVelocityModifier;
}
while (ToVelocity[1]>=-1 && ToVelocity[1] <=1) {
ToVelocity[1] += ((int) (Math.random() * randomChangeVelocity)) -
randomChangeVelocityModifier;
}
while (FromVelocity[0]>=-1 && FromVelocity[0] <=1) {
FromVelocity[0] += ((int) (Math.random() * randomChangeVelocity)) -
randomChangeVelocityModifier;
}
while (FromVelocity[1]>=-1 && FromVelocity[1] <=1) {
FromVelocity[1] += ((int) (Math.random() * randomChangeVelocity)) -
randomChangeVelocityModifier;
}
if (ToVelocity[0]>maxVelocity)
ToVelocity[0] = maxVelocity;
else if (ToVelocity[0]<-maxVelocity)
ToVelocity[0] = -maxVelocity;
if (ToVelocity[1]>maxVelocity)
ToVelocity[1] = maxVelocity;
else if (ToVelocity[1]<-maxVelocity)
ToVelocity[1] = -maxVelocity;
if (FromVelocity[0]>maxVelocity)
FromVelocity[0] = maxVelocity;
else if (FromVelocity[0]<-maxVelocity)
FromVelocity[0] = -maxVelocity;
if (FromVelocity[1]>maxVelocity)
FromVelocity[1] = maxVelocity;
else if (FromVelocity[1]<-maxVelocity)
FromVelocity[1] = -maxVelocity;
// Draw the newest line (the older ones are already up).
g.drawLine(ToPoints[numLines-1][0], ToPoints[numLines-1][1],
FromPoints[numLines-1][0], FromPoints[numLines-1][1]);
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
if (offscreen != null) {
paintApplet(offscreen);
g.drawImage(im, 0, 0, this);
}
else {
paintApplet(g);
}
}
public void start() {
if (mainThread == null) {
mainThread = new Thread(this);
mainThread.start();
}
}
public void stop() {
mainThread.stop();
mainThread = null;
}
public void run() {
// Ooo! Multithreaded!
while (mainThread!=null) {
try {mainThread.sleep(40);}
catch (InterruptedException e) {}
repaint();
}
mainThread = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -