// // Author : Will Schenk (chilly@psu.edu) import java.awt.*; class Star { // 별의 위치와 유형에 대한 정보를 가진 클래스 int H, V; int x, y, z; int type; Star( int width, int height, int depth, int type ) { this.type = type; H = width/2; V = height/2; x = (int)(Math.random()*width) - H; // 난수 발생으로 별의 좌표를 구한다. y = (int)(Math.random()*height) - V; if ( (x == 0) && (y == 0 ) ) x = 10; z = (int) (Math.random()*depth); // z는 3차원(깊이)을 표현하는 변수이다. } public void Draw( Graphics g, double rot ) // StarField 클래스의 PaintMe() 도구에 의해 호출되며 { // 실제 별을 표현한다. double X, Y; int h, v, hh, vv; int d; z-=2; if ( z < -63 ) z = 100; hh = (x*64)/(64+z); vv = (y*64)/(64+z); X = (hh*Math.cos(rot))-(vv*Math.sin(rot)); // cos(), sin() 도구의 인자는 라디안 값이다. Y = (hh*Math.sin(rot))+(vv*Math.cos(rot)); h = (int)X+H; // 생성자에서 뺏던 H, V 값을 다시 더한다. v = (int)Y+V; // h는 가로 좌표이고 v는 세로 좌표이다. if ( (h < 0) || (h > (2*H))) z = 100; if ( (v < 0) || (v > (2*H))) z = 100; GrayMe(g); // 깊이를 표현하는 z 값에 따라 별의 색을 결정 if( type == 0 ) { // type이 0일 경우 작은 사각형으로 별을 표현 d=(100-z)/50; // 깊이를 표현하는 z 값에 따라 별의 크기를 결정 if( d == 0 ) d = 1; g.fillRect( h, v, d, d ); } else { // type이 0이 아닐 경우 십자 모양(네 개의 선)으로 별을 표현 d=(100-z)/20; // 깊이를 표현하는 z 값에 따라 별의 크기를 결정 g.drawLine( h-d, v, h+d, v ); g.drawLine( h, v-d, h, v+d ); if( z < 50 ) { d/=2; g.drawLine( h-d, v-d, h+d, v+d ); g.drawLine( h+d, v-d, h-d, v+d ); } } } public void GrayMe(Graphics g) { // 3차원(깊이)을 표현하는 변수인 z의 값에 따라 색깔을 다르게 표현한다. /* if( z > 75 ) { g.setColor( Color.darkGray ); // 어두운 회색은 배경색과 큰 차이가 없어 주석처리 } else */ if ( z > 50 ) { // 먼 것은 회색 g.setColor( Color.gray ); } else if ( z > 25 ) { // 중간 지대는 연한 회색 g.setColor( Color.lightGray ); } else { g.setColor( Color.white ); // 가까운 것은 흰색으로 표현. } } } public class StarField extends java.applet.Applet implements Runnable { // 애플릿 StarField, Runnable 인터페이스로부터 파생되기 때문에 run() 도구를 가진다. int Width, Height; Thread me = null; // 별의 위치를 이동시켜주는 쓰레드 boolean suspend = false; Image im; Graphics offscreen; // 버퍼 이미지를 저장하는 그래픽 디바이스이다. double rot, dx, ddx; // dx는 rot의 증가량이고 ddx는 dx의 증가량이다. int speed, stars, type; double defddx, max; // defddx는 ddx의 기본값이고 max는 dx의 최대값이다. Star pol[]; // Points of light, 별들의 위치 배열이다. public void init() // 애플릿을 실행하기 위해 브라우저가 가장 먼저 호출 { rot = 0; dx=0; ddx=0; Width = size().width; Height = size().height; String theSpeed = getParameter( "speed" ); Show( "speed", theSpeed ); // 표준 출력으로 HTML 변수 정보를 출력 speed = (theSpeed == null ) ? 50 : Integer.valueOf( theSpeed ).intValue(); String theStars = getParameter( "stars" ); Show( "stars", theStars ); stars = (theStars == null ) ? 30 : Integer.valueOf( theStars ).intValue(); String theType = getParameter( "type" ); Show( "type", theType ); type = (theType == null ) ? 0 : Integer.valueOf( theType ).intValue(); String theRot = getParameter( "spin" ); Show( "spin", theRot ); rot = (theRot == null) ? 0 : Double.valueOf( theRot ).doubleValue(); String theMax = getParameter( "maxspin" ); Show( "maxspin", theRot ); max = (theMax == null) ? .1 : Double.valueOf( theMax ).doubleValue(); String theddx = getParameter( "ddx" ); Show( "ddx", theddx ); defddx = (theddx == null) ? .005 : Double.valueOf( theddx ).doubleValue(); try { im = createImage( Width, Height ); // 화면에는 나타나지 않는 버퍼 이미지를 생성한다. // 애니메이션에 자주 사용되는 더블 버퍼링에 사용된다. offscreen = im.getGraphics(); // 버퍼 이미지의 그래픽 디바이스를 구해서 offscreen으로 한다. } catch( Exception e) { offscreen = null; } pol = new Star[stars]; // Star 인스턴스가 저장될 배열 생성 for ( int i = 0; i < stars; i++ ) pol[i] = new Star( Width, Height, 100, type ); // stars 개의 Star 인스턴스 생성 } public void paint( Graphics g ) { if( offscreen != null ) { // 버퍼 이미지가 있는 경우 paintMe( offscreen ); // 버퍼 이미지에 배경을 칠하고 별들을 새로 그린다. g.drawImage( im, 0, 0, this ); // 그려진 버퍼 이미지를 화면에 뿌린다. } else { // 버퍼 이미지가 null이면 현재 그래픽 디바이스로 배경을 칠하고 별들을 그린다. paintMe( g ); } } public void paintMe( Graphics g ) { g.setColor( Color.black ); // 현재 색깔을 검정으로 한다. g.fillRect( 0, 0, Width, Height ); // 애플릿 전체를 검게 칠한다. //g.setColor( Color.gray ); for( int i = 0; i < stars; i++ ) // 별들을 stars 개수만큼 그린다. pol[i].Draw( g, rot ); } public void start() // 웹 브라우저가 애플릿을 실행할 때 init() 다음으로 호출한다. { if ( me == null ) { // me 쓰레드는 별의 위치를 이동시키는 쓰레드이다. me = new Thread( this ); // run()을 실행하는 me 쓰레드를 생성한다. me.start(); // 생성된 me 쓰레드가 run()을 실행한다. } } public void stop() // 웹 브라우저에 의해 호출된다. { if( me != null ) { me.stop(); me = null; } } public void run() { while( me != null ) { rot += dx; dx += ddx; if( dx > max ) ddx=-defddx; // dx가 max보다 크면 ddx 값을 음으로 한다. if( dx < -max) ddx=defddx; // dx가 -max보다 작으면 ddx 값을 양으로 한다. try { Thread.sleep( speed ); } catch (InterruptedException e){} repaint(); // update() 호출 } } public void update( Graphics g ) { paint( g ); // 별들을 새로 그린다. } public boolean mouseDown( java.awt.Event evt, int x, int y ) { ddx = (ddx == 0) ? defddx : 0; // ddx가 0이었으면 defddx로, 0이 아니었으면 0으로 한다. return true; } public void Toggle( ) { if( me != null ) { if( suspend ) { me.resume(); // me 쓰레드 수행을 재개한다. } else { me.suspend(); // me 쓰레드 수행을 일단 멈춘다. } suspend = !suspend; // suspend 값을 토글한다. } } public void Show( String theString, String theValue ) // HTML의 변수와 값을 표준출력으로 출력 { if ( theValue == null ) { System.out.println( theString + " : null"); } else { System.out.println( theString + " : " + theValue ); } } }