teaching machines

CS 145 Lecture 24 – Adding Gravity

May 2, 2012 by . Filed under cs145, lectures, spring 2012.

Agenda

Code

DroppingCircles.java

package prefinal;

import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;

public class DroppingCircles {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Drop Circles");
    
//    frame.add(new JButton("Click me!"));
//    frame.add(new JLabel("Read me!"));
    final DroppingCirclesCanvas canvas = new DroppingCirclesCanvas();
    frame.add(canvas);
    
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        canvas.repaint();
      }
    }, 0, 33);
    
    frame.setSize(512, 512);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Sphere.java

package prefinal;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Sphere {
  public static final int RADIUS = 25;
  
  private int centerX;
  private int centerY;
  private Color color;
  private Stopwatch timer;

  public Sphere(int x,
                int y) {
    centerX = x;
    centerY = y;
    
    Color[] colors = {
      Color.PINK,
      Color.GREEN,
      Color.DARK_GRAY,
      Color.CYAN,
      Color.BLUE,
      Color.RED,
      Color.MAGENTA,
      Color.ORANGE,
      Color.YELLOW,
    };
    
    Random generator = new Random();
    int i = generator.nextInt(colors.length);
    color = colors[i];
    
    timer = new Stopwatch();
    timer.start();
  }

  public void draw(Graphics g) {
    g.setColor(color);
    g.fillOval(centerX - RADIUS, (int) (getY() - RADIUS), 2 * RADIUS, 2 * RADIUS);
  }
  
  public double getY() {
    double t = timer.peekAtElapsedSeconds();
    return 4.9 * t * t + centerY;
  }
}

Stopwatch.java

package prefinal;

import java.util.Scanner;

public class Stopwatch {
  private long startTime;
  private long endTime;
  
  private boolean hasStarted;
  private double seconds;
  
  public Stopwatch() {
    seconds = endTime - startTime;
    hasStarted = false;
    seconds = 0.0;
  }

  public void start() {
    startTime = System.currentTimeMillis();
    hasStarted = true;
  }
  
  public void stop() {
    endTime = System.currentTimeMillis();
    hasStarted = false;
  }

  /**
   * Resets the stopwatch. If running, it continues running.
   */
  public void reset() {
    startTime = endTime = System.currentTimeMillis();
    
//    endTime = System.currentTimeMillis();
//    startTime = endTime;
    
    seconds = 0.0;
  }
  
  public double getElapsedSeconds() {
    seconds = (endTime - startTime) / 1000.0;
    return seconds;
  }
  
  public double peekAtElapsedSeconds() {
    seconds = (System.currentTimeMillis() - startTime) / 1000.0;
    return seconds;
  }
  
  public static void main(String[] args) {
//    System.out.println(System.currentTimeMillis());
    
    Stopwatch ticker = new Stopwatch();
    
    String zyx = "zyxwvutsrqponmlkjihgfedcba";
    Scanner in = new Scanner(System.in);
    System.out.print("Hit Enter when ready");
    in.nextLine();
    
    ticker.start();
    String line = in.nextLine();
    ticker.stop();

    boolean isMatch = zyx.equals(line);
    
    if (isMatch) {
      System.out.println(ticker.getElapsedSeconds());
    } else {
      System.out.println("Sorry.");
    }
  }
}

DroppingCirclesCanvas.java

package prefinal;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;

public class DroppingCirclesCanvas extends JPanel implements MouseListener, MouseMotionListener {
  private Sphere[] spheres;
  private int nSpheres;
  
  public DroppingCirclesCanvas() {
    addMouseListener(this);
    addMouseMotionListener(this);
    
    spheres = new Sphere[1000];
    nSpheres = 0;
  }
  
  protected void paintComponent(Graphics g) {
    super.paintComponents(g);

    for (int i = 0; i < nSpheres; ++i) {
      spheres[i].draw(g);
    }
  }

  public void mouseClicked(MouseEvent e) {
    spheres[nSpheres] = new Sphere(e.getX(), e.getY());
    ++nSpheres;
    repaint();
  }

  public void mouseEntered(MouseEvent arg0) {
  }

  public void mouseExited(MouseEvent arg0) {
  }

  public void mousePressed(MouseEvent arg0) {
  }

  public void mouseReleased(MouseEvent arg0) {
  }

  @Override
  public void mouseDragged(MouseEvent e) {
    spheres[nSpheres] = new Sphere(e.getX(), e.getY());
    ++nSpheres;
    repaint();
  }

  @Override
  public void mouseMoved(MouseEvent e) {
  }
}

Haiku

Children are the best.
But programs come in second.
I love making things.