hello. i have a problem running my very first applet (im just converting my previous application to applet). i tried to follow the instructions i found in the manuals, both the one given by my prof and the ones i found in the web, but unfortunately my program wont work. :(
i tried other sample applets and they run successfully, so i think the problem is with my codes and not with my browser. can you please help me find the bug in my program? id be very glad to hear from you. thank you!

here's my program:

/*Criselle Zion T. Ampo
 *2006-26021*/

/*this program creates an applet of a GUI that will draw an oval, a rectangle,or a line based on the dimensions inputted by the user.*/

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

public class MyGUIApplet extends Applet implements ActionListener 
{
    private MyCanvas fCanvas;
    private TextField x;
    private TextField y;
    private TextField w;
    private TextField h;
    private TextField x1;
    private TextField y1;
    private TextField x2;
    private TextField y2;

    //Creates frame and add components
    public void init() 
    {
        //creates the canvas GUI
        JPanel JP1 = new JPanel();
        BorderLayout BL = new BorderLayout();
        JP1.setLayout(BL);
        fCanvas = new MyCanvas();
        JP1.add(fCanvas);

        //creates label x and its textfield
        JPanel JP3 = new JPanel();
        FlowLayout BL9 = new FlowLayout();
        JP3.setLayout(BL9);
        JLabel ex = new JLabel("         X  ");
        x = new TextField(5);
        JP3.add(ex);
        JP3.add(x);

        //creates label y and its textfield
        JPanel JP4 = new JPanel();
        FlowLayout BL1 = new FlowLayout();
        JP4.setLayout( BL1 );
        JLabel wai = new JLabel("Y  ");
        y = new TextField(5);
        JP4.add(wai);
        JP4.add(y);

        //integrates the panels of x&y into one panel
        JPanel JP5 = new JPanel();
        BorderLayout BL2 = new BorderLayout();
        JP5.setLayout( BL2 );
        JP5.add(JP3, BorderLayout.WEST);
        JP5.add(JP4, BorderLayout.EAST);

        //creates label width and its textfield
        JPanel JP6 = new JPanel();
        FlowLayout BL3 = new FlowLayout();
        JP6.setLayout( BL3 );
        JLabel width = new JLabel("Width  ");
        w = new TextField(5);
        JP6.add(width);
        JP6.add(w);

        //creates label height and its textfield
        JPanel JP7 = new JPanel();
        FlowLayout BL4 = new FlowLayout();
        JP7.setLayout( BL4 );
        JLabel height = new JLabel("Height  ");
        h = new TextField(5);
        JP7.add(height);
        JP7.add(h);

        //integrates the panels of width&height into one panel
        JPanel JP8 = new JPanel();
        BorderLayout BL5 = new BorderLayout();
        JP8.setLayout( BL5 );
        JP8.add(JP6, BorderLayout.WEST);
        JP8.add(JP7, BorderLayout.EAST);

        //Creates Rectangular&Oval buttons
        JPanel JP9 = new JPanel();
        FlowLayout BL6 = new FlowLayout();
        JP9.setLayout( BL6 );
        JButton rect = new JButton("Rectangular");
        rect.setActionCommand("rect");
        rect.addActionListener(this); 
        JButton oval = new JButton("Oval");
        oval.setActionCommand("oval");
        oval.addActionListener(this); 
        JP9.add(rect);
        JP9.add(oval);

        //creates the label of x1 & its textfield 
        JPanel JP12 = new JPanel();
        FlowLayout BL10 = new FlowLayout();
        JP12.setLayout( BL10 );
        JLabel ex1 = new JLabel("      X1  ");
        x1 = new TextField(5);
        JP12.add(ex1);
        JP12.add(x1);


        //creates the label of y1 & its textfield
        JPanel JP13 = new JPanel();
        FlowLayout BL11 = new FlowLayout();
        JP13.setLayout( BL11 );
        JLabel wai1 = new JLabel("      Y1  ");
        y1 = new TextField(5);
        JP13.add(wai1);
        JP13.add(y1);

        //integrates the panels of x1&y1 into one panel
        JPanel JP14 = new JPanel();
        BorderLayout BL12 = new BorderLayout();
        JP14.setLayout( BL12 );
        JP14.add(JP12, BorderLayout.WEST);
        JP14.add(JP13, BorderLayout.EAST);


        //creates the label of x2 & its textfield
        JPanel JP15 = new JPanel();
        FlowLayout BL13 = new FlowLayout();
        JP15.setLayout( BL13 );
        JLabel ex2 = new JLabel("      X2  ");
        x2 = new TextField(5);
        JP15.add(ex2);
        JP15.add(x2);


        //creates the label of y2 & its textfield
        JPanel JP16 = new JPanel();
        FlowLayout BL14 = new FlowLayout();
        JP16.setLayout( BL14 );
        JLabel wai2 = new JLabel("      Y2  ");
        y2 = new TextField(5);
        JP16.add(wai2);
        JP16.add(y2);

        //integrates the panels of x2&y2 into one panel
        JPanel JP17 = new JPanel();
        BorderLayout BL15 = new BorderLayout();
        JP17.setLayout( BL15 );
        JP17.add(JP15, BorderLayout.WEST);
        JP17.add(JP16, BorderLayout.EAST);

        //creates the Line button
        JPanel JP18 = new JPanel();
        FlowLayout BL16 = new FlowLayout();
        JP18.setLayout( BL16 );
        JButton line = new JButton("Line");
        line.setActionCommand("line");
        line.addActionListener(this); 
        JP18.add(line);

        //integrates all panels into one panel
        JPanel JP11 = new JPanel();
        GridLayout GL = new GridLayout(6,1);//row,column
        JP11.setLayout(GL);
        JP11.setBackground(Color.BLACK);
        JP11.add(JP5);
        JP11.add(JP8);
        JP11.add(JP9);
        JP11.add(JP14);
        JP11.add(JP17);
        JP11.add(JP18);

        JPanel pFrame = new JPanel();
        BorderLayout BL8 = new BorderLayout();
        pFrame.setLayout(BL8);
        pFrame.add(JP1, BorderLayout.WEST);
        pFrame.add(JP11, BorderLayout.EAST);
        pFrame.add(JP1);

        //adds the panel of all panels to the applet
        add(pFrame);

    }

    //Listen to the buttons' event and perform an action
    public void actionPerformed(ActionEvent evt) 
    {
        //check what button is pressed and get the input values  
        if(evt.getActionCommand().equals("rect"))
        {
            String aa = x.getText();
            int a = Integer.parseInt(aa);
            String bb = y.getText();
            int b = Integer.parseInt(bb);
            String cc = w.getText();
            int c = Integer.parseInt(cc);
            String dd = h.getText();
            int d = Integer.parseInt(dd);
            int temp = 1;
            fCanvas.Draw(a,b,c,d,temp);
            fCanvas.repaint();
        }
           else if(evt.getActionCommand().equals("oval"))
        {
            String x2 = x.getText();
            int a = Integer.parseInt(x2);
            String y2 = y.getText();
            int b = Integer.parseInt(y2);
            String width2 = w.getText();
            int c = Integer.parseInt(width2);
            String height2 = h.getText();
            int d = Integer.parseInt(height2);
            int temp = 2;
            fCanvas.Draw(a,b,c,d,temp);
            fCanvas.repaint();
        }    
        else if(evt.getActionCommand().equals("line"))
        {
            String X1 = x1.getText();
            int a = Integer.parseInt(X1);
            String Y1 = y1.getText();
            int b = Integer.parseInt(Y1);
            String X2 = x2.getText();
            int c = Integer.parseInt(X2);
            String Y2 = y2.getText();
            int d = Integer.parseInt(Y2);
            int temp = 3;
            fCanvas.Draw(a,b,c,d,temp);
            fCanvas.repaint();
        }
    }   
}

//does the drawing stuffs on the canvas
class MyCanvas extends Canvas 
{ 
    public int x1,y1,x2,y2,choice;

    //sets the size and color of the canvas
    public MyCanvas() 
    { 
        setSize(300, 300);
        setBackground(Color.CYAN);
    }

    //receives the values passed by actionPerformed method
    public void Draw(int a,int b,int c,int d,int temp)
    {
        x1=a;
        y1=b;
        x2=c;
        y2=d;
        choice=temp;
    }

    //draws the shape on the canvas according to the user's preference
    public void paint(Graphics g) 
    {
        if(choice==1)
        {
            g.drawRect(x1,y1,x2,y2);
        }
        else if(choice==2)
        {
            g.drawOval(x1,y1,x2,y2);
        }
        else if(choice==3)
        {
            g.drawLine(x1,y1,x2,y2);
        }
    }
}

:)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.