Professional Documents
Culture Documents
First Applet
First Applet
Applets can be run within any modern browser To run modern Java applets, old browsers need an up-to-date Java plugin appletviewer is a program that can run
Java supplies a huge library of pre-written code, ready for you to use in your programs Code is organized into classes Classes are grouped into packages One way to use this code is to import it You can import a single class, or all the classes in a package
The Applet class contains code that works with a browser to create a display window Capitalization matters!
import is a keyword java.applet is the name of the package A dot ( . ) separates the package from the class Applet is the name of the class There is a semicolon ( ; ) at the end
awt stands for Abstract Window Toolkit The java.awt package includes classes for:
Drawing lines and shapes Drawing letters Setting colors Choosing fonts
Since you may want to use many classes from the java.awt package, simply import them all:
import java.awt.*;
The asterisk, or star (*), means all classes The import directives can go in any order, but must be the first lines in your program
C and C++ have an #include directive that copies a library function into your program This makes your program bigger Javas import gives you access to the library It does not make your program bigger Its OK to use lots of include directives!
extends Applet says that our Drawing is a kind of Applet, but with added capabilities
Javas Applet just makes an empty window We are going to draw in that window
10
11
Our applet is going to have a method to paint some colored rectangles on the screen This method must be named paint paint needs to be told where on the screen it can draw
12
public says that anyone can use this method void says that it does not return a result
A Graphics (short for Graphics context) is an object that holds information about a painting
It remembers what color you are using It remembers what font you are using You can paint on it (but it doesnt remember what you have painted)
13
14
Colors
The java.awt package defines a class named Color There are 13 predefined colorshere are their fullyqualified names:
Color.PINK Color.RED Color.ORANGE Color.YELLOW Color.MAGENTA Color.GREEN Color.CYAN Color.BLUE
For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black, Color.darkGray, etc.
15
New colors
Every color is a mix of red, green, and blue You can make your own colors: new Color( red , green , blue ) Amounts range from 0 to 255 Black is (0, 0, 0), white is (255, 255, 255) We are mixing lights, not pigments Yellow is red + green, or (255, 255, 0)
16
Setting a color
To use a color, we tell our Graphics g what color we want: g.setColor(Color.RED); g will remember this color and use it for everything until we tell it some different color
17
18
Pixels
one pixel is one dot on your screen there are typically 72 to 90 pixels per inch
19
(w-1, h-1)
Java uses an (x, y) coordinate system (0, 0) is the top left corner (50, 0) is 50 pixels to the right of (0, 0) (0, 20) is 20 pixels down from (0, 0) (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height
20
Drawing rectangles
There are two ways to draw rectangles: g.drawRect( left , top , width , height );
21
g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30); g.setColor(Color.RED); g.fillRect(50, 30, 50, 30);
} }
22
g.drawLine( x1 , y1 , x2 , y2 ); g.drawOval( left , top , width , height ); g.fillOval( left , top , width , height ); g.drawRoundRect( left , top , width , height ); g.fillRoundRect( left , top , width , height ); g.drawArc( left , top , width , height , startAngle , arcAngle ); g.drawString( string , x , y );
23
You can only run an applet in an HTML page The HTML looks something like this:
<html> <body> <h1>Applet Name</h1> <applet code="Drawing.class" width="250" height="200"> </applet> </body> </html>
The End
25