0

My code is fine, it compiles just fine, my teacher looked at it and we cannot figure out how to make the applet run correctly. I am making a GUI that takes your weight and calculates how much you would weigh on a selected planet. When I run it, the applet pops up, but is blank and says "Start applet not initialised". I am using BlueJ. Here is my code:

   import java.applet.Applet;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.Scanner;
   import javax.swing.JOptionPane;
   import java.text.*;
   public class Lab24 extends Applet implements ActionListener, ItemListener
{
private Label lab1;
protected TextField info, result;
private TextField input;
private Button butt;
private Choice planet;
private Font font1,font2,font3,font4;
static double w1,w2;

public void init(){
    setBackground(new Color(0,0,199));
    setLayout(new GridLayout(3,2,20,50));
    font1 = new Font("Courier",Font.PLAIN,15);
    font2 = new Font("Monospaced",Font.BOLD,8);
    font3 = new Font("TimesNewRoman",Font.PLAIN,12);
    font4 = new Font("Serif",Font.ITALIC,18);

    info.setText("This program allows you to\ncalculate your weight on\nanother planet!");
    info.setEditable(false);
    info.setFont(font1);
    add(info);

    lab1 = new Label();
    lab1.setFont(font2);
    lab1.setForeground(Color.black);
    lab1.setText("Enter your weight(in pounds)!");
    add(lab1);

    input = new TextField();
    input.addActionListener(this);
    add(input);

    planet.setFont(font3);
    planet = new Choice();
    planet.setBackground(new Color(107,180,243));
    planet.setForeground(Color.black);
    planet.add("Choose a planet");
    planet.add("Mercury");
    planet.add("Venus");
    planet.add("Mars");
    planet.add("Jupiter");
    planet.add("Saturn");
    planet.add("Uranus");
    planet.add("Neptune");
    planet.addItemListener(this);
    add(planet);

    butt.setFont(font2);
    butt = new Button("Calculate");
    butt.addActionListener(this);
    add(butt);

    result.setFont(font4);
    result = new TextField();
    result.setEditable(false);
    result.setForeground(Color.blue);
    add(result);
}

public void itemStateChanged(ItemEvent e) {
    if(e.getSource()==planet){
        switch(planet.getSelectedIndex()){
            case 1: w2 = w1 * 0.37;
            break;
            case 2: w2 = w1 * 0.88;
            break;
            case 3: w2 = w1 * 0.38;
            break;
            case 4: w2 = w1 * 2.64;
            break;
            case 5: w2 = w1 * 1.15;
            break;
            case 6: w2 = w1 * 1.15;
            break;
            case 7: w2 = w1 * 1.12;
            break;
        }
    }
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource()==input){
        String s = input.getText();
        input.setText(s);
    }
    if(e.getSource() == butt){
        result.setText("You would weigh\n"+w2+" pounds!");
    }
}

public void paint(Graphics g){
    g.setColor(Color.red);
    g.drawOval(10,50,25,25);
}

}

  • 4
    Let's start with, why are you using AWT? It's 2017, it's seriously time to let go of it, second, why are you using applets? They are deprecated and are no longer supported; How are you loading the applet? As a recommendation, I'd move on to Swing, if not JavaFX - tell your teacher they need to update and move with the times – MadProgrammer May 17 '17 at 20:19
  • 1
    1) [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 4) See .. – Andrew Thompson May 18 '17 at 06:11
  • .. [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 5) `String s = input.getText(); input.setText(s);` What did you expect this to achieve? – Andrew Thompson May 18 '17 at 06:12

0 Answers0