Java
Java
Java
Introduction
An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. There are some important differences between an applet and a standalone Java application, including the following: An applet is a Java class that extends the java.applet.Applet class. A main() method is not invoked on an applet, and an applet class will not define main(). Applets are designed to be embedded within an HTML page. When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment. The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed. Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
Examples:
Drawing Lines
import java.applet.*; import java.awt.*; public class DrawingLines extends Applet { int width, height; public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black ); } public void paint( Graphics g ) { g.setColor( Color.green ); for ( int i = 0; i < 10; ++i ) { g.drawLine( width, height, i * width / 10, 0 ); } } }
Color
import java.applet.*; import java.awt.*; public class DrawingWithColor1 extends Applet { int width, height; int N = 25; // the number of colors created Color[] spectrum; // an array of elements, each of type Color Color[] spectrum2; // another array public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black );
// Allocate the arrays; make them "N" elements long spectrum = new Color[ N ]; spectrum2 = new Color[ N ]; // Generate the colors and store them in the arrays. for ( int i = 1; i <= N; ++i ) { // The three numbers passed to the Color() constructor // are RGB components in the range [0,1]. // The casting to (float) is done so that the divisions will be // done with floating point numbers, yielding fractional quotients. // As i goes from 1 to N, this color goes from almost black to white. spectrum[ i-1 ] = new Color( i/(float)N, i/(float)N, i/(float)N ); // As i goes from 1 to N, this color goes from almost pure green to pure red. spectrum2[ i-1 ] = new Color( i/(float)N, (N-i)/(float)N, 0 ); } } public void paint( Graphics g ) { int step = 90 / N; for ( int i = 0; i < N; ++i ) { g.setColor( spectrum[ i ] ); g.fillArc( 0, 0, 2*width, 2*height, 90+i*step, step+1 ); g.setColor( spectrum2[ i ] ); g.fillArc( width/3, height/3, 4*width/3, 4*height/3, 90+i*step, step+1 ); } } }
Keyboard Input
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Keyboard1 extends Applet implements KeyListener, MouseListener { int width, height; int x, y; String s = "";
public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black ); x = width/2; y = height/2; addKeyListener( this ); addMouseListener( this ); } public void keyPressed( KeyEvent e ) { } public void keyReleased( KeyEvent e ) { } public void keyTyped( KeyEvent e ) { char c = e.getKeyChar(); if ( c != KeyEvent.CHAR_UNDEFINED ) { s = s + c; repaint(); e.consume(); } } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { } public void mouseReleased( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { x = e.getX(); y = e.getY(); s = ""; repaint(); e.consume(); } public void paint( Graphics g ) { g.setColor( Color.gray ); g.drawLine( x, y, x, y-10 ); g.drawLine( x, y, x+10, y ); g.setColor( Color.green ); g.drawString( s, x, y ); } }
Clocks
import java.applet.*; import java.awt.*; import java.util.*; import java.text.*; public class Clock1 extends Applet implements Runnable { int width, height; Thread t = null; boolean threadSuspended; int hours=0, minutes=0, seconds=0; String timeString = ""; public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black ); } public void start() { if ( t == null ) { t = new Thread( this ); t.setPriority( Thread.MIN_PRIORITY ); threadSuspended = false; t.start(); } else { if ( threadSuspended ) { threadSuspended = false; synchronized( this ) { notify(); } } } } public void stop() { threadSuspended = true; } public void run() { try { while (true) {
// Here's where the thread does some work Calendar cal = Calendar.getInstance(); hours = cal.get( Calendar.HOUR_OF_DAY ); if ( hours > 12 ) hours -= 12; minutes = cal.get( Calendar.MINUTE ); seconds = cal.get( Calendar.SECOND ); SimpleDateFormat formatter = new SimpleDateFormat( "hh:mm:ss", Locale.getDefault() ); Date date = cal.getTime(); timeString = formatter.format( date ); // Now the thread checks to see if it should suspend itself if ( threadSuspended ) { synchronized( this ) { while ( threadSuspended ) { wait(); } } } repaint(); t.sleep( 1000 ); // interval given in milliseconds } } catch (InterruptedException e) { } } void drawHand( double angle, int radius, Graphics g ) { angle -= 0.5 * Math.PI; int x = (int)( radius*Math.cos(angle) ); int y = (int)( radius*Math.sin(angle) ); g.drawLine( width/2, height/2, width/2 + x, height/2 + y ); } void drawWedge( double angle, int radius, Graphics g ) { angle -= 0.5 * Math.PI; int x = (int)( radius*Math.cos(angle) ); int y = (int)( radius*Math.sin(angle) ); angle += 2*Math.PI/3; int x2 = (int)( 5*Math.cos(angle) ); int y2 = (int)( 5*Math.sin(angle) ); angle += 2*Math.PI/3; int x3 = (int)( 5*Math.cos(angle) ); int y3 = (int)( 5*Math.sin(angle) ); g.drawLine( width/2+x2, height/2+y2, width/2 + x, height/2 + y ); g.drawLine( width/2+x3, height/2+y3, width/2 + x, height/2 + y ); g.drawLine( width/2+x2, height/2+y2, width/2 + x3, height/2 + y3 ); }
public void paint( Graphics g ) { g.setColor( Color.gray ); drawWedge( 2*Math.PI * hours / 12, width/5, g ); drawWedge( 2*Math.PI * minutes / 60, width/3, g ); drawHand( 2*Math.PI * seconds / 60, width/2, g ); g.setColor( Color.white ); g.drawString( timeString, 10, height-10 ); } }