teaching machines

CS 491 Lecture 6 – Smoothing and Projectiles

September 18, 2015 by . Filed under fall 2015, gamedev2, lectures.

Agenda

TODO

Code

SmoothingDemo.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SmoothingDemo extends JFrame {
  private JPanel panel;
  private boolean isLeftDown;
  private boolean isRightDown;
  
  private double currentPressedness;
  
  private double[] pressednesses = new double[600];
  private int pi;
  private double weight = 1.0;
  
  private double rectangleX = 300;

  public SmoothingDemo() {
    final JSlider slider = new JSlider(0, 1000, 1000);
    slider.addChangeListener(new ChangeListener() {
      @Override
      public void stateChanged(ChangeEvent e) {
        weight = slider.getValue() / 1000.0;
        panel.requestFocus();
      }
    });
    add(slider, BorderLayout.NORTH);
    
    panel = new SmoothPanel();
    add(panel);
    setSize(600, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    panel.requestFocus();
  }

  private class SmoothPanel extends JPanel implements KeyListener {
    public SmoothPanel() {
      addKeyListener(this);
      
      Timer timer = new Timer(10, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          double previousPressedness = pressednesses[(pi + pressednesses.length - 1) % pressednesses.length];
          
          pressednesses[pi] = (1.0 - weight) * previousPressedness + weight * currentPressedness;
          rectangleX += pressednesses[pi] * 10;

          pi = (pi + 1) % pressednesses.length;
          panel.repaint();
        }
      });
      timer.setRepeats(true);
      timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      
      g.setColor(Color.MAGENTA);
      g.drawLine(pi, 0, pi, getHeight());
      
      g.setColor(Color.BLUE);
      for (int i = 0; i < pressednesses.length - 1; ++i) {
        g.drawLine(i, (int) (-0.4 * getHeight() * pressednesses[i] + 0.5 * getHeight()),
                   i + 1, (int) (-0.4 * getHeight() * pressednesses[i + 1] + 0.5 * getHeight()));
      }
      
      g.setColor(Color.YELLOW);
      g.fillRect((int) rectangleX, 200, 30, 30);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
      if (!isLeftDown && e.getKeyCode() == KeyEvent.VK_LEFT) {
        System.out.println("[left");
        isLeftDown = true;
        currentPressedness = -1;
      } else if (!isRightDown && e.getKeyCode() == KeyEvent.VK_RIGHT) {
        System.out.println("[right");
        isRightDown = true;
        currentPressedness = 1;
      }
    }

    @Override
    public void keyReleased(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        System.out.println("left]");
        isLeftDown = false;
        currentPressedness = 0;
      } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        System.out.println("right]");
        isRightDown = false;
        currentPressedness = 0;
      }
    }
  }

  public static void main(String[] args) {
    new SmoothingDemo();
  }
}

Haiku

The world changes fast
But it blends the present and past
What’s now is half-last