AsciiAnim.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //AsciiAnim.java
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. public class AsciiAnim extends JFrame implements ActionListener{
  6. AsciiCanvas cnvAnim = new AsciiCanvas();
  7. JButton btnPrev = new JButton("prev");
  8. JButton btnNext = new JButton("next");
  9. JButton btnSave = new JButton("save");
  10. JButton btnLoad = new JButton("load");
  11. JButton btnAnim = new JButton("animate");
  12. JPanel pnlSouth = new JPanel();
  13. Timer timer = new Timer(100, this);
  14. boolean animating = false;
  15. public AsciiAnim(){
  16. this.setUpGUI();
  17. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. this.setVisible(true);
  19. this.setSize(640, 480);
  20. } // end constructor
  21. public void setUpGUI(){
  22. //like it says, set up GUI
  23. Container pnlMain = this.getContentPane();
  24. pnlSouth.setLayout(new FlowLayout());
  25. pnlMain.add(cnvAnim, BorderLayout.CENTER);
  26. pnlMain.add(pnlSouth, BorderLayout.SOUTH);
  27. pnlSouth.add(btnPrev);
  28. pnlSouth.add(btnNext);
  29. pnlSouth.add(btnAnim);
  30. pnlSouth.add(btnSave);
  31. pnlSouth.add(btnLoad);
  32. //add action listeners
  33. btnPrev.addActionListener(this);
  34. btnNext.addActionListener(this);
  35. btnAnim.addActionListener(this);
  36. btnSave.addActionListener(this);
  37. btnLoad.addActionListener(this);
  38. } // end setUpGUI
  39. public void actionPerformed(ActionEvent e){
  40. if (e.getSource() == btnPrev){
  41. cnvAnim.prevFrame();
  42. } else if (e.getSource() == btnNext){
  43. cnvAnim.nextFrame();
  44. } else if (e.getSource() == btnAnim){
  45. //change the animation status
  46. if (timer.isRunning()){
  47. btnAnim.setText("animate");
  48. timer.stop();
  49. this.animating = false;
  50. } else {
  51. btnAnim.setText("stop");
  52. timer.start();
  53. this.animating = true;
  54. } // end if
  55. } else if (e.getSource() == btnSave){
  56. cnvAnim.save();
  57. } else if (e.getSource() == btnLoad){
  58. cnvAnim.load();
  59. } else if (this.animating == true){
  60. cnvAnim.anim();
  61. } else {
  62. System.out.println("action not defined");
  63. } // end if
  64. } // end actionPerformed
  65. public static void main(String[] args){
  66. new AsciiAnim();
  67. } // end main
  68. } // end class def