📄 selfref.java
字号:
package selfref;import java.awt.*;import java.awt.event.*;import java.applet.*;/** * Title: selfref * Description: * Copyright: Copyright (c) 2004 * Company: J & K Studio * @author Jake & Keats * @version 1.0 */public class selfref extends Applet implements Runnable{ Thread runner;//The thread for the animation MyCanvas canvas;//The canvas for the painting Button btnStart;//Start Button Button btnReset;//Reset button Button btnClockwise;//Clockwise rotation button Button btnCounter;//Counter-clockwise rotation button TextField txtDegree;//The angle value for rotation Button btnExample;//The Auto button for the illustration Button btnOff;//The light on/off button Scrollbar slbZoom;//The scroll bar of the zoom value TextField txtZoom;//The zoom value boolean running;//If the animation is running boolean first;//If this is the first time to run int Sleep=100;//The interval int width;//The width of the canvas int height;//The height of the canvas Color Back_Color;//The background of the canvas boolean isStandalone = false; /**Get a parameter value*/ int maxCollisionTimes;//The max-times of the lights collision int IncreaseHue;//The increase hue degree every time int ReduceHue;//Reduce hue degree float ReduceP;//Reduce hue degree possiblity double Resolution;//The resolution of the screen, i.e., one grid is for how many points //CSize ScreenSize; //boolean running; boolean LightOff;//The flag for light on/off double Matrix[];//the transform matrix float Offsety;//Offset float Offsetx; float Rotation;//The angle of rotation float myScale;//The scale value of zoom int Cd;//The middle var for the array long Hue[];//The main array long SingleHue;//Ligh's hue degree int MyHeight;//the actual pixel height int MyWidth; int Flag[];//a flag for redraw the backgroud of the pixels float setDegree; public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } /**Construct the applet*/ public selfref() { //Construct function Back_Color=Color.black;//background } /**Initialize the applet*/ public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { //Initial this.setLayout(new BorderLayout()); Panel pan= new Panel(); //Create a pannel pan.setLayout(new BorderLayout()); Panel pan1=new Panel(); Panel pan2=new Panel(); pan2.setLayout(new BorderLayout()); btnStart=new Button("Start"); //btnReset=new Button("Reset"); btnClockwise=new Button(">"); btnCounter=new Button("<"); txtDegree = new TextField(6); btnExample=new Button("Auto"); btnOff=new Button("Off"); slbZoom=new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,200); txtZoom=new TextField(6); txtDegree.addTextListener(new java.awt.event.TextListener() { public void textValueChanged(TextEvent e) { txtDegree_textValueChanged(e); } }); txtZoom.addTextListener(new java.awt.event.TextListener() { public void textValueChanged(TextEvent e) { txtZoom_textValueChanged(e); } }); pan1.add(btnStart); pan1.add(btnClockwise); pan1.add(btnCounter); pan1.add(txtDegree); pan1.add(btnExample); pan1.add(btnOff); this.add("South",pan); pan.add("North",pan1); pan2.add("Center",slbZoom); pan2.add("East",txtZoom); pan.add("South",pan2); //创建动画展示幕布,this为当前的动画程序实例传递给canvas动画幕布 canvas=new MyCanvas(this); this.add("Center",canvas); //把动画展示幕布放到程序屏幕上 } public int reinit(){ //srand( (unsigned)time( NULL ) ); MyWidth=350;//The size of the pixels MyHeight=350; SingleHue = 200; IncreaseHue = 200; ReduceHue=200; ReduceP=(float)(0.5); maxCollisionTimes=2000; Cd=0; myScale = (float)0.6; slbZoom.setValue((int)(myScale*100)); txtZoom.setText(Float.toString(myScale)); Rotation = (float)0.0; Offsetx = 0; Offsety = 0; Resolution=1; //Resolution=0.5; MyWidth/=Resolution; MyHeight/=Resolution; //ScreenSize=CSize(MyWidth*Resolution,MyHeight*Resolution); setDegree=(float)(Rotation*180/(3.1416)); txtDegree.setText(Float.toString(setDegree)); ///////////m_Rotation=Rotation*180/(3.1416); //UpdateData(false); constructMatrix(); Hue=new long[MyWidth*MyHeight*2]; Flag=new int[MyWidth*MyHeight]; for(int i=0;i<MyWidth;i++){ for(int j=0;j<MyHeight;j++){ Hue[Cd*MyWidth*MyHeight+i*MyHeight+j]=0; Hue[(1-Cd)*MyWidth*MyHeight+i*MyHeight+j]=0; Flag[i*MyHeight+j]=0; } } LightOff=false; running=false; //m_Base=false; return 0; } /**Start the applet*/ public void start() { //Start the animation thread if (runner == null){ runner= new Thread(this); runner.start(); running = false;} //getThe size of the canvas width=canvas.size().width; height=canvas.size().height; reinit(); } /**Stop the applet*/ public void stop() { if (runner!=null){ runner.stop(); runner=null; running = false; } } /**Destroy the applet*/ public void destroy() { if (runner!=null){ running = false; runner.stop(); runner=null; } } /**Get Applet information*/ public String getAppletInfo() { return "Applet Information"; } /**Get parameter info*/ public String[][] getParameterInfo() { return null; } /**Main method*/ public static void main(String[] args) { selfref applet = new selfref(); applet.isStandalone = true; Frame frame; frame = new Frame() { protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title) { super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(500,40520); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } public void run() { //The main process of every time while (true) { if(first){ reinit(); first=false; } if(running){ //all of actions processed in this function process(); } try{Thread.sleep(Sleep);} catch (InterruptedException e) { } }} public void process(){ if(!running)return; Graphics g=canvas.getGraphics(); if(!LightOff){ AddPoints(0); } int i, j; double ra=GenerateRandom(); if(ra*100<0.1){ Disturb(); } //loop for all the pixels on the screen for(i = 0;i<MyWidth;i++){ for(j = 0;j<MyHeight;j++){ long h = Hue[Cd*MyWidth*MyHeight+i*MyHeight+j]; if(h>255) h=255; if(Flag[i*MyHeight+j]==1){ Flag[i*MyHeight+j]=0; g.setColor(new Color((int)h,(int)h,(int)h)); g.fillRect((int)(i*Resolution),(int)(j*Resolution),1,1); } if(h==0){ continue; } Point pt; pt=Transform(new Point(i,j)); if(pt.x>=0&&pt.x<MyWidth&&pt.y>=0&&pt.y<MyHeight){ Hue[(1 - Cd)*MyWidth*MyHeight+pt.x*MyHeight+pt.y]+= h; Point pt0=new Point(); pt0.x=pt.x; pt0.y=pt.y; if(Hue[(1-Cd)*MyWidth*MyHeight+pt0.x*MyHeight+pt0.y]>255){ //collision boolean Pass=true; int times=0; while(Pass&×<maxCollisionTimes&&(Hue[(1-Cd)*MyWidth*MyHeight+pt0.x*MyHeight+pt0.y]+IncreaseHue)>255){ Point ptCollision=Collision(0); pt0.x=pt0.x+ptCollision.x; pt0.y=pt0.y+ptCollision.y; times++; if(pt0.x>0&&pt0.x<MyWidth&&pt0.y>0&&pt0.y<MyHeight){ Pass=true; }else{ Pass=false; } } if(Pass){ Hue[(1-Cd)*MyWidth*MyHeight+pt0.x*MyHeight+pt0.y]+=IncreaseHue; } Hue[(1-Cd)*MyWidth*MyHeight+pt.x*MyHeight+pt.y]=255; } } ra=GenerateRandom(); if(ra<ReduceP){ Hue[Cd*MyWidth*MyHeight+i*MyHeight+j]-=ReduceHue; if(Hue[Cd*MyWidth*MyHeight+i*MyHeight+j]<0){ Hue[Cd*MyWidth*MyHeight+i*MyHeight+j]=0; } Flag[i*MyHeight+j]=1; }else{ Hue[Cd*MyWidth*MyHeight+i*MyHeight+j]=0; Flag[i*MyHeight+j]=1; } g.setColor(new Color((int)h,(int)h,(int)h)); g.fillRect((int)(i*Resolution),(int)(j*Resolution),1,1); } } Cd = 1 - Cd; } public boolean action(Event evt, Object o) { if(evt.target==btnStart){ running=!running; if(!running){ btnStart.setLabel("Stop"); }else{ btnStart.setLabel("Start"); } return true; } else if(evt.target==btnCounter){ Rotation=(float)(Rotation-(Math.PI/180)); setDegree=(float)(Rotation*180/(Math.PI)); txtDegree.setText(Float.toString(setDegree)); constructMatrix(); } else if(evt.target==btnClockwise){ Rotation=(float)(Rotation+(Math.PI/180)); setDegree=(float)(Rotation*180/(Math.PI)); txtDegree.setText(Float.toString(setDegree)); constructMatrix(); } else if(evt.target==btnExample){ AddPoints(2); myScale=(float)1.12; //myScale=1.03; //Rotation=0.06; Rotation=(float)0.06; //Rotation=0.0; constructMatrix(); LightOff=true; running=true; setDegree=(float)(Rotation*180/(Math.PI)); txtDegree.setText(Float.toString(setDegree)); } else if(evt.target==btnOff){ LightOff=!LightOff; if(LightOff){ btnOff.setLabel("On"); }else{ btnOff.setLabel("Off"); } } return false; } public boolean handleEvent(Event evt) { switch (evt.id) { case Event.SCROLL_ABSOLUTE: case Event.SCROLL_LINE_DOWN: case Event.SCROLL_LINE_UP: case Event.SCROLL_PAGE_DOWN: case Event.SCROLL_PAGE_UP: myScale=(float)(slbZoom.getValue())/(float)100; constructMatrix(); txtZoom.setText(Float.toString(myScale)); } return(super.handleEvent(evt)); } public void paint (Graphics g) { g.setColor(Color.black); g.clearRect(0,0,MyWidth,MyHeight); } public void AddPoints(int type) { //add points of the light according different type int x,y; switch(type){ case 0: for(x = (int)(MyWidth / 2);x<=(int)(MyWidth / 2 + width/ 10);x++){ int p = x - (MyWidth / 2 + width / 20); p = p * p; float tmp =(float)Math.sqrt((width / 20) * (width / 20) - p); for(y = (int)(MyHeight / 3 - tmp);y<(int)(MyHeight / 3 + tmp);y+=1){ Hue[Cd*MyWidth*MyHeight+x*MyHeight+y] +=SingleHue; if(Hue[Cd*MyWidth*MyHeight+x*MyHeight+y]>255){ Hue[Cd*MyWidth*MyHeight+x*MyHeight+y]=255; } } } break; case 1: for(x = (int)(MyWidth / 2);x<=(int)(MyWidth / 2 + width / 10);x++){ for(y = (int)(MyHeight / 3-15);y<=(int)(MyHeight / 3+15);y+=1){ //int x=MyWidth/3; //int y=MyHeight/5; Hue[Cd*MyWidth*MyHeight+x*MyHeight+y] += SingleHue; if(Hue[Cd*MyWidth*MyHeight+x*MyHeight+y]>255){ Hue[Cd*MyWidth*MyHeight+x*MyHeight+y]=255; } } } break; case 2: int r=(int)(width / 20); for(x = (int)(MyWidth / 2-r);x<=(int)(MyWidth / 2+r);x++){ int p = x-MyWidth/2; p=p*p; float tmp = (float)Math.sqrt(r * r - p); for(y = (int)(MyHeight / 2 - tmp);y<=(int)(MyHeight / 2 + tmp);y+=1){ Hue[Cd*MyWidth*MyHeight+x*MyHeight+y] +=255; if(Hue[Cd*MyWidth*MyHeight+x*MyHeight+y]>255){ Hue[Cd*MyWidth*MyHeight+x*MyHeight+y]=255; } } } break; } } public Point Collision(int type) { //process to deal with collision Point out=new Point(0,0); int tmp; switch(type){ case 0: tmp=GenerateRandomInt(0,3); switch(tmp){ case 0: out.x+=1; break; case 1: out.y+=1; break; case 2: out.x-=1; break; case 3: out.y-=1; break; } break; case 1: tmp=GenerateRandomInt(0,1); switch(tmp){ case 0: out.x+=1; break; case 1: out.x+=1; break; case 2: out.x-=1; break; case 3: out.x-=1; break; } break; } return out; } public void constructMatrix() { //according the basic parameter to construct the transfor matrix Matrix=new double[3*3]; float centerptx = MyWidth / 2; float centerpty = MyHeight / 2; Matrix[0*3+0] = myScale * Math.cos(Rotation); Matrix[0*3+1] = myScale * Math.sin(Rotation); Matrix[0*3+2] = 0; Matrix[1*3+0] = -myScale * Math.sin(Rotation); Matrix[1*3+1] = myScale * Math.cos(Rotation); Matrix[1*3+2] = 0; Matrix[2*3+0] = -myScale * Math.cos(Rotation) * centerptx + myScale * centerpty * Math.sin(Rotation) + centerptx + Offsetx; Matrix[2*3+1] = -myScale * Math.sin(Rotation) * centerptx - myScale * centerpty * Math.cos(Rotation) + centerpty + Offsety; Matrix[2*3+2] = 1; } public void Disturb() { Rotation+=GenerateRandom()*0.02-0.01; Offsetx+=GenerateRandom()*0.2-0.1; Offsety+=GenerateRandom()*0.2-0.1; constructMatrix(); } public double GenerateRandom() { return (double)Math.random(); } public int GenerateRandomInt(int minValue, int maxValue) { int len=maxValue-minValue; if(len<0)len=-len; int tmp=(int)((len+1)*GenerateRandom())+minValue; return tmp; } public Point Transform(Point pt) { //perform transform int x1,y1; int x,y; x=pt.x; y=pt.y; x1 = (int)(Matrix[0*3+0] * (double)x + Matrix[1*3+0] * (double)y + Matrix[2*3+0]); y1 = (int)(Matrix[0*3+1] * (double)x + Matrix[1*3+1] * (double)y + Matrix[2*3+1]); return new Point(x1,y1); } void txtDegree_textValueChanged(TextEvent e) { setDegree=Float.valueOf(txtDegree.getText()).floatValue(); Rotation=(float)(setDegree*(Math.PI)/180); constructMatrix(); } void txtZoom_textValueChanged(TextEvent e) { String zoom=txtZoom.getText(); float z=Float.valueOf(zoom).floatValue(); if(z>0&&z<=2){ myScale=z; slbZoom.setValue((int)(myScale*100)); } }}class MyCanvas extends Canvas{ selfref local; public MyCanvas(selfref localcopy){ local=localcopy; setBackground(local.Back_Color); } public void paint(Graphics g) { //Deal with repaint g.setColor(local.Back_Color); g.fillRect(0,0,local.width,local.height); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -