teaching machines

CS 245 Lecture 2 – Graphical User Interfaces

September 5, 2013 by . Filed under cs245, fall 2013, lectures.

Agenda

TODO

Code

Eaushow.java

package lecture0905;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Eaushow {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Eaushow");
    
    JButton previousButton = new JButton("<");
    frame.add(previousButton, BorderLayout.WEST);
    
    JButton nextButton = new JButton(">");
    frame.add(nextButton, BorderLayout.EAST);
    
    JLabel imageLabel = new JLabel("INSERT PIC HERE");
    imageLabel.setHorizontalAlignment(JLabel.CENTER);
    frame.add(imageLabel, BorderLayout.CENTER);
    
    frame.setSize(700, 700);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

ImageReel.java

package lecture0905;

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

public class ImageReel {
  private int currentIndex;
  private File[] images;
//  private String pathToDirectory;

  public ImageReel(String pathToDirectory) {
//    this.pathToDirectory = pathToDirectory;
    File dir = new File(pathToDirectory);
    images = dir.listFiles();
    Arrays.sort(images);
    currentIndex = 0;
  }
  
  public File getCurrent() {
    return images[currentIndex];
  }
  
  public void advance() {
    ++currentIndex;
  }
  
  public void retreat() {
    --currentIndex;
  }
}

ImageReelTest.java

package lecture0905;

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

public class ImageReelTest {
  @Test
  public void testCtor() {
    ImageReel reel = new ImageReel("/home/johnch/Desktop/album");
    File actual = reel.getCurrent();
    Assert.assertEquals("reel not focused correctly", "a.jpg", actual.getName());
  }
  
  @Test
  public void testAdvance() {
    ImageReel reel = new ImageReel("/home/johnch/Desktop/album");
    reel.advance();
    File actual = reel.getCurrent();
    Assert.assertEquals("advance fails", "b.jpg", actual.getName());
  }
  
  @Test
  public void testRetreat() {
    ImageReel reel = new ImageReel("/home/johnch/Desktop/album");
    reel.advance();
    reel.advance();
    reel.advance();
    reel.retreat();
    File actual = reel.getCurrent();
    Assert.assertEquals("retreat fails", "c.jpg", actual.getName());
  }
  
  
  @Test
  public void testRetreatEarly() {
    ImageReel reel = new ImageReel("/home/johnch/Desktop/album");
    reel.retreat();
    File actual = reel.getCurrent();
    Assert.assertEquals("retreat fails", "e.jpg", actual.getName());
  }
}

Haiku

We used to fight work
But we built tools to help us
Now we fight our tools