Structure of an  applet

 

public class Simple extends java.applet.Applet {

 

<APPLET> 
		[CODEBASE = applet-url]
		CODE = applet-filename
		WIDTH = pixel-width
		HEIGHT = pixel-height
		[ALT = alternate-text]
		[NAME = applet-name]
		[ALIGN = alignment]
		[VSPACE = vertical-pixel-space]
		[HSPACE = horizontal-pixel-space]
	>
	[<PARAM NAME=parameter VALUE=value>]
	[<PARAM NAME=parameter VALUE=value>]
	....

 

[alternate-html]
	</APPLET>
The following is an example of an HTML fragment that displays a text string: 
	<APPLET codebase="./textdir" code="MyTextApplet.class" width=300 height=200>
	<PARAM name=message      value="Electronic Documents Course">
	<PARAM name=font         value=Courier>
	<PARAM name=point size   value=25>
	</APPLET>
The browser will find the applet with name "MyTextApplet", download it, and run it in the browser. It will show it in a 300 by 200 window and display the string "Electronic Documents Course" in a Courier font of point size 25. 
Parameters are very handy, because they allow you to change the appearance of the applet without having to change and recompile the Java code, e.g. you can easily change the string that the MyTextApplet is displaying by changing the message parameter. 
[alternate-html]
	</APPLET>

The following is an example of an HTML fragment that displays a text string:

	<APPLET codebase="./textdir" code="MyTextApplet.class" width=300 height=200>
	<PARAM name=message      value="Electronic Documents Course">
	<PARAM name=font         value=Courier>
	<PARAM name=point size   value=25>
	</APPLET>

The browser will find the applet with name "MyTextApplet", download it, and run it in the browser. It will show it in a 300 by 200 window and display the string "Electronic Documents Course" in a Courier font of point size 25.

Parameters are very handy, because they allow you to change the appearance of the applet without having to change and recompile the Java code, e.g. you can easily change the string that the MyTextApplet is displaying by changing the message parameter.


 

 

 

import java.awt.*;
import java.applet.Applet;
public class Ovals extends Applet {
	private Font font;
	
	public void init() {
		font = new Font("Helvetica", Font.BOLD, 48);
	}
	public void paint(Graphics g) {
		g.setColor(Color.red);
		g.drawOval(10, 10, 330, 100);
		g.fillOval(10, 10, 330, 100);
		g.setColor(Color.black);
		g.setFont(font);
		g.drawString("Hello World!", 40, 75);
	}
}

 

 

Exercise 1

Write an applet that displays your name centered in a circle centered in the applet.


Exercise 2

Write an applet that displays a set of three concentric circles of different colors centered in the applet.


Exercise 3

Convert this for use in an applet.

 

          switch (N) {   // assume N is an integer variable
             case 1:
                System.out.println("The number is 1.");
                break;
             case 2:
             case 4:
             case 8:
                System.out.println("The number is 2, 4, or 8.");
                System.out.println("(That's a power of 2!)");
                break;
             case 3:
             case 6:
             case 9:
                System.out.println("The number is 3, 6, or 9.");
                System.out.println("(That's a multiple of 3!)");
                break;
             case 5:
                System.out.println("The number is 5.");
                break;
             default:
                System.out.println("The number is 7,");
                System.out.println("   or is outside the range 1 to 9.");
          }

Images

The java.awt.image package that provides classes for managing image data, including color models, cropping, color filtering, setting pixel values, and grabbing snapshots. The Image class itself is part of thejava.awt package though.

Java supports GIF and JPEG image formats. The following code fragment reads in an image called "imagename.gif" and draws the image at location (x,y) on the screen:

	Image img;
	public void init() {
		img = getImage(getDocumentBase(), "imagename.gif");
	}
	public void paint(Graphics g) {
		g.drawImage(img, x, y, this);
	}

The call to getImage, an Applet method, automatically creates an image. Thus, you need to import the java.awt.Image class.

Exercise 4

.Display the image below in the center of the applet. make the applet as big as possible

cover.gif (8432 bytes)

 

extra

Read the article on Image Processing with Java2D. Try some of the examples.