CH 9
CH 9
Shah
JAVA PROGRAMMING
CHAPTER 9
GRAPHICS
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
fillArc
2
BCA SEM IV Prof. Aayushi G. Shah
JAVA PROGRAMMING
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
}
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
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;
}
}
}