Java Tutorial Home 2 3 4 5 6 7 8 9
Java Applets
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
javax.swing.JApplet
Applets Structure
- There is no main method.
- Two methods that are called automatically- init() and
paint()
- The init method initializes variables and objects; if you
don't have one you will inherit one from the JApplet class.
- Use paint to draw screen
- A lot of methods exist in JApplet class so the "extends"
keyword inherits everything that the class has. In the above
example JApplet is parent class and shellapplet is the
subclass so use the keyword "extends" to create inheritance.
- You have to import JApplet and java.awt.Graphics (abstract
windowing toolkit) to get Graphics to paint.
- All applets must inherit JApplet
- Use the "super" keyword in subclass to invoke method in
the superclass.
- super.paint( g ); this says uses the paint method from
JApplet in my paint class and I'm not adding anything to it.
Exampl
import
javax.swing.JApplet;
import java.awt.Graphics;
public class AnyApplet extends JApplet
{
// declare variables here
public void init( )
{
// data initialization goes here
}
public void paint( Graphics g )
{
super.paint( g );
// your code goes here
}
}
- The window gets drawn by invoking the paint class of the
applet, which calls the superclass paint which draws the
actual window you've given the size for.
- Other applet methods
-
repaint() –
repaints when the window which the applet resides
re-gains focus
-
stop() called when
applet is no longer visible. Signature: public void
stop(). Stopt is called by the browser or applet viewer
to inform this applet that it should stop its execution.
It is called when the Web page that contains this applet
has been replaced by another page, and also just before
the applet is to be destroyed. A subclass of JApplet
should override this method if it has any operation that
it wants to perform each time the Web page containing it
is no longer visible. The implementation of stop method
provided by the JApplet class does nothing.
-
start() called
when the applet is visible. The signature for the start
method is as follows: public void start(). Start is
called by the browser or applet viewer to inform this
applet that it should start its execution. It is called
after the init method and each time the applet is
revisited in a Web page. A subclass of JApplet should
override this method if it has any operation that it
wants to perform each time the Web page containing it is
visited. The implementation of start method provided by
the JApplet class does nothing.
-
destroy() when
hosting window is closed (exit). Tha signature is as
follows: public void destroy(). Destory method is called
by the browser or applet viewer to inform this applet
that it is being cultivated and that it should destroy
any resources that it has allocated. The stop method
will always be called before destroy. A subclass of
JApplet should override this method if it has any
operation that it wants to perform before it is
destroyed. The implementation of destory method provided
by the JApplet class does nothing.
Running an Applet
We need to use html code to run applets. The minimum html
required to run applet with a browser (java host) is as
follows:
<HTML>
<HEAD>
<TITLE>TitleName</TITLE>
</HEAD>
<BODY>
<APPLET>
CODE =
Classname.class
CODEBASE = . directory of class file
WIDTH = 50 width of window in pixels
HEIGHT = 50 height of window in pixels
</APPLET>
</BODY>
</HTML>
Note that in the applet tag
you include the. class bytecode file and not the .java.
Graphics Class
- Browser or appletviewer
sends a Graphics object to the paint method.
- The Graphics object represents the applet window,
current font, and current color and provides methods to
draw shapes (rectangles, triangles, circles, etc.) and
text on the window.
The Graphics Class
Coordinate System
- Never use 0,0 because the applet itself uses it for the
title bar.
Graphics class methods
- No return type so they
are all void
- Draw ()… methods draw an outlined share
- Fill ()… methods draw a solid shapeDisplaying text use
g.drawString( "string", x, y); Need to draw the strings.
- Draw a lin g.drawLine( xStart,
yStart, xEnd, yEnd );
- How do you draw a square? You need to make multiple
lines that attach at the points.
Drawing a rectangle, Use pixels for height and width and
an anchoring corner.
Drawing an oval, drawn inside an invisible rectangle.
Give rectangle coordinates.
Graphics methods, Use offsets to make your figure easier
to move resize.
import javax.swing.JApplet;
import java.awt.Graphics;
public class ShapesApplet extends JApplet
{
public void paint( Graphics g )
{
super.paint( g );
g.drawRect( 900, 40, 30, 900 );
g.fillRect( 200, 70, 80, 80 );
g.fillOval( 100, 50, 40, 100 );
g.drawOval( 100, 200, 100, 40 );
int X = 250, Y = 225;
int r = 25;
g.drawOval( X - r, Y - r, r * 2, r * 2 );
}
}
- Draw a rectangle g.drawRect( x,
y, width, height );
- Draw a solid recta ngle:
g.fillRect( x, y, width, height );
- Draw am oval: g.drawOval( x, y,
width, height );

- Draw a filled (solid) oval:
g.fillOval( x, y, width, height );

Using the Color class
-
Found in the Java.awt
-
All drawing is done
in the current color
-
Default color is
black
-
On the Graphics
object method called setColor. This takes a Color
object. Example:
g.setColor( Color.RED );
-
Many different
constants in the color class defining many preset
colors
-
Colors consist of
red, green and blue components (RGB)
-
You may create custom
colors in the Color class by passing in three
integers for red, green and blue. Example:
Color green = new Color(0,255,0);
-
Static color
Constants defined in the Color class
Color.BLACK
Color.GRAY
Color.WHITE
Color.ORANGE
Color.RED
Color.YELLOW
Color.GREEN
Color.PINK
Color.BLUE
Color.MAGENTA
Color.CYAN
Color.LIGHT_GRAY
Color.DARK_GRAY
Using the Font class
-
Also found in Java.awt
-
Works just like color, in
that color, in that the drawing of strings is done in
the current font. Example:
Font myFont = new Font(“TimesRoman”
Font.ITALIC, 28);
g.Font.setFont(myFont);
Font(string fontName, int fontStyle, int fontSize)
Example
import java.awt.*;
class FontsExample extends JApplet {
public void paint(Graphics g) {
g.setFont(new Font("TimesRoman", Font.ITALIC, 28));
g.drawString("Some Examples of Fonts", 20, 45);
g.setFont(new Font("Helvetica", Font.PLAIN, 12));
g.drawString("This is an example of plain 12pt Helvetica font", 20,
70);
g.setFont(new Font("TimesRoman", Font.PLAIN, 12));
g.drawString("This is an example of plain 12pt TimesRoman font",
20, 90);
}
}
Java Tutorial Home
1
2
3
4
5 6
7
8
9
Java Tutorial
|
Home |
Hosting | Domains
| Support |
Contacts
|
| Terms
& Condition |
Privacy Policy
|
Copyright © 2005 by
HostItWise.com Read our Copyright. All
rights reserved.
|