Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Bouncing Ball Exception Handling

    Sometimes when I lose power on my laptop, when I go to open my java file, all I get is a long row of 0's and 1's. What's happening and is there a way to fix it?

    The title is about exception handling, but I edited the original post because I want to take another try and figure things out on my own and come back with a more educated question if I need to.
    Last edited by trantommyd; 06-20-2012 at 07:14 AM.

  • #2
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks in advance for your time
    Last edited by trantommyd; 06-20-2012 at 06:55 AM.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok now I'm tryin to add an actionListener for jbtAdd. When jbtAdd is pressed, I want to add another ball. However, I can't get the new ball to show up on the screen. What am I missing?

    Code:
    /* Assignment 2: Happy Bouncy Ball
    
    */
    
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.Timer;
    import java.awt.event.*;
    
    public class BallBounce
    {
    	public static void main(String args[])
    	{
    		Applet1 applet = new Applet1();
    		applet.init();
    		applet.start();
    		
    		JFrame frame = new JFrame();
    		frame.setTitle("Bouncy Ball");
    		frame.setSize(500, 500);
    		frame.setLocation(50, 50);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		
    		frame.add(applet);
    	}
    }
    
    class Applet1 extends Applet
    {
    		public Applet1()
    	{
    		add(new BallControl());
       }
    }
    class BallControl extends JPanel
    {
    	//make a ball, buttons, and a scrollbar
    	private Ball ball = new Ball();
    	private ImageIcon suspendIcon = new ImageIcon("suspend.gif");
    	private JButton jbtSuspend = new JButton(suspendIcon);
    	private ImageIcon resumeIcon = new ImageIcon("resume.gif");
    	private JButton jbtResume = new JButton(resumeIcon);
    	private JButton jbtAdd = new JButton("Add a ball.");
    	private JScrollBar jsbDelay = new JScrollBar();
    	
    	public BallControl()
    	{
    		//make and fill a panel with the buttons
    		JPanel p1 = new JPanel();
    		p1.setPreferredSize(new Dimension(300,300));
    		jbtResume.setPreferredSize(new Dimension(20, 20));
    		jbtSuspend.setPreferredSize(new Dimension(20, 20));
    		p1.add(jbtSuspend);
    		p1.add(jbtResume);
    		p1.add(jbtAdd);
    		
    		ball.setBorder(new javax.swing.border.LineBorder(Color.blue));
    		ball.setPreferredSize(new Dimension(200, 100));
    		jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
    		ball.setDelay(jsbDelay.getMaximum());
    		setLayout(new BorderLayout(100, 100));
    		add(jsbDelay, BorderLayout.NORTH);
    		add(ball, BorderLayout.CENTER);
    		add(p1, BorderLayout.SOUTH);
    		
    		//make listeners for the components
    		
    		jbtAdd.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent a)
    			{
    				System.out.println("Added a ball.");
    				/*Ball ball2 = new Ball();
    				ball2.setBorder(new javax.swing.border.LineBorder(Color.blue));
    				ball2.setPreferredSize(new Dimension(200, 100));
    				ball2.setDelay(jsbDelay.getMaximum());
    				add(ball2, BorderLayout.CENTER);*/
    			}
    		});
    		
    		jbtSuspend.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				try
    				{
    					if(!ball.isMoving())
    					{
    						throw new AlreadySuspendException("Already suspended.");
    					}
    					ball.suspend();
    				}
    				catch(AlreadySuspendException ex)
    				{
    					System.out.println("Already suspended.");
    				}
    			}
    		});
    		
    		jbtResume.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				try
    				{
    					if(ball.isMoving())
    					{
    						throw new Exception("Ball is already moving.");
    					}
    					
    					ball.resume();
    				}
    				catch(Exception r)
    				{
    					System.out.println("Ball is already moving.");
    				}
    			}	
    		});
    		
    		jsbDelay.addAdjustmentListener(new AdjustmentListener()
    		{
    			public void adjustmentValueChanged(AdjustmentEvent e)
    			{
    				try
    				{
    					ball.setDelay(jsbDelay.getMaximum() - e.getValue());
    				}
    				catch(NullPointerException ex)
    				{	
    					System.out.println("Nullpointer ex.");
    				}
    			}
    		});
    	}
    }
    
    class AlreadySuspendException extends Exception
    {
    	public AlreadySuspendException(String ex)
    	{
    		super("Already suspended.");
    	}
    }
    
    class Ball extends JPanel
    {
    	//create a time with a 1000 millisecond delay
    	private int delay = 10;
    	private Timer timer = new Timer(delay, new TimerListener());
    	
    	//the balls initial position
    	private int x = 0;
    	private int y = 0;
    	
    	//the balls radius(size)
    	private int radius = 3;
    	
    	//increment the balls position
    	private int ix = 2;
    	private int iy = 2;
    	
    	//create a counter to dictate the color of the ball
    	int color = 0;
    	
    	public Ball()
    	{
    		timer.start();
    		color++;
    	}
    	
    	private class TimerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			repaint();
    		}
    	}
    	
    	protected void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		
    		if(color == 1)
    		g.setColor(Color.blue);
    		
    		else if(color == 2)
    		g.setColor(Color.red);
    		
    		else
    		g.setColor(Color.green);
    		
    		//check the boundaries to keep the ball inside the box
    		if(x < radius)
    		{
    			ix = Math.abs(ix);
    		}
    		if(x > getWidth() - radius)
    		{
    			ix = -Math.abs(ix);
    		}
    		if(y < radius)
    		{
    			iy = Math.abs(iy);
    		}
    		if(y > getHeight() - radius)
    		{
    			iy = -Math.abs(iy);
    		}
    		
    		//adjust the position of the ball
    		x += ix;
    		y += iy;
    		g.fillOval(x-radius, y-radius, radius*2, radius*2);
    	}
    	
    	public void suspend()
    	{
    		timer.stop();
    	}
    	
    	public boolean isMoving()
    	{
    		return timer.isRunning();
    	}
    	
    	public void resume()
    	{
    		timer.start();
    	}
    	
    	public void setDelay(int delay)
    	{
    		this.delay = delay;
    		timer.setDelay(delay);
    	}
    }

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Any ideas?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •