Java Code



import java.awt.*;
import javax.swing.*;
public class Smiley2 extends JPanel
{
	Color faceColor;
	int width, height;
	public Smiley2( int w, int h )
	{
		width = w;
		height = h;
		setPreferredSize( new Dimension( width, height ) );
	}
	public void paintComponent ( Graphics g )
	{
		super.paintComponent( g );
		g.setColor( Color.yellow );	// face
		g.fillOval( 0,0, width, height );

		g.setColor( Color.black );	// outline of face
		g.drawOval ( 0,0, width,height );
		
		g.fillRect ( width/4,height/3, width/5,6 ); 	// left eye
		g.setColor( Color.blue );	// right eye
		g.fillOval(  width-width/3, height/4, width/10, height/6 );
		
		g.setColor( Color.red ); 	// mouth
	      	g.fillArc( width/4, height/2, width/2, height/3, 180, 180 );
      }
	public void setColor( Color newColor )
	{
		faceColor = newColor;
	}
}


import java.awt.*; import javax.swing.*; public class SmileyApplet2 extends JApplet { Smiley2 smiles, happy; JButton button; JTextField tf; public void init( ) { // create Smiley object smiles = new Smiley2( 200, 200 ); smiles.setColor( Color.MAGENTA ); happy = new Smiley2( 400, 100 ); happy.setColor( Color.GREEN ); // setPreferredSize accomplished in Smiley2 class so no need to do it here! // create button and textfield button = new JButton( "go!" ); tf = new JTextField(10); // add to applet setLayout( new FlowLayout( ) ); add( button ); add( smiles ); add( tf ); } }

Return to Code Examples
©2007 E.S.Boese