java applet nightmare

Discussion in 'OT Technology' started by elevator, Dec 9, 2006.

  1. elevator

    elevator The tenants think it's wonderful! ヽ(´ OT Supporter

    Joined:
    Mar 25, 2005
    Messages:
    10,967
    Likes Received:
    0
    Location:
    Houston, TX
    I have been working on this little applet forever. It uses panels, button and keylisteners, but I am just trying to get it to show up now. Whenever I go to it, the applet fails to load and it gives me a nullpointerexception in the status bar. I have checked the applet code tag on the html file, made sure all my objects were assigned values other than null. I just don't know what to do. The button listener and textfield is the second phase and the first phase works fine without it. Code is below, html page: stu.cbu.edu/~dswett/P2.html

    (// on some lines to show where I moved the assignment of the objects)

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.Font;
    import java.awt.event.*;
    import java.lang.Math;
     
    class DrawingPanel extends Panel implements KeyListener {
     
            int x1=(int)(450*Math.random()), y1=(int)(450*Math.random()), x2=(int)(450*Math.random()), y2=(int)(Math.random()), n=20;    
            int w=(int)(100*Math.random()), h=(int)(100*Math.random());
            int key;        
     
            int x3[]= new int[12];
            int y3[]= new int[12];
            int w3[]= new int[12];
            int h3[]= new int[12];
     
            Font f = new Font("Courier",Font.BOLD,100);
            Font f2 = new Font("Serif",Font.BOLD,20);
     
     
            public void paint(Graphics g) {
                        setBackground(Color.yellow);
                        addKeyListener(this);
     
                        for (int i=0;i<12;i++){
                x3[i] = (int)(450*Math.random());
                y3[i] = (int)(450*Math.random());
                w3[i] = (int)(50*Math.random());
                h3[i] = (int)(50*Math.random());
                } 
     
     
                            g.setFont(f);
                if (((x2-5) < x1 && x1 < (x2+5)) && ((y2-5) < y1 && y1 < (y2+5))) {
                    g.setColor(Color.pink);
                    g.drawString("YOU WIN!", 10, 80);
                    }
                for (int i3=0;i3<12;i3++){
                    if (((x3[i3]-20) < x1 && x1 < (x3[i3]+20)) && ((y3[i3]-20) < y1 && y1 < (y3[i3]+20))) {
                    g.setColor(Color.red);
                    g. drawString("YOU LOSE!", 10, 400);
                    }
                }
                g.setColor(Color.blue);
                g.fillOval(x1, y1, w, h);
                g.setColor(Color.red);
                g.drawOval(x2, y2, w, h);
                for (int i2=0;i2<12;i2++){
                    g.drawOval(x3[i2], y3[i2], w3[i2], h3[i2]);
                }
            }
     
            public void keyTyped(KeyEvent e) {}
              public void keyPressed(KeyEvent e) {
                key = e.getKeyCode();
                if (key == 37) {
                    x1 -= 5;
                } else if (key == 39) {
                    x1 += 5;
                } else if (key == 38) {
                    y1 -= 5;
                } else if (key == 40) {
                    y1 += 5;
                }
     
                repaint();
            }
              public void keyReleased(KeyEvent e) {}
     
    }
     
     
    class UIPanel extends Panel {
     
    //    DrawingPanel d;
            SLE filename = new SLE("Filename:", 10);
            Label message = new Label();
     
        public UIPanel (DrawingPanel p) {
     
            Font f2 = new Font("Serif",Font.BOLD, 20);
            setFont(f2);
            setSize(new Dimension(300,130));
            setLayout(new GridLayout(1,2,50,10));
     
    //              filename = new SLE("Filename:", 10);
            add(filename);
     
            Button b = new Button("Label");
            b.addActionListener(new ButtonHandler(this));
            add(b);
     
    //        message = new Label();
            add(message);
        }
    }
     
     
    class ButtonHandler implements ActionListener {
        UIPanel d;
     
        public ButtonHandler(UIPanel x) {d=x;}
        public void actionPerformed (ActionEvent e)
        {
            String plabel = d.filename.getText();
            d.message.setText(plabel);
            return;
        }    
    }
     
    class SLE extends Panel {
        TextField t;
        Label l;
     
        public SLE (String s, int x) {
            t=new TextField(x);
            l=new Label ((s + ":"), Label.LEFT);
     
            setLayout (new GridLayout(1,2));
            add(l);
            add(t);
        }
     
        public String getText() {
            return t.getText();
        }
    }
     
     
    public class P2 extends Applet {
     
           UIPanel u;
        DrawingPanel d;
     
        public void init() {
     
                    SLE filename = new SLE("Filename:", 10);
            add(filename);
     
            setLayout (new BorderLayout());
     
            d = new DrawingPanel();
            add("Center",d);
     
            u = new UIPanel(d);
                add("North" , u);
        }
     
    }
    
     
  2. elevator

    elevator The tenants think it's wonderful! ヽ(´ OT Supporter

    Joined:
    Mar 25, 2005
    Messages:
    10,967
    Likes Received:
    0
    Location:
    Houston, TX
  3. elevator

    elevator The tenants think it's wonderful! ヽ(´ OT Supporter

    Joined:
    Mar 25, 2005
    Messages:
    10,967
    Likes Received:
    0
    Location:
    Houston, TX
    printstack on the error if it helps...

    :)
    java.lang.NoClassDefFoundError: SLE
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2328)
    at java.lang.Class.getConstructor0(Class.java:2640)
    at java.lang.Class.newInstance0(Class.java:321)
    at java.lang.Class.newInstance(Class.java:303)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:721)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:650)
    at sun.applet.AppletPanel.run(AppletPanel.java:324)
    at java.lang.Thread.run(Thread.java:613)
     
  4. YodaHart

    YodaHart New Member

    Joined:
    Jul 7, 2006
    Messages:
    605
    Likes Received:
    0
    What is the SLE class?
     
  5. sprite

    sprite Active Member

    Joined:
    May 20, 2003
    Messages:
    2,967
    Likes Received:
    0
    Location:
    MI, USA
    Make sure all your class files have been re-compiled / up to date.
     
  6. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    I opened your page and this showed up in the console so you don't have things set up properly:

    java.lang.NoClassDefFoundError: DrawingPanel
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.applet.AppletPanel.createApplet(Unknown Source)
    at sun.plugin.AppletViewer.createApplet(Unknown Source)
    at sun.applet.AppletPanel.runLoader(Unknown Source)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
     

Share This Page