teaching machines

CS 245 Lecture 3 – Callbacks a la Interfaces

January 28, 2014 by . Filed under cs245, lectures, spring 2014.

Agenda

TODO

Code

ImageDirectoryCursor.java

package lecture03;

import java.io.File;
import java.util.Arrays;

public class ImageDirectoryCursor {
  private int i;
  private File dir;
  private File[] images;
  
  public ImageDirectoryCursor(File dir) {
    this.dir = dir;
    images = dir.listFiles();
//    System.out.println(Arrays.toString(images));
    Arrays.sort(images);
    i = 0;
  }
  
  public int getCount() {
    return images.length;
  }
  
  public void next() {
    i = (i + 1) % images.length;
  }
  
  public void previous() {
    i = (i + images.length - 1) % images.length;
  }
  
  public File getCurrent() {
    return images[i];
  }
}

TestImageDirectoryCursor.java

package lecture03;

import java.io.File;
import org.junit.Assert;
import org.junit.Test;

public class TestImageDirectoryCursor {
  @Test
  public void testCount() {
    ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));
    Assert.assertEquals("Incorrect number of images", 3, cursor.getCount());
  }
  
  @Test
  public void testConstructor() {
    ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));
    Assert.assertEquals(new File("/Users/johnch/Desktop/imgs/a.jpg"), cursor.getCurrent());
  } 
  
  @Test
  public void testNext1() {
    ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));
    cursor.next();
    Assert.assertEquals(new File("/Users/johnch/Desktop/imgs/b.jpg"), cursor.getCurrent());
  } 
  
  @Test
  public void testNext2() {
    ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));
    cursor.next();
    cursor.next();
    Assert.assertEquals(new File("/Users/johnch/Desktop/imgs/c.jpg"), cursor.getCurrent());
  }
  
  @Test
  public void testNext3() {
    ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));
    cursor.next();
    cursor.next();
    cursor.next();
    Assert.assertEquals(new File("/Users/johnch/Desktop/imgs/a.jpg"), cursor.getCurrent());
  }
  
  @Test
  public void testPrevious1() {
    ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));
    cursor.previous();
    Assert.assertEquals(new File("/Users/johnch/Desktop/imgs/c.jpg"), cursor.getCurrent());
  } 
  
  @Test
  public void testPrevious2() {
    ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));
    cursor.previous();
    cursor.previous();
    Assert.assertEquals(new File("/Users/johnch/Desktop/imgs/b.jpg"), cursor.getCurrent());
  }
  
  @Test
  public void testPrevious3() {
    ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));
    cursor.previous();
    cursor.previous();
    cursor.previous();
    Assert.assertEquals(new File("/Users/johnch/Desktop/imgs/a.jpg"), cursor.getCurrent());
  }
}

NextListener.java

package lecture03;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class NextListener implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent event) {
    // Toolkit.getDefaultToolkit().beep();
    System.out.println("clickety click click");
  }
}

Clickture.java

package lecture03;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Clickture {
  public static void main(String[] args) throws IOException {
    JFrame frame = new JFrame("Clickture");

    final ImageDirectoryCursor cursor = new ImageDirectoryCursor(new File("/Users/johnch/Desktop/imgs"));

    BufferedImage image = ImageIO.read(new File("/Users/johnch/Desktop/imgs/a.jpg"));
    final JLabel imageLabel = new JLabel(new ImageIcon(image));
    frame.add(imageLabel, BorderLayout.CENTER);

    JButton leftButton = new JButton("<");
    frame.add(leftButton, BorderLayout.WEST);
    leftButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent event) {
        try {
          cursor.previous();
          BufferedImage image = ImageIO.read(cursor.getCurrent());
          imageLabel.setIcon(new ImageIcon(image));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    });

    JButton rightButton = new JButton(">");
    frame.add(rightButton, BorderLayout.EAST);
    rightButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent event) {
        try {
          cursor.next();
          BufferedImage image = ImageIO.read(cursor.getCurrent());
          imageLabel.setIcon(new ImageIcon(image));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    });

    frame.setSize(1000, 700);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Haiku

Grandma left two things
Meatloaf recipe and code
I use both daily