?? laserpic.java
字號:
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
public class LaserPic extends Applet
{
private Image image;
private int x = 25,y = 25;
private Random random;
private int width,height,w,h,image_size,pixels[];
public void init()
{
random=new Random();
String imageName=getParameter("Image");
image=getImage(getDocumentBase(),imageName);
MediaTracker imageTracker=new MediaTracker(this);
imageTracker.addImage(image,0);
try
{
imageTracker.waitForID(0);
}
catch (InterruptedException e)
{
}
}
public void start()
{
width=getSize().width;
height=getSize().height;
w=image.getWidth(this);
h=image.getHeight(this);
x = (width - w)/2;
y = (height - h)/2;
image_size=w*h;
pixels=new int[image_size];
PixelGrabber pg=new PixelGrabber(image,0,0,w,h,pixels,0,w);
try
{
pg.grabPixels();
}
catch (InterruptedException e)
{
}
}
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0,0,getSize().width,getSize().height);
drawImage(g,image,x,y);
}
private void drawImage(Graphics g,Image image,int x,int y)
{
while(true)
{
g.setColor(Color.white);
g.fillRect(0,0,getSize().width,getSize().height);
try
{
int one_time=w;
int S_x=0,S_y=0;
S_x=(int)(random.nextFloat()*width);
S_y=(int)(random.nextFloat()*height);
Laser[] nextlot=new Laser[one_time];
int k=0,l=0;
int step=1,start=0;
float f=random.nextFloat();
step=(f<0.8)?34759:(f<0.9?1:image_size-1);
start=(int)(random.nextFloat()*image_size);
f=random.nextFloat();
start=(f<0.5)?image_size:0;
int sofar=0;
for (k=start;l<image_size;l++,k+=step)
{
Thread.sleep(2);
if (k<0) k+=image_size;
k%=image_size;
int row=k/w;
int col=k%w;
Color colr=new Color(pixels[k]);
int finishx=x+col;
int y1=y+row;
nextlot[sofar]=new Laser(colr,new Point(S_x,S_y),new Point(finishx,y1));
sofar++;
if (sofar==one_time)
{
Track(g,nextlot);
sofar=0;
}
}
}
catch (Exception e)
{
}
g.setPaintMode();
g.drawImage(image,x,y,this);
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
}
}
}
private synchronized void Track(Graphics g,Laser[] nextlot)
{
Color back=Color.white;
g.setXORMode(back);
for (int pass=0;pass<2;pass++)
{
for (int pixnr=0;pixnr<nextlot.length;pixnr++)
{
Laser p=nextlot[pixnr];
if (!close(p.c,back))
{
g.setColor(p.c);
g.drawLine(p.start.x,p.start.y,p.finish.x,p.finish.y);
}
if (pass==1)
{
g.setColor(p.c);
g.drawLine(p.finish.x,p.finish.y,p.finish.x,p.finish.y);
}
}
}
Thread.yield();
}
private boolean close(Color c1,Color c2)
{
return (Math.abs(c1.getRed()-c2.getRed()) + Math.abs(c1.getGreen()-c2.getGreen()) +
Math.abs(c1.getBlue()-c2.getBlue()))<0xff;
}
}
class Laser
{
public Color c;
public Point start,finish;
public Laser(Color c,Point start,Point finish)
{
this.c=c;
this.start=start;
this.finish=finish;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -