0% found this document useful (0 votes)
3 views9 pages

CH 9

Chapter 9 of the document discusses graphics programming in Java using AWT, detailing various methods for drawing shapes such as lines, ovals, arcs, polygons, and rectangles. It provides the syntax and parameters for each method, along with example implementations for creating a smiley face, a moving car effect, and a traffic light. The chapter emphasizes the use of the Graphics class and the importance of setting colors for rendering graphics.

Uploaded by

jairajpinky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

CH 9

Chapter 9 of the document discusses graphics programming in Java using AWT, detailing various methods for drawing shapes such as lines, ovals, arcs, polygons, and rectangles. It provides the syntax and parameters for each method, along with example implementations for creating a smiley face, a moving car effect, and a traffic light. The chapter emphasizes the use of the Graphics class and the importance of setting colors for rendering graphics.

Uploaded by

jairajpinky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BCA SEM IV Prof. Aayushi G.

Shah
JAVA PROGRAMMING

CHAPTER 9
GRAPHICS

The AWT supports a rich assortment of graphics methods.


All output to a window takes place through a graphics context. A graphics
context is encapsulated by the Graphics class.

METHODS:
drawLine( ) : The drawLine( ) method has been used in the program to draw
the line in the applet. Syntax: drawLine(int X1, int Y1, int X2, int Y2);
Where, (X1,Y1) is the coordinates of starting point and (X2,Y2) is the
coordinates of ending point of the line.

drawOval( ) : The drawOval( ) method draws the oval. Here is the syntax of
the drawOval() method : g.drawOval(int X, int Y, int Width, int height);
Draws the outline of an oval. The result is a circle or ellipse that fits within the
rectangle specified by the x, y, width, and height arguments.
The oval covers an area that is width + 1 pixels wide and height + 1
pixels tall.
Parameters:
x - the x coordinate of the upper left corner of the oval to be drawn.
y - the y coordinate of the upper left corner of the oval to be drawn.
width - the width of the oval to be drawn.
height - the height of the oval to be drawn.

fillOval
public abstract void fillOval(int x, int y, int
width, int height)
Fills an oval bounded by the specified rectangle with the current color.
Parameters:
x - the x coordinate of the upper left corner of the oval to be filled.
y - the y coordinate of the upper left corner of the oval to be filled.
width - the width of the oval to be filled.
height - the height of the oval to be filled.

drawArc
public abstract void drawArc(int x, int y, int width,
int height, int startAngle, int arcAngle)
Draws the outline of a circular or elliptical arc covering the specified rectangle.

1
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING

The resulting arc begins at startAngle and extends for arcAngle


degrees, using the current color. Angles are interpreted such that 0 degrees is at
the 3 o'clock position. A positive value indicates a counter-clockwise rotation
while a negative value indicates a clockwise rotation.
The center of the arc is the center of the rectangle whose origin is (x, y) and
whose size is specified by the width and height arguments.
The resulting arc covers an area width + 1 pixels wide by height + 1
pixels tall.
The angles are specified relative to the non-square extents of the bounding
rectangle such that 45 degrees always falls on the line from the center of the
ellipse to the upper right corner of the bounding rectangle. As a result, if the
bounding rectangle is noticeably longer in one axis than the other, the angles to
the start and end of the arc segment will be skewed farther along the longer axis
of the bounds.
Parameters:
x - the x coordinate of the upper-left corner of the arc to be drawn.
y - the y coordinate of the upper-left corner of the arc to be drawn.
width - the width of the arc to be drawn.
height - the height of the arc to be drawn.
startAngle - the beginning angle.
arcAngle - the angular extent of the arc, relative to the start angle.

fillArc

public abstract void fillArc(int x, int y, int width,


int height, int startAngle, int arcAngle)
Fills a circular or elliptical arc covering the specified rectangle.
The resulting arc begins at startAngle and extends for arcAngle
degrees. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A
positive value indicates a counter-clockwise rotation while a negative value
indicates a clockwise rotation.
The center of the arc is the center of the rectangle whose origin is (x, y) and
whose size is specified by the width and height arguments.
The resulting arc covers an area width + 1 pixels wide by height + 1
pixels tall.
The angles are specified relative to the non-square extents of the bounding
rectangle such that 45 degrees always falls on the line from the center of the
ellipse to the upper right corner of the bounding rectangle. As a result, if the
bounding rectangle is noticeably longer in one axis than the other, the angles to
the start and end of the arc segment will be skewed farther along the longer axis
of the bounds.
Parameters:

2
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING

x - the x coordinate of the upper-left corner of the arc to be filled.


y - the y coordinate of the upper-left corner of the arc to be filled.
width - the width of the arc to be filled.
height - the height of the arc to be filled.
startAngle - the beginning angle.
arcAngle - the angular extent of the arc, relative to the start angle.

drawPolygon
public abstract void drawPolygon(int[] xPoints,
int[] yPoints,
int nPoints)
Draws a closed polygon defined by arrays of x and y coordinates. Each pair of
(x, y) coordinates defines a point.
This method draws the polygon defined by nPoint line segments, where the
first nPoint - 1 line segments are line segments from (xPoints[i -
1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ≤ i ≤
nPoints. The figure is automatically closed by drawing a line connecting the
final point to the first point, if those points are different.
Parameters:
xPoints - a an array of x coordinates.
yPoints - a an array of y coordinates.
nPoints - a the total number of points.

fillPolygon
public abstract void fillPolygon(int[] xPoints, int[]
yPoints, int nPoints)
Fills a closed polygon defined by arrays of x and y coordinates.
This method draws the polygon defined by nPoint line segments, where the
first nPoint - 1 line segments are line segments from (xPoints[i -
1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ≤ i ≤
nPoints. The figure is automatically closed by drawing a line connecting the
final point to the first point, if those points are different.
The area inside the polygon is defined using an even-odd fill rule, also known
as the alternating rule.
Parameters:
xPoints - a an array of x coordinates.
yPoints - a an array of y coordinates.
nPoints - a the total number of points.

drawRect

3
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING
public void drawRect(int x, int y, int width, int
height)
Draws the outline of the specified rectangle. The left and right edges of the
rectangle are at x and x + width. The top and bottom edges are at y and y
+ height. The rectangle is drawn using the graphics context's current color.
Parameters:
x - the x coordinate of the rectangle to be drawn.
y - the y coordinate of the rectangle to be drawn.
width - the width of the rectangle to be drawn.
height - the height of the rectangle to be drawn.

drawRoundRect
public abstract void drawRoundRect(int x, int y, int
width,
int height, int arcWidth, int arcHeight)
Draws an outlined round-cornered rectangle using this graphics context's
current color. The left and right edges of the rectangle are at x and x +
width, respectively. The top and bottom edges of the rectangle are at y and y
+ height.
Parameters:
x - the x coordinate of the rectangle to be drawn.
y - the y coordinate of the rectangle to be drawn.
width - the width of the rectangle to be drawn.
height - the height of the rectangle to be drawn.
arcWidth - the horizontal diameter of the arc at the four corners.
arcHeight - the vertical diameter of the arc at the four corners.

fillRoundRect
public abstract void fillRoundRect(int x, int y, int
width, int height, int arcWidth, int arcHeight)
Fills the specified rounded corner rectangle with the current color. The left and
right edges of the rectangle are at x and x + width - 1, respectively. The
top and bottom edges of the rectangle are at y and y + height - 1.
Parameters:
x - the x coordinate of the rectangle to be filled.
y - the y coordinate of the rectangle to be filled.
width - the width of the rectangle to be filled.
height - the height of the rectangle to be filled.
arcWidth - the horizontal diameter of the arc at the four corners.
arcHeight - the vertical diameter of the arc at the four corners.

4
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING

setColor
public abstract void setColor(Color c)
Sets this graphics context's current color to the specified color. All subsequent
graphics operations using this graphics context use this specified color.
Parameters:
c - the new rendering color.

Example
1) Smiley
public class displayGraphics extends Canvas
{
public static void main(String[] args) {
displayGraphics m=new displayGraphics();
JFrame f=new JFrame();
f.add(m);
f.setSize(400,500);
f.setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.YELLOW);
g.fillOval(10, 10, 200, 200);
// draw Eyes
g.setColor(Color.BLACK);
g.fillOval(55, 65, 30, 30);
g.fillOval(135, 65, 30, 30);
// draw Mouth
g.fillOval(50, 110, 120, 60);
// adding smile
g.setColor(Color.YELLOW);
g.fillRect(50, 110, 120, 30);
g.fillOval(50, 120, 120, 40);
}
}

5
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING

2) A moving car effect


public class Image_disp extends Canvas implements Runnable
{
Thread t;
int x=0,y=90;
public static void main(String[] args)
{
Image_disp i =new Image_disp();
JFrame f=new JFrame();
f.add(i);
f.setSize(400,400);
f.setVisible(true);
i.th_start();
}
public void th_start()
{
t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
x=x+5;
repaint();
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{

}
if(x>400)
x=0;

6
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING

}
}
public void paint(Graphics g)
{
Toolkit t=Toolkit.getDefaultToolkit();
Image i=t.getImage("D:\\images\\Car.png");
g.drawImage(i, x,y,100,150,this);
}
}

3) Traffic Light

public class Traffic_light extends Canvas implements Runnable


{
Thread t;
int r,g1,y,i;

public static void main(String[] args)


{
Traffic_light tl=new Traffic_light();
JFrame f=new JFrame();
f.add(tl);
f.setSize(400,500);
f.setVisible(true);
tl.th_start();
}
public void th_start()
{
t=new Thread(this);
t.start();
r=0; g1=0;i=0; y=0;
}
public void run()
{
while(true)
{
try
{
if(i==3)
i=0;
if(i==0)
{
r=1;y=0;g1=0;

7
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING

repaint();
Thread.sleep(500);
}
if(i==1)
{
r=0;y=1;g1=0;
repaint();
Thread.sleep(500);
}
if(i==2)
{
r=0;y=0;g1=1;
repaint();
Thread.sleep(500);
}
i++;
}
catch(InterruptedException e)
{
System.out.println(e);
}
}

}
public void paint(Graphics g)
{
g.drawRect(100,100,100,300);
if (r==1)
{
g.setColor(Color.red);
g.fillOval(100,100,100,100);
g.setColor(Color.black);
g.drawOval(100,200,100,100);
g.drawOval(100,300,100,100);
r=0;
}
if (y==1)
{
g.setColor(Color.black);
g.drawOval(100,100,100,100);
g.setColor(Color.yellow);
g.drawOval(100,300,100,100);
g.setColor(Color.black);

8
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING

g.fillOval(100,200,100,100);
y=0;
}
if (g1==1)
{
g.setColor(Color.black);
g.drawOval(100,100,100,100);
g.drawOval(100,200,100,100);
g.setColor(Color.green);
g.fillOval(100,300,100,100);
g1=0;
}
}
}

You might also like