import java.awt.*; import java.applet.*; import shape; import circle; import square; //animates three shape objects to form a heart in the end- what lab does this sound similar to? public class Centergrav extends Applet implements Runnable { public shape[] heartparts; //The three shapes in this array create the heart Thread anim; public int framecount, delay; public void init() { setSize(400, 400); setBackground(Color.black); heartparts = new shape[3]; heartparts[0] = new square(200, 200); heartparts[1] = new circle(166, 166); heartparts[2] = new circle(234, 166); framecount=0; delay=80; } public void paint(Graphics g) { g.setColor(Color.red); if (framecount<3) //means "if the heart hasn't settled yet" { g.clearRect(0,0,400,400); //clear the canvas because I don't use repaint in run, just paint for (int i=0; i<3; i++) { heartparts[i].paint(g); heartparts[i].changevelocity(); heartparts[i].changeposition(); } if (heartparts[0].isStopped() && heartparts[1].isStopped() && heartparts[2].isStopped()) framecount++; //I have a few frames (3) once they're stopped to make sure they're in the right place } else //else if the heart has settled, framecount counts the letters being drawn { delay=600; //now that I'm just intermittantly painting letters, no reason to have a short delay g.setFont(new Font("Serif", Font.BOLD, 80)); for (int i=0; i<3; i++) heartparts[i].paint(g); //repaint the heart String message="I still"; String printable; if (framecount<10) printable=message.substring(0, framecount-3); //print as much of the string as I'm printing so far else printable=message; g.drawString(printable, 100, 90); if (framecount>11) //if I'm done printing the words at the top, start on the words on the bottom { message="love you"; if (framecount<19) printable=message.substring(0, framecount-11); else printable=message; g.drawString(printable, 60, 350); if (framecount>19) stop(); } framecount++; } } public void start() { anim = new Thread(this); anim.start(); //run(); } public synchronized void stop() { anim = null; notify(); } public synchronized void run() { while (Thread.currentThread()==anim) { paint(this.getGraphics()); //I paint without clearing so that it will go smoother when I'm printing the letters (the rest of it won't flicker) try { wait(delay); } catch (InterruptedException e) { return; } } } }