//CaughtUp Applet, version 1 Last updated 2/27/99 //M.Dubson, Feb. 1999, ENVD 3252 import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.lang.*; public class CaughtUp extends Applet implements MouseListener, Runnable { int nbrSpaces; //number of pixels between gridlines int maxGridlines; //Maximum number of gridlines = array size int increment; //increment of nbrSpaces int w, h; // width and height of applet frame int radius; Thread theThread; Image offscreen; int[][] xyGreen; //xy-coords of green circle public void init() { addMouseListener(this); w = getSize().width; h = getSize().height; //nbrSpaces = 30; nbrSpaces = Integer.parseInt( getParameter( "nbrSpaces")); increment = 1; maxGridlines = w/nbrSpaces+2; xyGreen = new int[maxGridlines][maxGridlines]; radius = nbrSpaces/3; System.out.println("Variable nbrSpaces is " + nbrSpaces + "."); for (int xline = 0; xline < maxGridlines; xline++) { for (int yline = 0; yline < maxGridlines; yline++) { xyGreen[xline][yline] = -1; //-1 means circle not green } } } public void step() { nbrSpaces += increment; radius = nbrSpaces/3; if (nbrSpaces > 150 || nbrSpaces < 30) //Breath in ... breath out { increment *= -1; } //System.out.println("Spacing is " + nbrSpaces + "."); repaint(); }//end of step method public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,w,h); for (int x = 0; x < w; x += nbrSpaces) //Draw vertical lines { int blueValue = x*255/w; g.setColor(new Color(0,0,blueValue)); g.drawLine(x, 0, x, h); } for (int y = 0; y < h; y += nbrSpaces)//Draw horizontal lines { int redValue = y*255/h; g.setColor(new Color(redValue,0,0)); g.drawLine(0, y, w, y); } for (int x = 0; x < w+nbrSpaces; x += nbrSpaces) //Draw disks at intersections { //int radius = nbrSpaces/3; int blueValue = x*255/(w+nbrSpaces); for (int y = 0; y < h+nbrSpaces; y += nbrSpaces) { int redValue = y*255/(h+nbrSpaces); g.setColor(new Color(redValue,0,blueValue)); g.fillOval(x - radius, y - radius, 2*radius, 2*radius); } }// end of disk drawing loops for (int xline = 0; xline <= w/nbrSpaces + 1; xline++) //Draw green disks { for (int yline = 0; yline <= h/nbrSpaces + 1; yline++) { if (xyGreen[xline][yline] == 1 ) { g.setColor(Color.green); int x = xline*nbrSpaces; int y = yline*nbrSpaces; g.fillOval(x - radius, y - radius, 2*radius, 2*radius); } } }// end of green disk drawing //System.out.println("xGreen[1] is " + xGreen[1] + ". "+"yGreen[1] is " + yGreen[1]); //System.out.println("xGreen[3] is " + xGreen[3] + ". "+"yGreen[3] is " + yGreen[3]); }//end of paint method public void mousePressed( MouseEvent e) { int x = e.getX(); int y = e.getY(); int x_near = ((x+nbrSpaces/2)/nbrSpaces)*nbrSpaces; int y_near = ((y+nbrSpaces/2)/nbrSpaces)*nbrSpaces; int xline = x_near/nbrSpaces; int yline = y_near/nbrSpaces; if (xyGreen[xline][yline] == -1) { xyGreen[xline][yline] = 1; } else xyGreen[xline][yline] = -1; Graphics g = getGraphics(); g.setColor(Color.green); g.fillOval(x_near - radius,y_near - radius, 2*radius, 2*radius); } public void update (Graphics g) { if (offscreen == null || offscreen.getWidth(this) != w || offscreen.getHeight(this) != h) { offscreen = createImage (w, h ); } Graphics g2 = offscreen.getGraphics(); paint(g2); g.drawImage(offscreen, 0, 0, this); }//end of update method public void mouseReleased( MouseEvent e){;} public void mouseClicked( MouseEvent e){;} public void mouseEntered( MouseEvent e){;} public void mouseExited( MouseEvent e){;} public void start() { if( theThread == null ){theThread = new Thread( this );} theThread.start(); } public void run() { while( true ) { step(); try { theThread.sleep( 100 ); } catch( Exception e ) {;} } }//end of run method public void stop() { if( theThread != null && theThread.isAlive() ) { theThread.stop(); } theThread = null; } } // end of class CaughtUp