Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,249 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,187 people online right now. Registration is fast and FREE... Join Now!




Applet Problems

2 Pages V  1 2 >  
Reply to this topicStart new topic

Applet Problems, Can't get my applet running?

mumeisyuu
13 Oct, 2008 - 09:53 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 21

Hi, I have a problem with making a java applet.

I have coded a simple program and would like to see if I can put it on a webpage so others can try it, but after following the guides on java.sun I still get the "Applet notinited" problem. I have looked for solution on the Internet but I just don't get it.

Where could I possibly go wrong? I have even tried the example code on sun.java:

CODE

package betaTest;

import javax.swing.JApplet;
import java.awt.Graphics;

/**
*
* @author Raymond
*/
public class testApp extends JApplet {
    @Override
    public void paint(Graphics g) {
    g.drawRect(0, 0,
           getSize().width - 1,
           getSize().height - 1);
        g.drawString("Hello world!", 5, 15);
    }
}


And in the web page source I've written:
CODE

<html>
<body>
<applet code=testApp.class width="200" height="200">
    </applet>
</body>
</html>


Did I miss anything or do anything wrong?
Thanks for help > <"
User is offlineProfile CardPM
+Quote Post

Unknown Hero
RE: Applet Problems
14 Oct, 2008 - 12:52 AM
Post #2

New D.I.C Head
Group Icon

Joined: 4 Sep, 2007
Posts: 37



Thanked: 7 times
Dream Kudos: 50
My Contributions
Every applet has to have init() method.
User is offlineProfile CardPM
+Quote Post

mumeisyuu
RE: Applet Problems
14 Oct, 2008 - 08:41 AM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 21

QUOTE(Unknown Hero @ 14 Oct, 2008 - 01:52 AM) *

Every applet has to have init() method.


I've tried to add the init() method but I am not sure what to put in.
It says I can just leave the content blank like this?

CODE
public class testApp extends JApplet {
    @Override
    public void init(){
        
    }
    
    @Override
    public void paint(Graphics g) {
    g.drawRect(0, 0,
           getSize().width - 1,
           getSize().height - 1);
        g.drawString("Hello world!", 5, 15);
    }
}


When I compile and run using Netbean 6.1 (both with and without the init() method) I can get the applet work, but when I tried to use a browser to open the applet, I keep getting the "Applet XXXXX notinited" problem.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Applet Problems
14 Oct, 2008 - 07:36 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
100% sure that you have to overload the init() method even if it does nothing
as far as the start() method is concerned, don't really know what the start() method of Applet does but I have never deployed an applet without having also start() overload even if it does nothing
User is offlineProfile CardPM
+Quote Post

mumeisyuu
RE: Applet Problems
14 Oct, 2008 - 09:55 PM
Post #5

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 21

QUOTE(pbl @ 14 Oct, 2008 - 08:36 PM) *

100% sure that you have to overload the init() method even if it does nothing
as far as the start() method is concerned, don't really know what the start() method of Applet does but I have never deployed an applet without having also start() overload even if it does nothing


I don't quite get it. So what do I have to write in init()? And does it mean I have to write the start() method as well?

Below is the code I've written so far.
It works when I run and compile on Netbean, but not on browsers :S

CODE
package betaTest;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

import javax.swing.JApplet;

/**
*
* @author Raymond
*/
public class possMain extends JApplet implements ActionListener {

    private JButton b1;
    private JTextArea textArea;
    private possCalc possRes;
    private final static String newline = "\n";

