# teaching machines

## CS 245 Lecture 7 – Abstract Classes

February 11, 2014 by . Filed under cs245, lectures, spring 2014.

### Agenda

• what ?s
• the inheritance spectrum
• a transformation hierarchy

### TODO

• Start homework 1. Due before February 24.

### Code

#### Transform.java

package lecture07;

import java.awt.Point;

public abstract class Transform {
protected double[][] matrix;

public Transform() {
matrix = new double[3][3];
}

public Point apply(Point in) {
int newX = (int) (matrix[0][0] * in.x + matrix[0][1] * in.y + matrix[0][2]);
int newY = (int) (matrix[1][0] * in.x + matrix[1][1] * in.y + matrix[2][2]);
return new Point(newX, newY);
}

//  public abstract void bloopity();
}

#### Translate.java

package lecture07;

public class Translate extends Transform {
public Translate(double dx, double dy) {
super();
matrix[0][0] = matrix[1][1] = matrix[2][2] = 1.0;
matrix[0][2] = dx;
matrix[1][2] = dy;
}
}

#### Scale.java

package lecture07;

public class Scale extends Transform {
public Scale(double sx, double sy) {
super();
matrix[0][0] = sx;
matrix[1][1] = sy;
matrix[2][2] = 1.0;
}
}

#### Rotate.java

package lecture07;

public class Rotate extends Transform {
super();
matrix[2][2] = 1.0;
}
}

#### DotMover.java

package lecture07;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DotMover extends JFrame implements MouseWheelListener, MouseListener, MouseMotionListener {
private ArrayList<Point> points;
private ArrayList<Transform> xforms;
private DotPanel panel;
private Point mouseLastAt;

public DotMover() {
super("DotMover");

points = new ArrayList<Point>();
for (int i = 0; i < 1000; i += 20) {
}
xforms = new ArrayList<Transform>();

panel = new DotPanel();

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1024, 600);
setVisible(true);

}

public class DotPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Random r = new Random(101);
for (Point p : points) {

for (Transform t : xforms) {
p = t.apply(p);
}

g.setColor(new Color(r.nextFloat(), r.nextFloat(), r.nextFloat()));
g.drawOval(p.x, p.y, 5, 5);
}
}
}

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

@Override
public void mouseWheelMoved(MouseWheelEvent event) {
//    xforms.add(new Scale(event.getWheelRotation() / 20.0, event.getWheelRotation() / 20.0));
panel.repaint();
}

@Override
public void mouseDragged(MouseEvent event) {
Point mouseNowAt = event.getPoint();
int diffY = mouseNowAt.y - mouseLastAt.y;
mouseLastAt = mouseNowAt;
panel.repaint();
}

@Override
public void mouseMoved(MouseEvent arg0) {
}

@Override
public void mouseClicked(MouseEvent arg0) {
}

@Override
public void mouseEntered(MouseEvent arg0) {
}

@Override
public void mouseExited(MouseEvent arg0) {
}

@Override
public void mousePressed(MouseEvent event) {
mouseLastAt = event.getPoint();
}

@Override
public void mouseReleased(MouseEvent arg0) {
}
}

### Haiku

Coding’s hardest part