Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Applet

i'm writing an applet which only has 2 function: click at smile button will draw a smile face and click at sleep button will display a sleep face but it not run, whenever i run it, it says that "applet not initialized" this is my code:

import java.awt.Button;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.applet.*;
import javax.swing.Button;
class Main extends Applet implements ActionListener{

Button bSmile;
Button bSleep;
int x=0;
public void init() {
Main app1=new Main();
app1.setSize(200,300);
Container contArea=new Container();
app1.add(contArea);
FlowLayout fl=new FlowLayout(FlowLayout.CENTER);
contArea.setLayout(fl);
bSmile=new Button("Smile");
bSleep=new Button("Sleep");
bSmile.addActionListener(this);
bSleep.addActionListener(this);
contArea.add(bSmile);
contArea.add(bSleep);
showStatus(getAppletInfo());
}

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Smile")
{
x=0;
repaint();
}
else if (e.getActionCommand()=="Sleep")
{
x=1;
repaint();
}

}
public void paint(Graphics g)
{
g.drawOval(100,50,100,100);
if (x==0)
{
g.drawOval(100,50,100,100);
g.drawLine(125,125,175,125);
g.drawLine(225,125,275,125);
}
else if(x==1)
{
g.drawOval(100,50,100,100);

}
else
g.drawString("Error",70,130);
}

}
[1848 byte] By [vanhelsing] at [2007-11-11 10:07:09]
# 1 Re: Applet
Usually this turns out to be because Java is not configured correctly on your machine. Do other applets work?
evlich at 2007-11-11 22:31:58 >
# 2 Re: Applet
How are you attempting to run the applet (HTML page or viewer)?
nspils at 2007-11-11 22:32:58 >
# 3 Re: Applet
I tried to run your applet. You need to make your class "public" (the Java Console reported that error when I compiled exactly what you wrote). When I added "public" before "class", and then compiled, the Applet ran.

[It felt odd to me to have a class named Main, so I renamed it MainNew and made the necessary modifications]
nspils at 2007-11-11 22:34:02 >
# 4 Re: Applet
i'm using netbean to create and run this applet. even if i use html to view, it turns out nothing :(

i feel very confusing because some program if i make my class "public" it turns out error can cannot compile unless i delete the "public" keyword

i tried to add the word "public " but it still not work

evlich, what do you mean by" Usually this turns out to be because Java is not configured correctly on your machine. " => how to know that java is not configured correctly on my machine? how to repair (i'm very amateur in those things. i'm using Version 1.5.0)
vanhelsing at 2007-11-11 22:35:01 >
# 5 Re: Applet
I forgot about another edit to your code that I had to make. The Swing library does not contain a Button class. It has JButton, though, so I changed the import statement to javax.swing.JButton.

Does your applet not run or do you get "nothing"?

I ran the applet using the java applet viewer. The applet drew a circle and two lines. No buttons were rendered. There was no functionality.

Is this what you observed?
nspils at 2007-11-11 22:36:00 >
# 6 Re: Applet
but i did add 2 button into the applet didn't i? how can it only display like that??

it turn out nothing, even i view by html, it only display a red X . when i view by netbeans, it display nothing but "applet not initialized"
vanhelsing at 2007-11-11 22:36:59 >
# 7 Re: Applet
what i want is just 2 buttons display on the screen and when i click "smile" button, it draws a smile face and when i click at "sleep" button, it displays a sleep face. don't mind about the graphic because i haven't finished it. the problem here is: it just cannot display everything but "applet not initialized" when i view by netbeans. whenever i view by html, it only displays a red X at the left top
vanhelsing at 2007-11-11 22:38:08 >
# 8 Re: Applet
I put your code into a NetBeans project, made my modifications (public, JButton), and it initialized the Applet in the AppletViewer when I ran the class. Same rendering as running the applet when I worked on the code in TextPad.
nspils at 2007-11-11 22:39:00 >
# 9 Re: Applet
it's true that my applet still run but it's only a blank applet with "applet not initialized". what is the problem that i'm having ?? :((
vanhelsing at 2007-11-11 22:40:02 >
# 10 Re: Applet
Since I have used your code and the applet generated by that code has initialized on two different computers of mine, I expect that the failure to initialize is not the fault of the code. Therefore, you step back to (1) the AppletViewer is not properly set up, (2) NetBeans is not properly set up, (3) the HTML page you've tried to use is not properly formatted to embed and run the applet, or (4) your Java installation is the problem.

Have you written any other applets or applications using your current set-up which have compiled and run as expected?
nspils at 2007-11-11 22:41:03 >
# 11 Re: Applet
Try look this code, it may help you

package test;

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class abc extends Applet implements ActionListener{

Button bSmile, bSleep;
int x=0;
public void init() {

bSmile=new Button("Happy");
bSmile.addActionListener(this);
bSleep=new Button("Unhappy");
bSleep.addActionListener(this);
add(bSmile);
add(bSleep);

}

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Happy")
{
x=0;
repaint();
}
else if (e.getActionCommand()=="Unhappy")
{
x=1;
repaint();
}

}

public void paint(Graphics g)
{
Dimension d=getSize();
int cx=d.width/2;
int cy=d.height/2;
int faceRadius=50;
int noseLength=20;
int mouthRadius=30;
int mouthAngle=50;
int eyeRadius=5;
g.drawRoundRect(2,2,d.width-5,d.height-5,20,20);
g.setColor(Color.red);
g.drawOval(cx-faceRadius,cy-faceRadius,faceRadius*2,faceRadius*2);
//draw eyes
g.setColor(Color.blue);
g.fillOval(cx-30-eyeRadius,cy-20,eyeRadius*2,eyeRadius*2);
g.fillOval(cx+30-eyeRadius,cy-20,eyeRadius*2,eyeRadius*2);
g.setColor(Color.red);
g.drawLine(cx,cy-(noseLength/2),cx,cy+(noseLength/2));
if(x==0)
g.drawArc(cx-mouthRadius,cy-mouthRadius,mouthRadius*2,mouthRadius*2,270-mouthAngle,mouthAngle*2);
else
g.drawArc(cx-mouthRadius,cy-mouthRadius+50,mouthRadius*2,mouthRadius*2,90-mouthAngle,mouthAngle*2);

}
public String getAppletInfo()
{
return "Created by Van Duc Nguyen";
}

}
vanduc at 2007-11-11 22:42:07 >
# 12 Re: Applet
i got my probblem :( it's not my code nor my program, at the begining, i chose to open a new java application file. It should be a new applet file
vanhelsing at 2007-11-11 22:43:10 >
# 13 Re: Applet
I tried to run your programme in Eclipse .
I also add " public" in front of my class .
But , there was one error , indicateds that last import in the class - import javax.swing.Button; has an error , said something " unused implementation"
I did disabled it ,result , 2 Buttons , does not appear in applet .

how we can make it run ?

further more ,
i am new to this forum .
is there any function that inform me i got answer from other members in my EMAIL ?
you can directly write to me : :WAVE:
lordrisborik@gmail.com
Tony will at 2007-11-11 22:44:11 >
# 14 Re: Applet
I developed and improve from your code, just look and run it?
vanduc at 2007-11-11 22:45:11 >
# 15 Re: Applet
i created my own code. The button's problem will be solve if you delete the Container
vanhelsing at 2007-11-11 22:46:15 >