Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help...:P
#2
Hey Jake! Welcome to the forums, and I am glad that you are enjoying my tutorials!

Well, your first assumption was correct.. You cannot use Scanners in applets, only in console applications. So you can simply get rid of any scanner code here.

Also: When creating an applet you need to tell Java that you are actually making an applet, you can do that by ending extends Applet onto the end of your class name. For example,
Code:
public class TriangleArea extends Applet
{
}

Also, since you are not using a scanner, you no longer need to import the java.util.* libraries.

Now: When creating an applet, the applet does not automatically run the MAIN method, instead it automatically runs the PAINT method. Therefore, you simply need to take everything in the main method, and put it into a new public void paint() method.

Also: You have alot of variables here, so I edited them a little bit.

Here is your new functional code: feel free to ask any questions about it!

Code:
import javax.swing.*;
import java.applet.Applet;
import java.awt.Graphics;
public class TriangleArea extends Applet {
        double base = Double.parseDouble(JOptionPane.showInputDialog("Please enter the base of your triangle:"));
        double height = Double.parseDouble(JOptionPane.showInputDialog("Please enter the height of your triangle:"));
       public void paint(Graphics g)
       {
           double PreArea3 = base * height;
           double Area = PreArea3 / 2;
           JOptionPane.showMessageDialog(null, "The area of your triangle is" + Area + " Units squared :)");
       }

}
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
Need some help...:P - by Jake - 11-10-2011, 01:46 PM
Re: Need some help...:P - by brandonio21_phpbb3_import2 - 11-10-2011, 05:36 PM
Re: Need some help...:P - by Jake - 11-10-2011, 10:21 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)