0% found this document useful (0 votes)
30 views

Graphics Programming

Uploaded by

preetippalankar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Graphics Programming

Uploaded by

preetippalankar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Graphics Programming

-: Introduction :-

One of the most important feature of java is its ability to draw graphics.

We can write Java applets that draw lines , figures of different shapes , images and text in
different fonts and styles. We can also incorporate different colors in display
A java applet draws graphical image inside its space using the coordinate system as shown in
Fig 15.
Java’s coordinate system has the origin (0,0) in the upper-left corner. Positive x values are to
be to the right. And positive y values are to be the bottom . The values of coordinates x and y
are in pixels.
-: 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 example, 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.
For ex: the statement will draw a rectangle starting at (10 , 60) having a
width of 40 pixels and a height of 30 pixels.
g.drawRect (10 , 60 , 40 , 30) ;
We can draw a solid box by using the method fillRect ( ). This also takes
four parameters (as drawRect) corresponding to the starting point, the
width and the height of the rectangle. ;
For ex : g.fillRect (60 , 10 , 30 , 80) ;
will draw a solid rectangle starting at (60 , 10) with a width of 30 pixels and a
height of 80 pixels.
We can also draw rounded rectangles (which are rectangles
with rounded edges), using the methods drawRoundRect ( ) and
fillRoundRect ( ) .
These two methods are similar to drawRect ( ) and fillRect ( )
except that they take two extra arguments representing the
width and height of the angle of corners.
These extra parameters indicate how much of corners will be
rounded.
ex:- g.drawRoundRect (10 , 100 , 80 , 50 , 10 , 10) ;
g.fillRoundRect (20 , 110 , 60 , 30 , 5 , 5) ;
//Java program Drawing lines and rectangles
import java.awt.*;
import java.applet.*;
/* <applet code="LineRect.class" width=700 height=700> </applet> */
public class LineRect extends Applet {
public void paint (Graphics g) {
g.drawLine(10, 10, 50, 50);
g.drawRect(10, 60, 40, 30);
g.fillRect(60, 10, 30, 80);
g.drawRoundRect(10, 100, 80, 50, 10, 10);
g.fillRoundRect(20, 110, 60, 30, 5, 5);
g.drawLine(100, 10, 230, 140);
g.drawLine(100, 140, 230, 10); } }
-: 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 the oval itself.
Note that if the width and height are the same, the oval becomes a circle.
Like rectangle methods, the drawOval ( ) method draws outline of an oval,
and the fillOval ( ) method draws a solid oval.
We can draw an object using a color object as follows :
g.setColor (Color.green) ;
after setting the color, all drawing operations will occur in that color.
//Java program to demonstrate circles and Ellipse
import java.awt.*;
import java.applet.*;
/* <applet code="GCircle.class" width=700 height=700> </applet> */
public class GCircle extends Applet
{
public void paint (Graphics g)
{
g.drawOval(20, 20, 200, 200);
g.setColor(Color.green);
g.fillOval(70, 55, 100, 100);
}
}
-: Drawings Arcs :-
An arc is a part of an oval.
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.
In Drawings arcs, Java actually formulates the arc as an oval and then draws
only a part of it as dictated by the last two arguments.
We can use the fillArc ( ) method to fill the arc . Filled arcs are drawn as if they
were sections of a pie. Instead of joining the two end points, they were joined to
the centre of the oval.
//Java program to draw human face
import java.awt.*;
import java.applet.*;
/* <applet code="Face.class" width=700 height=700> </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); //Right eye
g.fillOval(68, 81, 10, 10); //Pupil (left)
g.fillOval(121, 81, 10, 10); //Pupil (right)
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); } } //Right ear
-: Drawing Polygons :-

Polygons are shapes with many sides. A polygon may be considered as


a set of lines connected together.
The end point of the first line is beginning of the second line, the end of
second line is thee beginning of the third and so on.
This suggests that we can draw a polygon with n sides using the
drawLine( ) method n times in succession.
//java program to draw polygons using drawLine ( ) method.
import java.awt.*;
import java.applet.*;
/* <applet code="Gpolygon.class" width=700 height=700> </applet> */
public class Gpolygon extends Applet
{
public void paint (Graphics g)
{
g.drawLine(10, 20, 170, 40);
g.drawLine(170, 40, 80, 140);
g.drawLine(80, 140, 10, 20);
}
}
We can draw polygons more conveniently using the
drawPolygon ( ) method of Graphics class.
This method takes 3 arguments:-
1) An array of integers containing x coordinates.
2) An array of integers containing y coordinates.
3)An array for the total number of points.
It is obvious that x and y arrays should be of the same size
and must repeat the first point at the end of the array for
closing the polygon.
We can also draw a filled polygon by using the fillPolygon ()
method.
//Java program to draw Polygon using drawPolygon ( ) and fillPolygon ( ) methods.
import java.awt.*;
import java.applet.*;
/* <applet code="Gpolygon1.class" width=700 height=700> </applet> */
public class Gpolygon1 extends Applet {
int x1 [ ]={20, 120, 220, 20};
int y1 [ ]={20, 120, 20, 20};
int n1=4;
int x2 [ ]={120, 220, 220, 120};
int y2 [ ]={120, 20, 220, 120};
int n2=4;
public void paint (Graphics g) {
g.drawPolygon (x1, y1, n1);
g.fillPolygon(x2, y2, n2); } }
Another way of calling the methods drawPolygon ( ) and fillPolygon ( ) is
to use a Polygon object.
The Polygon class enables us to treat the polygon as an object rather than
having to deal with individual arrays.
This approach involves the following steps:
1)Defining x coordinate values as an array.
2)Defining y coordinate values as an array.
3)Defining the number of points n
4)Creating a Polygon object and initializing it with the above x , y and n
values.
5)Calling the method drawPolygon ( ) or fillPolygon ( ) with the
polygon
object as arguments.
//Java program to draw polygon using Polygon class object.
import java.awt.*;
import java.applet.*;
/* <applet code="Gpolygon2.class" width=700 height=700> </applet> */
public class Gpolygon2 extends Applet
{
public void paint (Graphics g)
{
int x [ ]={20, 120, 220, 20};
int y [ ]={20, 120, 20, 20};
int n=x.length;
Polygon poly=new Polygon(x, y, n);
g.drawPolygon(poly);
}
The Polygon class is useful if we want to add points to the polygon.
For example, if we have a polygon object existing, then we can add a point to it as
follows :
Poly.addPoint (x, y) ;
import java.awt.*;
import java.applet.*;
/* <applet code="Gpolygonadd.class" width=700 height=700> </applet> */
public class Gpolygonadd extends Applet {
public void paint (Graphics g) {
Polygon poly=new Polygon( );
poly.addPoint(20 ,20);
poly.addPoint(120, 120);
poly.addPoint(220, 20);
poly.addPoint(20, 20);
g.drawPolygon(poly); } }
Here, we first create an empty polygon and then add points to it one after
another.
Finally, we call the drawPolygon ( ) method using the poly object as an
argument to complete the process of drawing the polygon.

-: Line Graphs :-
We can design applets to draw line graphs to illustrate graphically the
relationship between two variables.
//Java program to draw line graph
import java.awt.*;
import java.applet.*;
/* <applet code="LineGraph.class" width=700 height=700> </applet> */
public class LineGraph extends Applet
{
int x [ ]={0, 60, 120, 180, 240, 300, 360, 400};
int y [ ]={400, 280, 220, 140, 60, 60, 100, 220};
int n=x.length;
public void paint (Graphics g)
{
g.drawPolygon(x, y, n);
}
}
-: Using Control Loops in Applets :-
 We can use all control structures in an applet.
 It uses a for loop for drawing circles repeatedly.
import java.awt.*;
import java.applet.*;
/* <applet code="ControlLoop.class" width=700 height=700> </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.
These values may be placed in HTML file as PARAM attributes and then
used in an applet for displaying a bar chart.
The method getParameter ( ) is used to fetch the data values from the
HTML file.
Note that the method getParameter ( ) returns only string values and
therefore we use the wrapper class method parseInt ( ) to convert strings to
integer values.
//Java program to draw Bar Charts.
import java.awt.*;
import java.applet.*;
/* <applet code="BarChart.class" width=700 height=700>
<PARAM NAME="columns" VALUE="4”>
<PARAM NAME="c1" VALUE="110">
<PARAM NAME="c2" VALUE="150">
<PARAM NAME="c3" VALUE="100">
<PARAM NAME="c4" VALUE="170">
<PARAM NAME="label1" VALUE="1991">
<PARAM NAME="label2" VALUE="1992">
<PARAM NAME="label3" VALUE="1993">
<PARAM NAME="label4" VALUE="1994“> </applet> */
public class BarChart extends Applet {
int n=0;
String label [ ];
int value [ ];
public void init ( ) {
try {
n=Integer.parseInt(getParameter("columns"));
label=new String[n];
value=new int[n];

label [0]=getParameter("label1");
label [1]=getParameter("label2");
label [2]=getParameter("label3");
label [3]=getParameter("label4");
value [0]=Integer.parseInt(getParameter("c1"));
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<n;i++)
{
g.setColor(Color.red);
g.drawString(label [i], 20, i*50+30);
g.fillRect(50, i*50+10, value [i], 40);
}
}
}

You might also like