//******************************************************************************** // Chart.java // // "FROG CHART"「かえるチャート」 // // (c) 1997 Maeda Mameo http://www.alles.or.jp/~mameo/ //******************************************************************************** import java.awt.*; import java.applet.Applet; public class Chart extends java.applet.Applet implements Runnable{ private Image imageOff; private Graphics gOff; private int theW, theH; private Thread theThread; private boolean paintLock, paintRequest; static final double PI = Math.PI; static final double PI2 = 2 * PI; static final int CHARTXNUM = 11, CHARTYNUM = 9; private Color[] chartColors = new Color[ CHARTXNUM * CHARTYNUM]; private int[] chartStatus = new int[ CHARTXNUM * CHARTYNUM]; private int colorR, colorG, colorB; private Color frogColor; private int frogR; static final int FROGNUM = 9; private int[] frogDirection; private int guideNum; private double magnification; //******************************************************************************** //******************************************************************************** public void init(){ int a, i, j, r, g, b; Dimension d; paintLock = paintRequest = false; guideNum = 0; a = 0; for( i = 0; i < CHARTYNUM; i++){ for( j = 0; j < CHARTXNUM; j++){ r = 255; g = 180 * ( CHARTYNUM * CHARTYNUM - i * i) / CHARTYNUM / CHARTYNUM; b = ( 180 - 180 * j * j / CHARTXNUM / CHARTXNUM) * ( CHARTYNUM * CHARTYNUM - i * i) / CHARTYNUM / CHARTYNUM; chartColors[a] = new Color( r, g, b); chartStatus[a] = 0; a++; } } frogDirection = new int[FROGNUM]; for( i = 0; i < FROGNUM; i++) frogDirection[i] = 0; setBackground( Color.white); d = size(); theW = d.width; theH = d.height; imageOff = createImage( theW, theH); gOff = imageOff.getGraphics(); } public void start(){ theThread = new Thread(this); theThread.start();} public void stop(){ theThread.stop(); theThread = null;} public void update( Graphics g){ paint( g);} public void paint( Graphics g){ if(paintLock) return; g.drawImage( imageOff, 0, 0, null); paintRequest = false; } public boolean mouseMove( Event evt, int x, int y){ if( x >= theW / 10 && x < theW * 9 / 10 && y >= theH / 10 && y < theH * 9 / 10 ){ chartStatus[ ( y - theH / 10) / ( theH * 8 / 10 / CHARTYNUM) * CHARTXNUM + ( x - theW / 10) / ( theW * 8 / 10 / CHARTXNUM) ] = 12; } return true; } public boolean mouseDown( Event evt, int x, int y){ int a, i, j; guideNum = ++guideNum % ( FROGNUM + 2); if( guideNum == 0){ a = 0; for( i = 0; i < CHARTYNUM; i++){ for( j = 0; j < CHARTXNUM; j++){ chartStatus[a] = 5 + (int)( 50 * Math.sqrt( ( j - CHARTXNUM / 2) * ( j - CHARTXNUM / 2) + ( i - CHARTYNUM / 2) * ( i - CHARTYNUM / 2) ) / ( CHARTXNUM * CHARTXNUM / 4 + CHARTYNUM * CHARTYNUM / 4 ) ); a++; } } } return true; } //******************************************************************************** //******************************************************************************** public void run(){ int i, j; int r, g, b; long t; while(true){ t = System.currentTimeMillis(); colorR = ( colorR + 3) % 400; colorG = ( colorG + 7) % 400; colorB = ( colorB + 11) % 400; if( colorR < 200) r = 20 + colorR; else r = 419 - colorR; if( colorG < 200) g = 56 + colorG; else g = 455 - colorG; if( colorB < 200) b = 20 + colorB; else b = 419 - colorB; frogColor = new Color( r, g, b); while(paintRequest) sleeper(1); paintLock = true; gOff.setColor( Color.white); gOff.fillRect( 0, 0, theW, theH); drawChart(); if( guideNum > 0){ gOff.setColor( Color.darkGray); gOff.drawOval( (int)(.02 * theW), (int)(.02 * theH), (int)(.96 * theW), (int)(.96 * theH) ); } drawFrog(); paintLock = false; paintRequest = true; repaint(); frogR = ++frogR % 60; while( System.currentTimeMillis() - t < 50) sleeper(1); } } private void drawChart(){ int a, i, j; int x, y, w, h; w = (int)( theW * 8 / 10 / CHARTXNUM) - 3; h = (int)( theH * 8 / 10 / CHARTYNUM) - 3; a = 0; for( i = 0; i < CHARTYNUM; i++){ y = (int)( theH / 10 + theH * i * 8 / 10 / CHARTYNUM); for( j = 0; j < CHARTXNUM; j++){ if( chartStatus[a] == 0){ x = (int)( theW / 10 + theW * j * 8 / 10 / CHARTXNUM); gOff.setColor( chartColors[a]); gOff.fillRect( x, y, w, h); } else chartStatus[a]--; a++; } } } private void drawFrog(){ int i, j; double a, b, x, y; a = ( FROGNUM + .4) * ( FROGNUM + .4) - ( FROGNUM + .4); for( i = 0; i < FROGNUM; i++){ j = i + 1; b = PI2 * (( frogR + ( j * j - j) * 60 / a) % 60) / 60; x = ( .5 + (.46 - .019 * i) * Math.sin(b)) * theW; y = ( .5 - (.46 - .019 * i) * Math.cos(b)) * theH; frogDirection[i] = ( frogDirection[i] + 59 - FROGNUM + i) % 60; magnification = j * .1; if( i > FROGNUM - guideNum){ gOff.setColor( Color.darkGray); gOff.drawOval( (int)( x - magnification * 56), (int)( y - magnification * 56), (int)( magnification * 112) + 1, (int)( magnification * 112) + 1 ); } drawOneFrog( x, y, frogColor, PI2 * frogDirection[i] / 60); } } private void drawOneFrog( double x, double y, Color c, double r){ double xL, xR, vx; double yL, yR, vy; vx = magnification * Math.cos(r); vy = magnification * Math.sin(r); x -= 16 * vx; y -= 16 * vy; xL = x + 28 * vx + 32 * vy; yL = y + 28 * vy - 32 * vx; xR = x + 28 * vx - 32 * vy; yR = y + 28 * vy + 32 * vx; gOff.setColor( Color.black); fillCircle( x, y, 40); fillCircle( xL, yL, 24); fillCircle( xR, yR, 24); gOff.setColor( c); fillCircle( x, y, 32); fillCircle( xL, yL, 16); fillCircle( xR, yR, 16); gOff.setColor( Color.white); fillCircle( xL, yL, 12); fillCircle( xR, yR, 12); gOff.setColor( Color.black); fillCircle( xL + 4 * vx - 6 * vy, yL + 4 * vy + 6 * vx, 7); fillCircle( xR + 4 * vx - 6 * vy, yR + 4 * vy + 6 * vx, 7); fillCircle( x - 5 * vx + 8 * vy, y - 5 * vy - 8 * vx, 3); fillCircle( x - 5 * vx - 8 * vy, y - 5 * vy + 8 * vx, 3); } private void fillCircle( double x, double y, int r){ gOff.fillOval( (int)( x - magnification * r), (int)( y - magnification * r), (int)( magnification * 2 * r) + 1, (int)( magnification * 2 * r) + 1 ); } private void sleeper( int t){ try theThread.sleep(t); catch( InterruptedException e);} }