Você está na página 1de 16

JAVA APPLET PROGRAMMES

Java Graphics Class


Usage:
The Graphics class is the basic drawing canvas for Java (1) programs.
Package:
Java.awt
Subclass of:
Java.lang.Object
Common Constructors:
Graphics is an abstract class, and cannot be constructed. A
Graphics object is passed to the Applet paint method as an
argument.
Common Methods:
clearRect (int X, int Y, int Width, int Height)
clipRect (int X, int Y, int Width, int Height)
drawArc (int X, int Y, int Width, int Height, int Theta, int
Delta)
drawImage (Image Img, int X, int Y, ImageObserver Observer)
drawImage (Image Img, int X, int Y, Color BgColor,
ImageObserver Observer)
drawImage (Image Img, int X, int Y, int Width, int Height,
ImageObserver Observer)
drawImage (Image Img, int X, int Y, int Width, int Height,
Color BgColor, ImageObserver Observer)
drawImage (Image img, int Dx1, int Dy1, int Dx2, int Dy2,
int Sx1, int Sy1, int Sx2, int sy2, ImageObserver Observer)
drawImage (Image img, int Dx1, int Dy1, int Dx2, int Dy2,
int Sx1, int Sy1, int Sx2, int sy2, Color BgColor,
ImageObserver Observer)
drawLine (int X1, int Y1, int X2, int Y2)
drawOval (int X, int Y, int Width, int Height)
drawPolygon (int[] XArray, int[] YArray, int N)
drawPolyline (int[] XArray, int[] YArray, int N)
drawRect (int X, int Y, int Width, int Height)
drawRoundRect (int X, int Y, int Width, int Height, int
OvalWidth, int OvalHeight)
drawString (java.lang.String Text, int X, int Y)
fill3DRect (int X, int Y, int Width, int Height, boolean
Raised)
fillArc (int X, int Y, int Width, int Height, int Theta, int
Delta)
fillOval (int X, int Y, int Width, int Height)
fillPolygon (int[] XArray, int[] YArray, int N)
fillRect (int X, int Y, int Width, int Height)
fillRoundRect (int X, int Y, int Width, int Height, int
OvalWidth, int OvalHeight)
setColor (Color C)
setFont (Font F)
setXORMode (Color C)
setPaintMode ()
translate (int X, int Y)

Clearing Rectangular Area :

Usage
The Graphics clearRect method can be used to clear a rectangular area.

Class
java.awt.Graphics

Arguments
Height
The vertical size of the rectangle in pixels.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.

Source Code - Clear1.java


import java.applet.Applet;
import java.awt.*;

