0

I'm wondering why my applet isn't running. I'm using netbeans, and the program works fine with no errors, but I can't seem to get the applet screen to appear to test all the functions of my program.I was wondering if someone can point out what I am doing wrong in starting my applet up. I haven't done to many things with applets, so I'm quite curious why it isn't upping and running for me. I really didn't want to post the full code of my program, but I've no idea if there's an error in my code, or I'm just not doing the right thing to actually start the applet or something.

I do not get any errors while running my program, that applet simply does not appear and run in itself. Otherwise I get no errors from neatbeans while running my program.

import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;



public class Program7 extends JApplet implements ActionListener, ItemListener
{

//static varaibles
private static int GuiHeight = 90;
private static int Square = 50;

//gui varaibles
private JRadioButton drawSquare;
private JRadioButton mess;

private JTextField messageFeild;

private JCheckBox color;

private JButton button;

private JComboBox location;

//modable global varaibles
private int width, height, drawX, drawY;
private Color drawColor;
private String draw;

@Override
public void init()
{
    //creating the panels
    JPanel drawChoice;

    JPanel drawSet;

    //setting group onject to link certain buttons together
    ButtonGroup ObjGroup;

    //setting layout to make things look nice
    setLayout(new FlowLayout());

    //ibtializing other varaibles
    width = 0;
    height = 0;
    drawX = 0;
    drawY= 0;
    drawColor = Color.BLACK;
    draw = "";

    //intialzing the gui interface
    drawSquare = new JRadioButton("Draw a Square!");
    mess = new JRadioButton("Write a Message!");
    location = new JComboBox(new String[]{"", "Random Pick", "Top Left"});
    color = new JCheckBox("Draw in Color!");
    button = new JButton("Draw it!");


    //linking the buttons together
    ObjGroup = new ButtonGroup();
    ObjGroup.add(drawSquare);
    ObjGroup.add(mess);


    //adding event handlers to the buttons
    location.addItem(this);
    button.addActionListener(this);
    color.addItemListener(this);
    drawSquare.addActionListener(this);
    mess.addActionListener(this);

    messageFeild = new JTextField(20);


    //adding to the first paels
    drawChoice = new JPanel();
    drawChoice.add(drawSquare);
    drawChoice.add(mess);
    drawChoice.add(messageFeild);
    //adding to the applet
    add(drawChoice);

    //adding to the second panel
    drawSet = new JPanel();
    drawSet.add(new JLabel("Select where to draw!"));
    drawSet.add(location);
    drawSet.add(color);
    drawSet.add(button);
    //adding to the applet
    add(drawSet);

}
@Override
public void paint(Graphics g)
{
    //calling super constructor
    super.paint(g);

    //getting width
    width = getWidth();
    height = getHeight();

    //setting graphics color
    g.setColor(drawColor);
    if (location.getSelectedItem() != "")
    {
        //ifstatement checking for inputs
        if(draw == "Square"||draw == "square"||draw == "SQUARE")
        {
            //creating rectange
            g.fillRect(drawX, drawY, width, height);
        }
        if(draw == "Message"|| draw == "MESSAGE"||draw == "message")
        {
            //writing message
            g.drawString(messageFeild.getText(), drawX, drawY);
        }
    }
}
    @Override
    public void actionPerformed(ActionEvent e)
    {
        //repainting
        repaint();
    }
    @Override
    public void itemStateChanged(ItemEvent e)
    {
        //if statement checking for the various changes being made by the user in the pograms interface

        //if statement check checking to see if there's a square or to write a message
        if (e.getSource() == drawSquare && e.getStateChange() == ItemEvent.SELECTED)
        {
            draw = "Square";
            messageFeild.setEnabled(false);
        }
        if (e.getSource() == mess && e.getStateChange() == ItemEvent.SELECTED)
        {
            draw = "Message";
            messageFeild.setEnabled(true);
        }
        //if statement set checking for locational input
        if (e.getSource() == location)
        {
            if(location.getSelectedItem() == "Random Pick")
            {
                drawX=(int)(Math.random()*(width+1));
                drawY=(int)(GuiHeight+ Math.random()*(height-GuiHeight+1));
            }
            if(location.getSelectedItem() == "Top Left")
            {
                drawX=0;
                drawY=GuiHeight;
            }

        }
        //if statement to check for color change
        if(e.getSource()==color && e.getStateChange()==ItemEvent.SELECTED)
        {
            drawColor = Color.GREEN;
        }
        if(e.getSource()==color && e.getStateChange()==ItemEvent.DESELECTED)
        {
            drawColor = Color.BLACK;
        }


    }



public static void main(String[] args) {

}

}

  • Are you sure you are running the Applet and not the (empty) `main` method that you have in the same file? – mastov May 05 '16 at 17:30
  • Don't compare strings with `==`. See: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jesper May 05 '16 at 17:31
  • I don't know I might, how do I choose to run the applet over the main method then? – Hayden Gfeller May 05 '16 at 17:31
  • why main method ?http://stackoverflow.com/questions/932052/why-do-applets-not-need-a-main – Gangaraju May 05 '16 at 17:32
  • @HaydenGfeller: Please post the command (or clicks in the IDE) that you use to run the Applet. Do you get any error messages? – mastov May 05 '16 at 17:36
  • @mastov I added the imports I used, and I do not get any error messages. – Hayden Gfeller May 05 '16 at 17:41
  • First of all, why do you have a main method ? xD Then when i try on my computer, i see your applet running properly... – Cukic0d May 05 '16 at 17:44
  • @Cukic0d I don't know, I deleted the main, but I still get nothing. When I click the green arrow to the run it on the bar above, I get nothing just that the program's run its course. – Hayden Gfeller May 05 '16 at 17:48
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [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. – Andrew Thompson May 07 '16 at 06:28

0 Answers0