Java Code
import java.awt.*;
import javax.swing.*;
public class SmileyApplet
extends JApplet
{
Smiley smiles;
JButton button;
JTextField tf;
public void init( )
{
// create Smiley object
smiles = new Smiley( );
// MUST set the bounds otherwise you won't see it!
// default is width=0 height=0!!!
smiles.setPreferredSize( new Dimension( 300,300 ) );
// create button and textfield
button = new JButton( "go!" );
tf = new JTextField(10);
// add to applet
setLayout( new FlowLayout( ) );
add( button );
add( smiles );
add( tf );
}
}
import java.awt.*;
import javax.swing.*;
public class Smiley extends JPanel
{
public void paintComponent ( Graphics g )
{
super.paintComponent( g );
g.setColor( Color.yellow ); // face
g.fillOval( 10,10, 200, 200 );
g.setColor( Color.black ); // outline of face
g.drawOval ( 10,10, 200,200 );
g.fillRect ( 50,75, 50,5 ); // left eye
g.setColor( Color.blue ); // right eye
g.fillOval( 130,50, 30,50 );
g.setColor( Color.red ); // mouth
g.fillArc( 70, 100, 100, 70, 180, 180 );
}
}
©2006 by E.S.Boese. All Rights Reserved