Sei sulla pagina 1di 7

Object Oriented Programming

Comp 314 Applet Programming

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

Steps in Creating Applets


1. Write the code
2. Compile
3. Write the HTML document
4. Run the html document

Sample Code:

Sample 1: Using JApplet class Sample 2: Using Applet class


import javax.swing.JApplet; // import class JApplet import java.applet.*;
import java.awt.Graphics; // import class Graphics import java.awt.*;

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

JApplet/ Applet – super class/ base class


WelcomeApplet- sub class/ derived class

Methods that Applet Container calls for an applet


1. public void init()- initialize variables
2. public void start()- start execution of applet
3. public void stop()
4. public void destroy()
5. public void paint(Graphics g)

Initialization- occurs when the applet is first-loaded (or reloaded)


-might include the object it needs, setting up an initial state, loading images or fonts or setting
parameters.
Starting- occur if the applet was previously stopped
-might include starting up a thread to control the applet, sending the appropriate messages to helper
objects or in some way telling the applet to begin running
Stopping- occurs when the reader leaves the page that contains a currently running applet or you can
stop the applet by calling stop().
Destroying- enables the applet to clean up after itself just before it is freed or the browser exists.
Zhella Anne V. Nisperos 1
IIT, DMMMSU-MLUC
Object Oriented Programming
Comp 314 Applet Programming
Painting- is how an applet actually draws something on screen, be it text, a line, a colored background,
or an image.
-can occur many hundreds of times during an applet’s life cycle.
-make sure that the Graphics class (part of java.awt package) gets imported
A Simple Applet
import java.applet.*;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class WelcomeAgainApplet extends Applet


{ Font f=new Font(“Times New Roman”, Font.BOLD, 36);
public void paint( Graphics g )
{ g.setFont(f);
g.setColor(Color.red);
g.drawString( "Welcome to Java Programming!", 5, 50 );
}
}

Including an Applet Into A Webpage


<html>
<applet code=”sample.class” width=300 height=50>
</applet>
</html>

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

more applet attributes:


align = defines how the applet will be aligned on the page.

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>

GRAPHICS, FONT AND COLOR

The Graphics Class-a part of the java.awt package


- provides a set of simple built-in graphics primitives for drawing, including lines, rectangles,
polygons, ovals and arcs.

1. Lines- use drawline() method.


-four arguments:
1. x and
2. y coordinates of the starting point and
3. x and
4. y coordinates of the ending point

public void paint(Graphics g)


{
g.drawLine(25,25,75,75,);
}

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

public void paint(Graphics g)


{
g.drawRect(20,20,60,60);
g.fillRect(120,20,60,60);
}

2.2 Rounded Rectangle- use either the drawRoundRect() or fillRoundRect() methods


- six arguments:
1. x and
2. y coordinates of top left corner of the rectangle
3. the width and
Zhella Anne V. Nisperos 3
IIT, DMMMSU-MLUC
Object Oriented Programming
Comp 314 Applet Programming
4. height of rectangle
5. width of angle
6. height of angle
public void paint(Graphics g)
{
g.drawRoundRect(20,20,60,60,10,10);
g.fillRoundRect(120,20,60,60,20,20);

}
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

public void paint(Graphics g)


{
g.draw3DRect(20,20,60,60,true);
g.fill3DRect(120,20,60,60,false);

3. Polygons- shapes with unlimited sides

3.2 Use either drawPolygon() or fillPoygon()


- 3 arguments:
1. array of integers representing x coordinates
2. array of integers representing y coordinates
3. total number of points

public void paint(Graphics g)


{
int exes={39,94,47,142,53,58,26};
int whys={33,74,36,70,108,80,106};
int pts=exes.length;
g.drawPolygon(exes, whys, pts);
}

3.3 OR Use Polygon Object


1. create the Polygon object
Polygon poly= new Polygon();
2. or create polygon from a set of points using integer arrays
public void paint(Graphics g)
{
int exes={39,94,47,142,53,58,26};
int whys={33,74,36,70,108,80,106};
int pts=exes.length;
Polygon poly= new Ploygon(exes, whys, points);
Zhella Anne V. Nisperos 4
IIT, DMMMSU-MLUC
Object Oriented Programming
Comp 314 Applet Programming
}

* To append points: poly.addPoint(20,35);

public void paint(Graphics g)


{
int exes={39,94,47,142,53,58,26};
int whys={33,74,36,70,108,80,106};
int pts=exes.length;
Polygon poly= new Ploygon(exes, whys, points);
g.fillPolygon(poly);
}

4. Ovals- to draw ellipses or circles


- use either drawoval() or fillOval() methods
-4 arguments:
1. x and
2. y coordinates of top left corner of the rectangle
3. the width and
4. height of imaginary rectangle

public void paint(Graphics g)


{
g.drawOval(20,20,60,60);
g.fillOval(120,20,60,60);
}

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)

public void paint(Graphics g)


{
g.drawArcl(20,20,60,60,90,180);
g.fillArc(120,20,60,60,90,180);
}

Elliptical arc
public void paint(Graphics g)
{
g.drawArc(10,20,150,50,25,-130);
g.fillArc(10,80,150,50,25,-130);
}

Zhella Anne V. Nisperos 5


IIT, DMMMSU-MLUC
Object Oriented Programming
Comp 314 Applet Programming
COPYING AND CLEARING

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);

2. clearRect()- fills the current background of the color of the applet


- arguments same as drawRect()
TEXTS AND FONTS

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;

drawString ()- 3 arguments:


1. text
2. x and
3. y of baseline of text
import java.awt.Font;
import java.awt.Graphics;
import java.applet.Applet;

public class ManyFonts extends Applet


{
Font f=new Font (“Arial”, Font.PLAIN, 18);
Font fb=new Font (“Arial”, Font.BOLD, 18);
Font fi=new Font (“Arial”, Font.ITALIC, 18);
Font fbi=new Font (“Arial”, Font.BOLD + Font.ITALIC, 18);

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);
}

Zhella Anne V. Nisperos 6


IIT, DMMMSU-MLUC
Object Oriented Programming
Comp 314 Applet Programming
II. Colors
1. using RBG values from 0-255
Color c= new Color(140, 140, 140);
2. using decimal values from 0-1.0
Color c= new Color(0.55,0.55,0.55);
3. using the basic colors (white, black, lightGray, gray, darkGray, red,green,blue,yellow,magenta,
cyan,pink, orange)

g.setColor(color.white);

Testing and Setting the Current Colors


g.setColor(Color.white);
setBackground(Color.white); set the background and foreground of the applet
setForeground(Color.red);

Zhella Anne V. Nisperos 7


IIT, DMMMSU-MLUC

Potrebbero piacerti anche