Results 1 to 5 of 5
  1. #1
    demaja is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default Applet throws exceptions

    My applet is supposed to show a circle growing by asking the user for a beginning and ending radius (it's also supposed to check to make sure that r1 < r2, but I want to fix this part of code first.) My code throws tons of exceptions and asks the user for the radii twice, whether I use the console or JOptionPane (which is what I left it on.) I think it asks twice because of the thrown exceptions, but I really don't know, nor do I know how to fix these exceptions. The book I'm using is Java 6 Illuminated. Any ideas on how I should fix this? Thank you. :)

    import javax.swing.JOptionPane;
    import java.awt.Graphics;
    import java.awt.Color;
    import javax.swing.JApplet;


    public class BallGrows extends JApplet
    {
    public void paint(Graphics g)
    {
    super.paint(g);

    final int X = 250;
    final int Y = 250;
    final Color COLOR = Color.BLUE;

    String rad1 = JOptionPane.showInputDialog(null, "Beginning radius:");
    String rad2 = JOptionPane.showInputDialog(null, "Ending radius:");

    int r1 = Integer.parseInt(rad1);
    int r2 = Integer.parseInt(rad2);

    for(int i = r1; i <= r2; i++)
    {
    g.setColor(COLOR);

    g.fillOval(X - i, Y - i, i*2, i*2);
    }

    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You are displaying the dialogs within the paint() method: consequently they will be displayed every time the applet paints itself.

    Try moving these lines to some other method. Perhaps one of the "milestone" methods.

  3. #3
    demaja is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    I'm trying to stay within the confines of my book, which don't go over any of those methods. But, if I can't figure it out otherwise, I'll use one and make a comment in my source code. Thank you.

  4. #4
    demaja is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    I'm trying to stay within the confines of my book, which don't go over any of those methods. But, if I can't figure it out otherwise, I'll use one and make a comment in my source code. Thank you.

  5. #5
    demaja is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Making a Pause class using the wait() method fixed the exceptions, but the dialog boxes still ask for user input twice. Why does it do that?

    import javax.swing.JOptionPane;
    import java.awt.Graphics;
    import java.awt.Color;
    import javax.swing.JApplet;


    public class BallGrows extends JApplet
    {
    public void paint(Graphics g)
    {
    super.paint(g);

    final int X = 250;
    final int Y = 250;
    final Color COLOR = Color.BLUE;

    String rad1 = JOptionPane.showInputDialog(null, "Beginning radius:");
    String rad2 = JOptionPane.showInputDialog(null, "Ending radius:");

    int r1 = Integer.parseInt(rad1);
    int r2 = Integer.parseInt(rad2);

    for(int i = r1; i <= r2; i++)
    {
    g.setColor(COLOR);

    g.fillOval(X - i, Y - i, i*2, i*2);

    Pause.wait(0.03);
    }

    }
    }

    class Pause
    {
    static void wait(double seconds){}
    }

Similar Threads

  1. throws exception
    By simorgh in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 01:24 AM
  2. Replies: 1
    Last Post: 11-25-2009, 07:48 AM
  3. Applet throws exception while recording
    By Basit56 in forum Java Applets
    Replies: 1
    Last Post: 08-20-2009, 02:42 PM
  4. Replies: 1
    Last Post: 07-27-2009, 12:59 PM
  5. throws
    By jdgallag in forum New To Java
    Replies: 14
    Last Post: 02-11-2009, 02:07 AM

Posting Permissions

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