JAVA UNIT 5 Graphics PDF
JAVA UNIT 5 Graphics PDF
<html>
<body>
<applet code="GraphicsDemo.class" width="300
" height="300">
</applet>
</body>
</html>
Lines and rectangles
• The simplest shape we can draw with the Graphics
class is a line. The drawLine ( ) method takes two pair
of coordinates, (x1, y1) and (x2, y2) as arguments and
draws a line between them
• For eg, the following statement draws a straight line
from the coordinate point (10,10) to (50,50):
g.drawLine (10,10, 50, 50);
The g is the Graphics object passed to paint( ) method
• Similarly ,we can draw a rectangle using the
drawRect( ) method. This method takes four
arguments. The first two represent the x and y
coordinates of the top-left corner of the rectangle, and
the remaining two represent the width and the height
of the rectangle
g.(drawRect (50, 80, 150, 100)
Drawing lines and rectangles
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo extends Applet {
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
Myapplet.html
<html>
<body>
<applet code="GraphicsDemo.class" width="300
" height="300">
</applet>
</body>
</html>
Circles and Ellipses
• The Graphics class does not have any method
for circles or ellipses. However, the drawOval()
method can be used to draw a circle or an
ellipse
• The drawOval( ) method takes four
arguments: the first two represent the top-left
corner of the imaginary rectangle and the
other two represent the width and height of
oval itself
Oval within an imaginary rectangle
• The drawOval() method draws outline of an oval,
and the fillOval( ) method draws a solid oval.
• The code segment shown below draws a filled
circle within an oval
public void paint (Graphics g)
{
g.drawOval (20, 20, 200, 120);
g.setColor (Color.green);
g.fillOval (70, 30, 100, 100); //This is a circle
}
Drawing Arcs
• An arc is a part of an oval. We can think of an
oval as a series of arcs that are connected
together in an orderly manner
• The drawArc( ) designed to draw arcs takes six
arguments. The first four are the same as the
arguments for drawOval( ) method and the
last two represent the starting angle of the arc
and the number of degrees(sweep angle)
around the arc
Applet for drawing human face
import java.awt.*;
import java.applet.*;
public class face extends Applet
{
public void paint(Graphics g)
{
g.drawOval(40, 40, 120, 150); //head
g.drawOval(57, 75, 30, 20); // left eye
g.drawOval(110, 75, 30, 20);
g.fillOval(68, 81, 10, 10); //pupil(left)
g.fillOval(121, 81, 10, 10);
g.drawOval(85, 100, 30, 30); //nose
g.fillArc(60, 125, 80, 40, 180, 180); //mouth
g.drawOval(25, 92, 15, 30); //left ear
g.drawOval(160, 92, 15, 30);
}
}
Output
Drawing Polygons
• Polygons are shapes with many sides.A
polygon may be considered a set of lines
connected together. The end of the first line is
the beginning of the second line, the end of
the second is the beginning of the third , and
so on
• This suggests that we can draw polygon with n
sides using the drawLine() method n times in
succession
• The code given below will draw a polygon of
three sides
public void paint (Graphics g)
{
g.drawLine (10, 20, 170, 40);
g.drawLine (170, 40, 80, 140);
g.drawLine (80, 140, 10, 20);
}
• Note that the end point of the third line is the same as the
starting point of the polygon
• We can draw polygons using drawPolygon( ) method of
Graphics class. This method takes three arguments
1. An array of integers containing x coordinates
2. An array of integers containing x coordinates
3. An integer for total number of points
Polygon using drawPolygon() method
public void paint (Graphics g)
{
int xPoints [ ] = {10, 170, 80, 10};
int yPoints [ ] = {20, 40, 140, 20};
int nPoints [ ]= xPoints.length;
g.drawPolygon (xPoints, yPoints, nPoints);
}
Line Graphs
• We can design applets to draw line graphs to
illustrate graphically the relationship between
two variables.
• Consider the table of values
X 0 60 120 180 240 300 360 400
}
Using Control Loops in Applets
• We can use all control structures in an applet.
• Program below uses a for loop for drawing circles repeatedly
import java.awt.*;
import java.applet.*;
public class ControlLoop extends Applet
{ public void paint (Graphics g)
{
for(int i=0; i<=4; i++)
{
if ((i%2)==0)
g.drawOval (120, i*60+10, 50, 50);
else
g.fillOval (120, i*60+10, 50, 50);
}
}
}
Drawing Bar Charts
• Applets can be designed to display bar
charts,which are commonly used in
comparative analysis of data.
• Consider the table below
Year 1991 1992 1993 1994
value[1] = Integer.parseInt(getParameter("c2"));
value[2] = Integer.parseInt(getParameter("c3"));
value[3] = Integer.parseInt(getParameter("c4"));
}
catch(NumberFormatException e){}
}
public void paint(Graphics g)
{
for(int i=0;i<4;i++) {
g.setColor(Color.black);
g.drawString(label[i],20,i*50+30);
g.setColor(Color.red);
g.fillRect(50,i*50+10,value[i],40);
}
}
}
HTML file for running the BarChart
applet
<html
<applet code=BarChart .class
width=300 height=250>
<param name = “columns” value = “4”>