Java Beginners

Moderators: zibadian
Number of threads: 1279
Number of posts: 2729

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Applet exceptions Posted by demaja on 7 May 2011 at 3:57 PM
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
Report
Re: Applet exceptions Posted by demaja on 7 May 2011 at 4:00 PM
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);
}

}
}
Report
Exceptions fixed; dialog still asks for input twice Posted by demaja on 9 May 2011 at 7:29 AM
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){}
}
Report
Re: Exceptions fixed; dialog still asks for input twice Posted by silveredge52 on 10 May 2011 at 4:42 AM
Hey,
This code eliminates the double prompts.
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
            }
        }
    }

The java virtual machine will invoke the paint() routine whenever it thinks it needs to be done. Thus the frequent input prompts.


regards, se52
Report
Re: Exceptions fixed; dialog still asks for input twice Posted by demaja on 10 May 2011 at 5:40 PM
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.
Report
Re: Exceptions fixed; dialog still asks for input twice Posted by demaja on 10 May 2011 at 5:58 PM
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!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.