    @Override
    public void init() {
        setLayout(new GridBagLayout());

        possRes = new possCalc(30);

        possChance[] possNumList = {
            new possChance(10),
            new possChance(30),
            new possChance(50),
            new possChance(70),
            new possChance(90),
        };
        //Create the combo box
        JComboBox possList = new JComboBox(possNumList);
        possList.setSelectedIndex(1);
        possList.addActionListener(this);

        //Create text area to show generation details
        textArea = new JTextArea(20, 20);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        //Create the generate button
        b1 = new JButton("Generate");
        b1.setVerticalTextPosition(AbstractButton.CENTER);
        b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales

        b1.setMnemonic(KeyEvent.VK_G);
        b1.setActionCommand("generate");

        //Listen for actions on buttons 1 and 3.
        b1.addActionListener(this);

        b1.setToolTipText("Click to start Generation.");

        //Add content to the container
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(b1, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(possList, gbc);

        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        add(scrollPane, gbc);
    }

    @Override
    public void start() {
        System.out.println("Applet starting.");
    }

    @Override
    public void stop() {
        System.out.println("Applet stopping.");
    }

    @Override
    public void destroy() {
        System.out.println("Destroy method called.");
    }

    public void actionPerformed(ActionEvent e) {
        //JComboBox cb = (JComboBox) e.getSource();
        //possChance newPoss = (possChance) cb.getSelectedItem();
        //possRes.setPossHold(newPoss.getChance());

        if ("generate".equals(e.getActionCommand())) {
            possRes.startGen();
            textArea.append("(" + possRes.getPoss() + ") " + possRes.getRes() + newline);
        } else {
            JComboBox cb = (JComboBox) e.getSource();
            possChance newPoss = (possChance) cb.getSelectedItem();
            possRes.setPossHold(newPoss.getChance());

        }
    }
}


Same problem "Applet XXXXX notinitied" ... OTZ
User is offlineProfile CardPM
+Quote Post

pbl
RE: Applet Problems
15 Oct, 2008 - 01:57 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(pbl @ 14 Oct, 2008 - 08:36 PM) *

100% sure that you have to overload the init() method even if it does nothing
as far as the start() method is concerned, don't really know what the start() method of Applet does but I have never deployed an applet without having also start() overload even if it does nothing

OK, I have checked... the init() and start() method in java.applet do nothing (both of them) so you don't really have to overload them


User is offlineProfile CardPM
+Quote Post

pbl
RE: Applet Problems
15 Oct, 2008 - 05:24 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Wouldn't bet a month of salary on that but I guess your applet as a size of (0,0)

I assume that Netbean (as it does not call the Applet from a web page) does init it with a default width and heigh which is not the case when you call it from the .html page

Your init method must at least do

CODE

public void init() {
   int width = Integer.parseInt(getParameter("width"));
   int height = Integer.parseInt(getParameter("height"));
   getContentPane.setSize(widht, height);
}


User is offlineProfile CardPM
+Quote Post

mumeisyuu
RE: Applet Problems
16 Oct, 2008 - 01:09 PM
Post #8

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 21

QUOTE(pbl @ 15 Oct, 2008 - 06:24 PM) *

Wouldn't bet a month of salary on that but I guess your applet as a size of (0,0)

I assume that Netbean (as it does not call the Applet from a web page) does init it with a default width and heigh which is not the case when you call it from the .html page

Your init method must at least do

CODE

public void init() {
   int width = Integer.parseInt(getParameter("width"));
   int height = Integer.parseInt(getParameter("height"));
   getContentPane.setSize(widht, height);
}



I have tried again today, and here are the code and results...

CODE
package betaTest;

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

/**
*
* @author Raymond
*/
public class NewAppletTest extends Applet{
    JButton button;
    
    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    @Override
    public void init() {
        setSize(200,200);
        setLayout(new BorderLayout(1,2));
        
        button = new JButton("Test Button");
        add("Center",button);
        
    }
}


I tried to keep things simple as for testing. It compiles and run with Netbean as shown below:
IPB Image

But when I tried on browser again (both IE and firefox) the applet just won't load.
IPB Image

Below is the log from java console:
IPB Image

And I honestly don't understand what it means the .class cannot be found :S


User is offlineProfile CardPM
+Quote Post

pbl
RE: Applet Problems
16 Oct, 2008 - 01:52 PM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Is your .html file and your NewAppletTest.class files in the same directory by any chance ?
If it is the case you'll have to remove

package betaTest;

from your code.

Works for me...

or you'll have to put your .class in directory betaTest and say that code=betaTest.NewAppTest.class

Don't tell me we spent all these efforts to fix your problem for a stupid directory problem :-)

This post has been edited by pbl: 16 Oct, 2008 - 08:27 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Applet Problems
16 Oct, 2008 - 08:19 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
You houu !mumeisyuu!!!
No news ? No thanks ?
User is offlineProfile CardPM
+Quote Post

mumeisyuu
RE: Applet Problems
17 Oct, 2008 - 01:13 AM
Post #11

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 21

QUOTE(pbl @ 16 Oct, 2008 - 02:52 PM) *

Is your .html file and your NewAppletTest.class files in the same directory by any chance ?
If it is the case you'll have to remove

package betaTest;

from your code.

Works for me...

or you'll have to put your .class in directory betaTest and say that code=betaTest.NewAppTest.class

Don't tell me we spent all these efforts to fix your problem for a stupid directory problem :-)


Sorry for my late reply > <

Yeah, they are in the SAME directory:
IPB Image

And I tried both removing the "package betaTest;" from the code or adding the the "betaTest.NewAppletTest.class" in the .html source~
But none works for me, still get the same problem OTZ...

Just another question, coz when I check the file in Netbean, it shows my NewAppletTest is a .java file instead of .class, could this be a reason? Or .java and .class are just purely the same?

Sorry for being so dumb and thanks for all you help > <"
Really appreciate~ <(_ _)>
User is offlineProfile CardPM
+Quote Post

pbl
RE: Applet Problems
17 Oct, 2008 - 01:54 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Your Applet is on the Web.. and it works biggrin.gif

http://www.pblinc.ca/numeisyuu/

So must be something wrong with your browsers.....

Code is also on the web page
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:04AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month