Phr0zen_Penguin

TimerDemo.java - Exploiting Anomalies

Mar 9th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. /*==================================================================================================
  2.   --------------------------------------------------------------------------------------------------
  3.   [TimerDemo.java]
  4.   Exploiting anomalies:
  5.   The code implemented here is proof that no two programmers think alike.
  6.  
  7.   It is coding convention to have an exception report an error (or anomaly, to be more precise), and
  8.   have you program take relative actions.
  9.  
  10.   This piece of code is written to reverse the aforementioned-trait however (portray the exception
  11.   as 'normal' operations), and (the catch block) is used to do the 'contemporary' work.
  12.  
  13.         (c) Damion 'Phr0z3n.dev' Tapper, 2015.
  14.         Email: [email protected]
  15.         Website: http://L337Stuff.Blogspot.com
  16.   --------------------------------------------------------------------------------------------------
  17.   ==================================================================================================*/
  18. import javax.swing.Timer;
  19. import javax.swing.JFrame;
  20. import javax.swing.JButton;
  21. import javax.swing.JMenuBar;
  22. import javax.swing.JMenu;
  23. import javax.swing.JMenuItem;
  24. import java.awt.event.WindowEvent;
  25. import java.awt.event.WindowAdapter;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.ActionEvent;
  28.  
  29. public class TimerDemo extends JFrame implements ActionListener
  30. {
  31.     public static void main(String arg[])
  32.     {
  33.         new TimerDemo();
  34.     }
  35.  
  36.     public TimerDemo()
  37.     {
  38.         this.addWindowListener(new WinAdapt());
  39.  
  40.         new Timer(1000 * 1, this).start(); // constructs a timer to pulse at 1 second intervals, and to report the timer pulses to the ActionListener
  41.  
  42.         buildMenu();
  43.         buildFrame();
  44.         this.setLocation(250, 150);
  45.         this.setVisible(true);
  46.     }
  47.  
  48.     private void buildFrame() // for aesthetics
  49.     {
  50.         JButton exitButton = new JButton("Exit");
  51.  
  52.         exitButton.addActionListener(this);
  53.  
  54.         this.add(exitButton);
  55.         this.setSize(300, 200);
  56.        
  57.     }
  58.  
  59.     private void buildMenu()
  60.     {
  61.         JMenuBar    menuBar = new JMenuBar();
  62.         JMenu       fileMenu = new JMenu("File");
  63.  
  64.         menuBar.add(fileMenu);
  65.  
  66.  
  67.         JMenuItem   exitMenuItem = new JMenuItem("Exit");
  68.  
  69.         exitMenuItem.addActionListener(this);
  70.  
  71.         fileMenu.add(exitMenuItem);
  72.         this.setJMenuBar(menuBar);
  73.     }
  74.  
  75.     @Override
  76.     public void actionPerformed(ActionEvent e) // called with each expiration of the timer
  77.     {
  78.         try
  79.         {
  80.             /**
  81.              * NOTE:
  82.              * The command string is null if no user input is done (one was not set).
  83.              *
  84.              * This is the magic bullet that triggers the exception (no pun intended).
  85.              */
  86.             switch(e.getActionCommand())
  87.             {
  88.                 case "Exit":
  89.                     System.exit(0);
  90.                     break;
  91.             }
  92.         }
  93.         catch(NullPointerException ex) // a NullPointerException is thrown if 'switch' tries to operate on a null string...
  94.         {
  95.             /**
  96.              * ...and when an exception should be a 'negative thing'...
  97.              */
  98.  
  99.             System.out.println("Timer fired!"); // I did this cause I can, Yes I did! Yes I did!
  100.         }
  101.     }
  102. }
  103.  
  104. class WinAdapt extends WindowAdapter
  105. {
  106.     @Override
  107.     public void windowClosing(WindowEvent e)
  108.     {
  109.         super.windowClosing(e);
  110.  
  111.             if(e.getID() == WindowEvent.WINDOW_CLOSING)
  112.                 System.exit(0);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment