Graphics
1. The graphics class
The graphics
class is part of the java.awt.package, so if your applet does any painting (as
it usually will), make sure you import that class at the beginning of your java
file:-
Import
java.awt Graphics;
Public class
Myclass extends java.applet.Applet
{
Public.void
paint (Graphics g)
}
Drawing and
filling:- the graphics class provide a set of simple built in graphics
primitives for drawing including lines, rectangles, polygons, ovals and arcs.
LINES
To draw a
straight lines, use the
Drawline ()
method, it takes four arguments: the x and y coordinates of stating and x and y
coordinates of ending
Import
java.awt.Graphics;
Public class
Mylines extends java.applet.Applet
{
Public void
paint (Graphics g)
}
g.draw line
(23,23,75,75);
RECTANGLES
The java
graphics primitives provides not just one, but tree kinds of rectangles.
· Plain rectangles
· Rounded rectangles, which are
rectangle with rounded corners
· Three – dimensional rectangle, which
are drawn with a shaded border.
For each of these rectangles, you have two
methods to choose from, one that draws the rectangle in outline form and one
that draws the rectangle filled with color.
To draw a
plain rectangle, use either draw Rect() or fill Rect() methods. Both take four
arguments the x and y coordinates of the top-left corner of the rectangle and
te width and height of the rectangle to draw
Import
java.awt. Graphics;
Public class
Myrect extends java.applet.Applet
{
Public void
paint (Graphics g)
{
g.draw Rect
(20,20,60,60);
g.fill Rect
(120,20,60,60);
}
Rounded
rectangles are as you might expect, rectangles with rounded corners. Te draw
Round Rect () and fill Round Rect ()
To draw
rounded rectangles are similar to regular rectangles except that rounded
rectangles have two extra arguments for width and height of the angle of the
corners
g.draw Round
Rect (20,20,60,60,10,10)
g.fill Round
Rect (120,20,60,60,10,10)
finally,
there are three dimensional rectangles. These rectangles aren’t finally 3D;
instead they have a slight shadow effect the make them after
g.draw 3D
Rect (20,20,60,60,true raise);
g.draw 3D
Rect (20,20,60,60, false intented);
POLYGONS
Polygons are
shapes with an unlimited number of sides. To draw a polygon, you need a set of
x and y coordinates. The polygon is then drawn as a set of straight lines from
the first paint to the second, the second to the third and so on.
As with
rectangle, you can draw an outline or a filled polygon using draw Polygon() and
Fill polygon
Using first
way, three argument
· An array of integer representing x cords
· An array of integer representing y
cords
· An integer representing total number
of points
{
x - int eyes
= {39,94,97,142,53,58,26};
y - int whys = {33,74,36,70,108,80,106};
total points - int
pts = eyes.length
g.draw
Polygon (exes,whyes,pts);
}
Polygon
object
Polygon poly
object=new Polygon()
g.fill
Polygon Poly object
OVALS
You use
ovals to draw ellipses or cicles. Ovals are just like rectangle with overly
rounded corners. You draw them using four arguments. The x and y of the top
corner and the width and height of the oval itself. Note that because you’re
drawing an oval, the starting point is some distance to the left and up from
the actual.
The draw
Oval () fill Oval ()
{
g.draw Oval
(20,20,60,60);
g.fill Oval
(120,20,100,60);
}
ARCS
X and y
coordinates, width and height total four argumens and 2 are the degrees.
Counter
clockwise
Draw Arc ()
fill Arc ()
{
g.draw Arc
(20,20,60,60,90,180);
g.fill Arc
(120,20,60,60,90,180);
}
Comments
Post a Comment