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

Problems in JApplet

The following code is for displaying a JApplet but when i run it, nothing comes out. Just only "build successful" no JApplet appear??? :confused:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

////////////////////////////////////////////////////////// class Assignment
public class Assignment {
//================================================== main method
public static void main(String args[]) {
JApplet app1 = new JApplet();
app1.getContentPane().add(new AssignmentPanel());
app1.setVisible(true);
}
}
//endclass Assignment

////////////////////////////////////////////////////////// class AssignmentPanel
class AssignmentPanel extends JPanel implements ActionListener
{
DrawFace1 aDrawFace= new DrawFace1();

//================================================== constructor
public AssignmentPanel()
{
//-- Create two buttons
JButton bAwake= new JButton("Awake");
JButton bSleep=new JButton("Sleep");

//-- create new JPanel and add buttons into flow layout
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(bAwake);
buttonPanel.add(bSleep);

//-- Set layout and add buttons and face
this.setLayout(new BorderLayout());
this.add(buttonPanel, BorderLayout.NORTH);
this.add(aDrawFace, BorderLayout.CENTER);

//--Add ActionListener to 2 buttons
bAwake.addActionListener(this);
bSleep.addActionListener(this);
}
//================================================== actionPerformed method

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Awake")
{
aDrawFace.rePaint1(false);
}
else if (e.getActionCommand()=="Asleep")
{
aDrawFace.rePaint1(true);
}
}
}
//endclass AssignmentPanel

////////////////////////////////////////////////////////// class DrawFace1
class DrawFace1 extends JPanel
{
boolean sleep1=true;
int faceRadius;

//================================================== constructor
public DrawFace1()
{
this.setPreferredSize(new Dimension(400, 400));
this.setBackground(Color.LIGHT_GRAY);
}

//================================================== repaint1 method
public void rePaint1(boolean x)
{
x=sleep1;
repaint();
}
//================================================== paintComponent method
public void paintComponent(Graphics g)
{
super.paintComponent(g);

Dimension d=getSize();
// @ xW: applet's width
int xW=d.width-7;
//@ yH: applet's height
int yH=d.height-6;


/**
*Make sure the face will always addjust according to the smaller
*size of the applet to fit inside the applet even if we addjust
*its width, height or both
**/
if ((xW-yH)>=0)
{faceRadius=d.height/2;}
else
{faceRadius=d.width/3;}

g.drawRoundRect(3,3,xW,yH,20,20);
g.setColor(Color.YELLOW);


if (sleep1)
{
//--Draw sleepy face
g.drawString("Z",8*xW/12,yH/3);
g.drawString("Z",9*xW/12,3*yH/12);
g.drawString("Z",10*xW/12,2*yH/12);

//--Eyes


//--Mouth

}
else
{
//--Draw awake face
g.drawString("Hello!",xW/6,yH/6);

//--Eyes


//--Mouth


}
}
}
//endclass DrawFace
[4267 byte] By [vanhelsing] at [2007-11-11 10:30:56]