Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input problems in an applet. 2

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I need someone to tell me what I'm doing wrong in this java applet, I used TextPad to write it & Sun's java software to compile it. I have several applets just like it that work, but I can't get this one to work.

import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class Circle extends Applet {
public void init() {
String prompt = "Enter a radius less than 150.";
String input = JOptionPane.showInputDialog(prompt);
int r = Integer.parseInt(input);
}

public void paint(Graphics g) {
Graphics2D pen = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, r * 2, r * 2);
pen.draw(circle);
}
private int r;
}

If anyone can help me I would apreciate it.
 
Yeah, you declare r as an int in init() effectively hiding the instance variable. Change the line to:

r = Integer.parseInt(input);

Regards,

Charles
 
you have declared 2 integers called r.

The one declared inside your init method will be destroyed as soon as the init has completed - i.e. it will go out of scope.

Your other r is a global variable. You need to make one small change - where you declare int r inside init. Just leave out the int part and have just

r = Integer.parseInt(input);

This should mean that when you refernce r you areonly rferencing the global version.

nb. i would declare all globals at the top, it is easier to read and debug that way.::)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top