/* ***************************************************************** Homework #5 Author: Butler, Martin Date: 2004.11.27 Due: 2004.12.02 CMPT 505 ***************************************************************** */ import java.applet.Applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.lang.*; public class Bubble extends JApplet implements ActionListener { final int MAXBUBBLE = 100; boolean playMode = false; // ************************** // south panel components // ************************** JPanel soPanel = new JPanel(); JButton startButton = new JButton ("Start"); JButton stopButton = new JButton ("Stop"); // ************************** // north panel components // ************************** JPanel noPanel = new JPanel(); JLabel totBubbles = new JLabel("Number of Bubbles:"); int intTotBubbles = 0; String strTotBubbles = "" + intTotBubbles; JLabel numTotBubbles = new JLabel(strTotBubbles); JLabel totHit = new JLabel("Bubbles Hit:"); int intTotHit = 0; String strTotHit = "" + intTotHit; JLabel numTotHit = new JLabel(strTotHit); // ************************** // center panel components // ************************** JPanel contentPane = new JPanel(new BorderLayout()); CanvasPanel canvas = new CanvasPanel (MAXBUBBLE); Dimension appSize; // ************************************************* // Random number to control random aspects of game, // including where to create of move a circle // ************************************************* Random r = new Random (); final float OFFSET = 0; final float RANGE = 50; int appWidth, appHeight; final int DELAY = 100; javax.swing.Timer timer; public Bubble () { System.out.println ("Fire constructed"); } public void init () { System.out.println ("Fire initialized with contentPane size "+ contentPane.getSize ()); timer = new javax.swing.Timer (DELAY, null); startButton.addActionListener (this); stopButton.addActionListener (this); timer.addActionListener (new ReboundListener()); String widthS = getParameter ("width"); String heightS = getParameter ("height"); appWidth = Integer.parseInt(widthS); appHeight = Integer.parseInt(heightS); System.out.println("width="+widthS+" height="+heightS); appSize = new Dimension (appWidth, appHeight); soPanel.setPreferredSize (new Dimension(150, 50)); soPanel.setBackground (Color.blue); soPanel.add (startButton); soPanel.add (stopButton); noPanel.setPreferredSize (new Dimension (150, 50)); noPanel.setBackground (Color.blue); noPanel.add (totBubbles); noPanel.add (numTotBubbles); noPanel.add (totHit); noPanel.add (numTotHit); contentPane.setSize (appSize); contentPane.setPreferredSize (appSize); contentPane.add (canvas, BorderLayout.CENTER); contentPane.add (noPanel, BorderLayout.NORTH); contentPane.add (soPanel, BorderLayout.SOUTH); contentPane.setOpaque (true); this.setContentPane(contentPane); } public void actionPerformed (ActionEvent e) { JButton b = (JButton) e.getSource(); if (b == startButton) { playMode = true; canvas.reset(); repaint(); System.out.println ("Timer Started"); timer.start(); } if (b == stopButton) { playMode = false; } } public class ReboundListener implements ActionListener { public void actionPerformed (ActionEvent e) { // **************************************************** // generate a random to determine actions during each // loop through the timer // **************************************************** float bubbleAction = r.nextFloat() * RANGE + OFFSET; System.out.println("bubbleAction="+bubbleAction); // ******************************************************* // only creates bubbles if random number is less then 10 // ******************************************************* if (bubbleAction < 10) { canvas.createBubble(); strTotBubbles = "" + intTotBubbles; } // ******************************************************** // verify if circles are needed to move by calling // movebubble and passing the random number to // compare against the circles speed attribute // ******************************************************** canvas.moveBubble(bubbleAction); // ******************************************************** // updated the counter displays // ******************************************************** strTotBubbles = "" + canvas.totBubbles; strTotHit = "" + canvas.totHit; numTotBubbles.setText(strTotBubbles); numTotHit.setText(strTotHit); repaint(); // ************************************************************ // performed if the stop button was clicked for the first time // after the start button clicked. // ************************************************************ if (playMode == false) { timer.stop(); canvas.clear(); repaint(); } } } }