public class Simple extends java.applet.Applet {
... public void init() { ... } public void start() { ... } public void stop() { ... } public void destroy() { ... }
}
Following are the most important methods in the Applet class. They should provide you with enough to let you write the skeleton of your first Java 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.
To read a parameter, specified with the <PARAM> tag in the HTML file, you use the getParameter() method. The following code fragment reads the parameter with name point size from the above example (MyTextApplet):
int point_size; public void init() { String ps; ps = getParameter("point size"); if (ps == null) { point_size = 12; } else { point_size = Integer.parseInt(ps); } }
The method init() is the method that gets called first for an applet, so you do your initializations here. This includes reading of parameter tags. The getParameter() method gets the value of the point size and stores it in ps. Then ps is checked if it is null. This is done in case there is no parameter called "point size" (remember parameters are optional). If ps is null then the point size is set to a default value (here 12), else the String is "parsed" to get an Integer value.
The Graphics object is used for drawing in applets. The Graphics class is probably the most important class in the java.awt package.
The java.awt package provides an integrated set of classes to manage user interface components such as windows, dialog boxes, buttons, checkboxes, lists, menus, scrollbars, and text fields (AWT stands for Abstract Window Toolkit). The package's classes can be divided into three groups:
We will learn how the GUI components work in lecture 3. The Graphics class contains methods for basic drawing like drawLine, fillRect, drawString, and also methods for image retrieval and display. The graphics context is owned by the browser and passed to the applet in the paint() method.
Lets draw an oval around the text:
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); } }
Write an applet that displays your name centered in a circle centered in the applet.
Write an applet that displays a set of three concentric circles of different colors centered in the applet.
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."); }
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.
.Display the image below in the center of the applet. make the applet as big as possible
extra
Read the article on Image Processing with Java2D. Try some of the examples.