Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

applet trouble here

 
Shane McKenzie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to know why this won't work; it seems as though it should...I overrode the init() method so that the user can type a choice in JOptionPane, and that works when the html file is opened, but then regardless of user input, NO drawing elements appear! My sanity is slipping.....

HTML:

<HTML>
<applet code = Test.class width = 200 height = 200>
</applet>
</HTML>

Applet file:

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

public class Test extends Applet
{
//data fields
private String shapeChoice;

public void init()
{
shapeChoice =
JOptionPane.showInputDialog( "Please enter C, S, or R:" );
}

public void paint(Graphics g)
{
if (shapeChoice == "c")
{
g.setColor(Color.black);
g.drawLine(0, 0, 100, 100);
}

else if (shapeChoice == "r")
{
g.setColor(Color.black);
g.drawLine(0, 0, 50, 50);
}

}

}// end class test

Any help here would be greatly appreciated!

shane
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be too easy, but I noticed you compare Strings using "==", which checks to see if both Strings are the same object. You need to use one of the String equals methods. Try choice.equalsIgnoreCase("C") and see if that helps. (Check the String API to make sure I spelled it right!)
[ November 09, 2004: Message edited by: Jeff Bosch ]
 
Shane McKenzie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff, I appreciate the help. Such a rookie mistake, but there ya go! Anyway, that seems to have worked ok, it runs properly now. Thanks again mate!
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're quite welcome, Shane. I made that mistake more than once when I was first learning Java. Now I always start debugging by looking for the easy stuff.

By the way, very cool sig. I got a good laugh out of that!
[ November 09, 2004: Message edited by: Jeff Bosch ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic