Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Applet exceptions

demajademaja Posts: 5Member
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);
}

}
}B

Comments

  • demajademaja Posts: 5Member
    Sorry. Somehow, I pasted it twice--once within itself.

    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);
    }

    }
    }
  • demajademaja Posts: 5Member
    I fixed the exceptions by adding a Pause class using the wait() method, but the dialog boxes and console still ask the user for input twice. How do I fix that?

    class Pause
    {
    static void wait(double seconds){}
    }
  • silveredge52silveredge52 Posts: 198Member
    Hey,
    This code eliminates the double prompts.
    [code]
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;

    public class BallGrows extends JApplet {

    String rad1, rad2;

    public void start() {
    getInput();
    }

    public void getInput() {
    rad1 = JOptionPane.showInputDialog(null, "Beginning radius:");
    rad2 = JOptionPane.showInputDialog(null, "Ending radius:");
    }

    public void paint(Graphics g) {
    super.paint(g);
    final int X = 250;
    final int Y = 250;
    final Color COLOR = Color.BLUE;


    int r1 = 25; // default values
    int r2 = 25; // default values
    if (rad1 != null) {
    r1 = Integer.parseInt(rad1);
    }
    if (rad2 != null) {
    r2 = Integer.parseInt(rad2);
    }

    g.setColor(COLOR);
    for (int i = r1; i <= r2; i++) {
    g.fillOval(X - i, Y - i, i * 2, i * 2);
    g.drawOval(X - i, Y - i, i * 2, i * 2); // missing statement
    }
    }
    }[/code]
    The java virtual machine will invoke the paint() routine whenever it thinks it needs to be done. Thus the frequent input prompts.


    regards, se52
  • demajademaja Posts: 5Member
    Thanks for the reply!!! I'm sorry, but your code is waaaaaaaaay beyond the point that I'm at in my book. What I am working on at the moment is making a class that receives the input and then another class that paints it.
  • demajademaja Posts: 5Member
    Btw, I'm not sure why you said that "g.drawOval(X - i, Y - i, i * 2, i * 2);" was a missing statement. g.fillOval draws a filled oval, which is what I want. I don't need to draw it then fill it, b/c it's all done in one go. But, perhaps there is something more to it that I'm missing? Thanks!
Sign In or Register to comment.