public class Clear1 extends Applet {

public void paint (Graphics g) {


g.fillOval (20, 20, 180, 180);
g.clearRect (50, 70, 120, 80);
}

Usage
The Graphics clipRect method can be used to restrict drawing to a
rectangular area.
Class
java.awt.Graphics

Prototype
void clipRect (int X, int Y, int Width, int Height)

Arguments
Height
The vertical size of the rectangle in pixels.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.

Applet - Clip1

Source Code - Clip1.java


import java.applet.Applet;
import java.awt.*;
import java.net.*;

public class Clip1 extends Applet {


private Image Picture;

public void init () {


URL Base;
Base = getDocumentBase ();
Picture = getImage (Base, "../../../images/stache.gif");
}

public void paint (Graphics g) {


g.clipRect (80, 90, 250, 45);
g.setColor (Color.red);
g.fillRect (10, 10, 90, 100);
g.drawImage (Picture, 50, 70, 305, 85, this);
}

Drawing Arcs
Usage:
Vertical location of the upper left corner of the rectangle, in pixels. The
Graphics drawArc method can be used to draw arcs which are portions of
ellipses, and the Graphics fillArc method can be used to draw pie shapes which
are portions of filled ellipses.
Class:
Graphics
Example Methods:
drawArc (int X, int Y, int Width, int Height, int Theta, int
Delta)
fillArc (int X, int Y, int Width, int Height, int Theta, int
Delta)
Arguments:
Delta
The size of the arc, in rectangular degrees.
Height
The vertical size of the oval, in pixels.
Theta
The beginning angle of the arc, in rectangular degrees.
Width
The horizontal size of the oval, in pixels.
X
Horizontal location of the upper left corner of the bounding rectangle, in
pixels.
Y
Vertical location of the upper left corner of the bounding rectangle, in
pixels.
Example:

Code:
import java.applet.Applet;
import java.awt.*;

public class Arc1 extends Applet {

public void paint (Graphics g) {


g.drawArc (20, 30, 150, 130, 45, 90);
g.fillArc (20, 30, 150, 130, 240, 60);
}

}
Drawing Images
Usage:
An image (.jpg, .jpeg, or .gif) can be displayed using the drawImage method
after being loaded with the getImage method of the Applet.
Class:
Graphics
Example Methods:
drawImage (Image Img, int X, int Y, ImageObserver Observer)
drawImage (Image Img, int X, int Y, Color BgColor,
ImageObserver Observer)
drawImage (Image Img, int X, int Y, int Width, int Height,
ImageObserver Observer)
drawImage (Image Img, int X, int Y, int Width, int Height,
Color BgColor, ImageObserver Observer)
drawImage (Image img, int Dx1, int Dy1, int Dx2, int Dy2,
int Sx1, int Sy1, int Sx2, int sy2, ImageObserver Observer)
drawImage (Image img, int Dx1, int Dy1, int Dx2, int Dy2,
int Sx1, int Sy1, int Sx2, int sy2, Color BgColor,
ImageObserver Observer)
Arguments:
BgColor
Background color - used to fill transparent regions of the image.
Dx1
Horizontal location of the first edge of the destination rectangle, in pixels.
Dx2
Horizontal location of the second edge of the destination rectangle, in
pixels.
Dy1
Vertical location of the first edge of the destination rectangle, in pixels.
Dy2
Vertical location of the second edge of the destination rectangle, in pixels.
Height
The vertical size of the bounding rectangle in pixels.
Sx1
Horizontal location of the first edge of the source rectangle, in pixels.
Sx2
Horizontal location of the second edge of the source rectangle, in pixels.
Sy1
Vertical location of the first edge of the source rectangle, in pixels.
Sy2
Vertical location of the second edge of the source rectangle, in pixels.
Width
The horizontal size of the bounding rectangle in pixels.
X
Horizontal location of the upper left corner of the bounding rectangle, in
pixels.
Y
Vertical location of the upper left corner of the bounding rectangle, in
pixels.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
import java.net.*;

public class Image1 extends Applet {


private Image Picture;

public void init () {


URL Base;
Base = getDocumentBase ();
Picture = getImage (Base, "../../../images/stache.gif");
}

public void paint (Graphics g) {


g.drawImage (Picture, 10, 20, this);
g.drawImage (Picture, 330, 20, Color.blue, this);
g.drawImage (Picture, 10, 120, 200, 50, this);
g.drawImage (Picture, 330, 120, 200, 50, Color.blue,
this);
g.drawImage (Picture, 10, 200, 160, 250,
200, 80, 40, 10, this);
g.drawImage (Picture, 330, 200, 480, 250,
200, 80, 40, 10, Color.blue, this);
}

Usage
The Graphics drawLine method can be used to draw straight lines.

Class
java.awt.Graphics

Example Methods
void drawLine (int X1, int Y1, int X2, int Y2)

Arguments
X1
Horizontal location of one end of the line, in pixels.
X2
Horizontal location of the other end of the line, in pixels.
Y1
Vertical location of one end of the line, in pixels.
Y2
Vertical location of the other end of the line, in pixels.
Example

Code
import java.applet.Applet;
import java.awt.*;

public class Line1 extends Applet {

public void paint (Graphics g) {


g.drawLine (20, 30, 150, 130);
}

}
Usage:
The Graphics drawOval method can be used to draw ovals, and Graphics
fillOval method can be used to draw filled ovals. In both cases, the location of
the oval is defined by the upper left corner of a rectangle which bounds the oval.
Class:
Graphics
Example Methods:
drawOval (int X, int Y, int Width, int Height)
fillOval (int X, int Y, int Width, int Height)
Arguments:
Height
The vertical size of the oval, in pixels.
Width
The horizontal size of the oval, in pixels.
X
Horizontal location of the upper left corner of the bounding rectangle, in
pixels.
Y
Vertical location of the upper left corner of the bounding rectangle, in
pixels.
Example:

Code:
import java.applet.Applet;
import java.awt.*;

public class Oval1 extends Applet {

public void paint (Graphics g) {


g.drawOval (20, 30, 130, 50);
g.fillOval (20, 100, 130, 50);
}

Usage
The Graphics drawPolygon method can be used to draw outline polygons,
and the Graphics fillPolygon method can be used to draw filled polygons.

Class
java.awt.Graphics

Prototype
void drawPolygon (int[] XArray, int[] YArray, int N)
void fillPolygon (int[] XArray, int[] YArray, int N)

Arguments
XArray
Array of the horizontal locations of the vertices of the polygon, in pixels.
YArray
Array of the vertical locations of the vertices of the polygon, in pixels.
N
Number of points.

Applet - Polygon1

Source Code - Polygon1.java


import java.applet.Applet;
import java.awt.*;

public class Polygon1 extends Applet {

public void paint (Graphics g) {


int Cursor;
int[] XArray = {20, 160, 120, 160, 20, 60};
int[] YArray = {20, 20, 90, 160, 160, 90};
g.drawPolygon (XArray, YArray, 6);
Cursor = 0;
while (Cursor < 6) {
XArray [Cursor] = XArray [Cursor] + 200;
Cursor = Cursor + 1;
}
g.fillPolygon (XArray, YArray, 6);
}

}
Applet - Polygon2

Source Code - Polygon2.java

import java.applet.Applet;
import java.awt.*;

public class Polygon2 extends Applet {

public void paint (Graphics g) {


int Cursor;
int[] XArray = {20, 160, 40, 90, 130};
int[] YArray = {50, 50, 120, 20, 120};
g.drawPolygon (XArray, YArray, 5);
Cursor = 0;
while (Cursor < 5) {
XArray [Cursor] = XArray [Cursor] + 180;
Cursor = Cursor + 1;
}
g.fillPolygon (XArray, YArray, 5);
}

}
Usage:
The Graphics drawPolyline method can be used to draw polylines.
Class:
Graphics
Example Methods:
drawPolyline (int[] XArray, int[] YArray, int N)
Arguments:
XArray
Array of the horizontal locations of the vertices of the polygon, in pixels.
YArray
Array of the vertical locations of the vertices of the polygon, in pixels.
N
Number of points.
Example:

Code:
import java.applet.Applet;
import java.awt.*;

public class Polyline1 extends Applet {


public void paint (Graphics g) {
int[] XArray = {20, 160, 120, 160, 20, 60};
int[] YArray = {20, 20, 90, 160, 160, 90};
g.drawPolyline (XArray, YArray, 6);
}

Usage
The Graphics drawRect method can be used to draw outline rectangles, and
the Graphics fillRect method can be used to draw filled rectangles.

Class
java.awt.Graphics

Example Methods
void drawRect (int X, int Y, int Width, int Height)
void fillRect (int X, int Y, int Width, int Height)

Arguments
Height
The vertical size of the rectangle in pixels.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.

Example

Code
import java.applet.Applet;
import java.awt.*;

public class Rect1 extends Applet {

public void paint (Graphics g) {


g.drawRect (20, 30, 130, 50);
g.fillRect (20, 100, 130, 50);
}

}
Usage:
The Graphics drawRoundRect method can be used to draw outline rectangles
with rounded corners, and the Graphics fillRoundRect method can be used to
draw filled rectangles with rounded corners.
Class:
Graphics
Example Methods:
drawRoundRect (int X, int Y, int Width, int Height, int
OvalWidth, int OvalHeight)
fillRoundRect (int X, int Y, int Width, int Height, int
OvalWidth, int OvalHeight)
Arguments:
Height
The vertical size of the rectangle in pixels.
OvalHeight
The vertical size of the oval from which the corners are formed, in pixels.
OvalWidth
The horizontal size of the oval from which the corners are formed, in
pixels.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.
Example:

Code:
import java.applet.Applet;
import java.awt.*;

public class RoundRect1 extends Applet {

public void paint (Graphics g) {


g.drawRoundRect (20, 30, 130, 50, 40, 40);
g.fillRoundRect (20, 100, 130, 50, 60, 30);
}

}
Usage:
The Graphics fill3DRect method can be used to draw filled rectangles, which
is shaded to appear either raised or lowered.
Class:
Graphics
Example Methods:
fill3DRect (int X, int Y, int Width, int Height, boolean
Raised)
Arguments:
Height
The vertical size of the rectangle in pixels.
Raised
If true, the "button" appears raised; if false, the "button" appears lowered.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.
Example:

Code:
import java.applet.Applet;
import java.awt.*;

public class Rect3D1 extends Applet {

public void paint (Graphics g) {


g.setColor (Color.green);
g.fill3DRect (20, 20, 130, 50, true);
g.fill3DRect (170, 20, 130, 50, false);
}

}
The Graphics drawLine method can be used to print text.
Class
Graphics
Example Methods
drawString (java.lang.String Text, int X, int Y)
Arguments
Text
The text do be drawn
X
Horizontal location of the left end of the text baseline, in pixels.
Y
Vertical location of the left end of the text baseline, in pixels.
Example

Code
import java.applet.Applet;
import java.awt.*;
public class String1 extends Applet {

public void paint (Graphics g) {


g.drawString ("Hello, World", 20, 50);
}

}
Usage:
The drawing color can be set with the Graphics setColor method.
Class:
Graphics
Example Methods:
setColor (Color C)
Arguments:
C
The new rendering color.
Example:

Code:
import java.applet.Applet;
import java.awt.*;

public class Color1 extends Applet {

public void paint (Graphics g) {


g.fillRect (10, 10, 30, 80);
g.setColor (Color.red);
g.fillRect (40, 10, 30, 80);
g.setColor (Color.green);
g.fillRect (70, 10, 30, 80);
g.setColor (Color.blue);
g.fillRect (100, 10, 30, 80);
g.setColor (Color.white);
g.fillRect (130, 10, 30, 80);
}

}
Usage:
The Graphics drawLine method can be used to print text.
Class:
Graphics
Example Methods:
setFont (Font F)
Arguments:
F
A text font to be used for future text drawing.
Example:

Code:
import java.applet.Applet;
import java.awt.*;

public class Font1 extends Applet {

public void paint (Graphics g) {


Font CurrentFont;
g.drawString ("Hello, World", 20, 20);
CurrentFont = new Font ("serif", Font.PLAIN, 24);
g.setFont (CurrentFont);
g.drawString ("Hello, World", 20, 50);
CurrentFont = new Font ("monospaced", Font.ITALIC, 18);
g.setFont (CurrentFont);
g.drawString ("Hello, World", 20, 80);
}

}
Usage:
In the default drawing mode, drawing is opaque - that is, each new draw
replaces the old pixel value with the current drawing color. In the XOR mode, set
by the setXOR method, the pixel color alternates between this graphics context's
current color and the new specified color. Logical pixel operations are performed
in the XOR mode, which alternates pixels between the current color and a
specified XOR color. When drawing operations are performed, pixels which are
the current color are changed to the specified color, and vice versa. Pixels that
are of colors other than those two colors are changed in an unpredictable but
reversible manner; if the same figure is drawn twice, then all pixels are restored
to their original values. The default drawing mode can be restore with the
setPaintMode method.
Class:
Graphics
Example Methods:
setXORMode (Color C)
setPaintMode ()
Arguments:
C
The new XOR color.
Example:
Code:
import java.applet.Applet;
import java.awt.*;

public class setXOR1 extends Applet {

public void paint (Graphics g) {


g.setColor (Color.red);
g.fillRect (10, 10, 60, 60);
g.setColor (Color.green);
g.fillRect (40, 40, 60, 60);
g.setXORMode (Color.blue);
g.setColor (Color.red);
g.fillRect (110, 10, 60, 60);
g.setColor (Color.green);
g.fillRect (140, 40, 60, 60);
g.setXORMode (Color.yellow);
g.setColor (Color.red);
g.fillRect (210, 10, 60, 60);
g.setColor (Color.green);
g.fillRect (240, 40, 60, 60);
g.setPaintMode ();
g.setColor (Color.red);
g.fillRect (310, 10, 60, 60);
g.setColor (Color.green);
g.fillRect (340, 40, 60, 60);
}

}
Usage:
The Graphics translate method can be used to move the origin of a Graphics
object context.
Class:
Graphics
Example Methods:
translate (int X, int Y)
Arguments:
X
Horizontal location of the new origin in the old coordinate system, in
pixels.
Y
Vertical location of the new origin in the old coordinate system, in pixels.
Example:
Code:
import java.applet.Applet;
import java.awt.*;

public class Translate extends Applet {

public void paint (Graphics g) {


g.translate (100, 100);
g.drawLine (-90, 0, 90, 0);
g.drawLine (0, -90, 0, 90);
g.setColor (Color.blue);
g.drawOval (-50, -50, 100, 100);
}

Você também pode gostar