package lab.games; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JTextArea; /** * An ASCIImation has a sequence of frames. * * @author Franck van Breugel */ public class ASCIImation { private boolean repeat; private List frames; //@ invariant this.frames != null /** * Initializes this ASCIImation from the file with the given name. * The file contains one of more frames, each followed by an empty line. * Each frame consists of 5 lines of ASCII characters and each line * contains at most 80 characters. * * @param fileName the name of the file with the content of this ASCIImation. * @pre. the file is in the right format. * @throws FileNotFoundException if the file with the given name cannot be found. */ public ASCIImation(String fileName) throws FileNotFoundException { this.setRepeat(false); this.setFrames(new ArrayList()); File file = new File(fileName); Scanner fileInput = new Scanner(file); StringBuffer frameContent = new StringBuffer(); int frameHeight = 0; while (fileInput.hasNextLine()) { String line = fileInput.nextLine(); if (frameHeight < Frame.HEIGHT) { frameContent.append(line); frameContent.append("\n"); frameHeight++; } else { Frame frame = new Frame(frameContent.toString()); this.getFrames().add(frame); frameContent = new StringBuffer(); frameHeight = 0; } } fileInput.close(); } /** * Returns the frames of this ASCIImation. * * @return the frames of this ASCIImation. */ private List getFrames() { return this.frames; } /** * Sets the frames of this ASCIImation to the given frames. * * @param frames the new frames of this ASCIImation. * @pre. frames != null */ private void setFrames(List frames) { this.frames = frames; } /** * Checks if repeat is set. * * @return true if repeat is set, false otherwise. */ private boolean isRepeat() { return this.repeat; } /** * Sets repeat to the given value. * * @param repeat the new value of repeat. */ private void setRepeat(boolean repeat) { this.repeat = repeat; } /** * Returns the number of frames of this ASCIImation. * * @return the number of frames of this ASCIImation. */ public int getNumberOfFrames() { int number; if (this.isRepeat()) { number = Integer.MAX_VALUE; } else { number = this.getFrames().size(); } return number; } /** * Returns the frame of this ASCIImation with the given index. * * @param index the index of the frame. * @pre. index >= 0 && index < number of frames * @return the frame of this ASCIImation with the given index. */ public Frame getFrame(int index) { return this.getFrames().get(index % this.getFrames().size()); } /** * Reverses the order of the frames of this ASCIImation. */ public void reverse() { Collections.reverse(this.getFrames()); } /** * Repeats the frames of this ASCIImation indefinitely. * * @throws UnsupportedOperationException if this optional method is not implemented. */ public void repeat() throws UnsupportedOperationException { this.setRepeat(true); } /** * Plays this ASCIImation. */ public void play() { JFrame frame = new JFrame("ASCIImation"); final int WIDTH = 1000; final int HEIGTH = 120; frame.setSize(WIDTH, HEIGTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea area = new JTextArea(Frame.HEIGHT, Frame.WIDTH); frame.add(area); frame.setVisible(true); final int DELAY = 500; try { for (int i = 0; i < this.getNumberOfFrames(); i++) { area.setText(this.getFrame(i).toString()); Thread.sleep(DELAY); } } catch (InterruptedException e) {} } }