Professional Documents
Culture Documents
APPLET PROGRAMMING
Java Applet- small program that runs in a web browser or an applet viewer.
Applet Containers
1. web browser- such as Internet Explorer, Mozilla Firefox, Netscape Navigator, Opera
2. applet viewer
Sample Code:
public class WelcomeApplet extends JApplet { public class WelcomeApplet extends Applet {
public void paint( Graphics g ) public void paint( Graphics g )
{ {
g.drawString( "Welcome to Java Programming!", 25, g.drawString( "Welcome to Java Programming!", 25,
25 ); 25 );
} }
} }
Inheritance
extends- keyword that indicates that class WelcomeApplet inherits existing pieces from another class
code= indicates the class file that contains the applet, including the .class extension.
width and height= are used to indicate the bouncing box of the applet
align= left The applet is placed at the left of the page, and all of the text following
that applet flows in the space to the right of that applet
align= right The applet is placed at the right of the page, and all of the text
following that applet flows in the space to the left of that applet
align= top Aligns the applet with the topmost item in the line(which may be
another applet, or an image, or the top of the text)
align= texttop Aligns the top of the applet with the top of the tallest text in the line
align=middle Aligns the middle of the applet with the middle of the baseline of the
text
align= absmiddle Aligns the middle of the applet with the middle of the largest item in the
line
align= baseline Aligns the bottom of the applet with the baseline of the text
align= bottom Same as align=baseline
align= absbottom Aligns the bottom of the applet with the lowest item in the line
hspace and vspace= used to set the amount of space, in pixels, between an applet and it’s surrounding
text.
hspace- controls the horizontal space
vspace- controls the vertical space
codebase- contains alternate pathname where classes are contained if class files are stored in other
directory.
Zhella Anne V. Nisperos 2
IIT, DMMMSU-MLUC
Object Oriented Programming
Comp 314 Applet Programming
*note: the directory containing class files should be same as directory of HTML files.
Example if class files are stored in directory called=”classes”, which is same directory as the HTML file:
<applet code=”myclass.class” codebase=”classes” width=100 height=100></applet>
2. Rectangles
3 Kinds of rectangles produced by Java:
1. Plain Rectangle
2. Rounded Rectangle
3. Three-dimentional Rectangles
* each of these rectangles have two methods: one that draws the rectangle in outline form; and one that
draws the rectangle filled with color.
2.1 The Plain Rectangle- use either the drawRect() or fillRect() methods
-four arguments:
1. x and
2. y coordinates of top left corner of the rectangle
3. the width and
4. height of rectangle
}
2.3 3D Rectangle- use either draw3DRect() or fill3DRect() methods
-5 arguments:
1. x and
2. y coordinates of top left corner of the rectangle
3. the width and
4. height of rectangle
5. Boolean value indicating whether to raise (true) or indent (false) it
5. Arc—part of an oval
- use either drawArc() or fillArc() methods
-6 arguments:
1. x and
2. y coordinates of top left corner of the rectangle
3. the width and
4. height of rectangle
5. angle at which to start arc
6. degrees on how far to draw before stopping (positive-counterclockwise)
Elliptical arc
public void paint(Graphics g)
{
g.drawArc(10,20,150,50,25,-130);
g.fillArc(10,80,150,50,25,-130);
}
1. CopyArea()
- 6 arguments:
1. x and
2. y coordinates of top left corner of the rectangle
3. the width and
4. height of rectangle
5. x and
6. y distance to which to copy the image.
g.copyArea(0,0,100,100,100,0);
I. Fonts
Font f= new Font (Font Name, Font Style, Font Size);
Font Name=”TimesRoman”,”Arial”,”Helvetica”
Font Style= Font. PLAIN, Font. BOLD, Font. ITALIC
Font Size= point size;
g.setFont(f);
g.drawString(“Hello”,10,25);
g.setFont(fb);
g.drawString(“World”,10,30);
g.setFont(fi);
g.drawString(“Welcome”,10,35);
g.setFont(fbi);
g.drawString(“to”,10,40);
g.setFont(new Font(“Helvetica”, Font.BOLD, 20));
g.drawString(“Java Programming”,10,45);
}
g.setColor(color.white);