Results 1 to 5 of 5
Thread: Applet throws exceptions
- 05-08-2011, 06:34 PM #1
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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);
}
}
}
- 05-08-2011, 06:57 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 05-08-2011, 10:27 PM #3
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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.
- 05-08-2011, 10:33 PM #4
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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.
- 05-09-2011, 02:57 PM #5
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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
-
throws exception
By simorgh in forum New To JavaReplies: 1Last Post: 07-30-2010, 12:24 AM -
Need HELP with java applet--- throws NullPointerException
By mjunit in forum Java AppletsReplies: 1Last Post: 11-25-2009, 06:48 AM -
Applet throws exception while recording
By Basit56 in forum Java AppletsReplies: 1Last Post: 08-20-2009, 01:42 PM -
Applet with Signed JAR throws AccessControlException in AppletViewer
By Technolithic in forum Java AppletsReplies: 1Last Post: 07-27-2009, 11:59 AM -
throws
By jdgallag in forum New To JavaReplies: 14Last Post: 02-11-2009, 01:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks