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

JAVA UNIT 5 Graphics PDF

The document discusses graphics programming and input/output in Java. It covers managing input/output streams, the Graphics class for drawing shapes, methods for drawing lines, rectangles, ovals, arcs, and polygons. It also discusses using control loops in applets and examples for drawing line graphs and bar charts.

Uploaded by

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

JAVA UNIT 5 Graphics PDF

The document discusses graphics programming and input/output in Java. It covers managing input/output streams, the Graphics class for drawing shapes, methods for drawing lines, rectangles, ovals, arcs, and polygons. It also discusses using control loops in applets and examples for drawing line graphs and bar charts.

Uploaded by

shwetha k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

UNIT-5

Graphics Programming, Input / Output


• . ManagingInput / Output in JAVA:
Introduction, Concept of Streams, Stream
Classes, Byte StreamClasses, Character Stream
Classes, Using Streams. Other Useful I/O
Classes, Using theFile Class, Input/output
Exceptions, Creation of Files, Reading/Writing
Characters,Reading/Writing Bytes, handling
Primitive Data Types, Concatenating and
Buffering Files,Interactive Input and Output,
Other Stream Classes.
Introduction,
• One of the most important features 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
• Every applet has its own area of the screen known as
canvas, where it creates its display.The size of an
applet’s space is decided by the attributes of the
<APPLET> tag. A java applet draws graphical image
inside its space using the coordinate system as shown
in fig below
• Java’s coordinate system has the origin (0,0) in the
upper-left corner. Positive x values are to the right, and
positive y values are to the bottom. The values of
coordinates x and y are in pixels
Coordinate System of Java
The Graphics class
• Java's Graphics class includes methods for
drawing many different types of shapes,from
simple lines to polygons to text in a variety of
fonts
• To draw a shape on the screen, we may call one
of the methods available in the Graphics class
• Below are the few most commonly used drawing
methods in the Graphics class .All the drawing
methods have arguments representing end
points, corners ,or starting locations of a shape as
values in the applet’s coordinate system. To draw
a shape, we only need to use the appropriate
method with the required arguments
Drawing Methods of the Graphics
Class
Method Description

clearRect ( ) Erases a rectangular area of the canvas

copyArea ( ) Copies a rectangular area of the canvas to


another area

drawArc ( ) Draws a hollow arc

drawLine ( ) Draws a straight line

drawPolygon ( ) Draws a hollow polygon

fillArc ( ) Draws a filled arc

setColor ( ) Sets the drawing color

setFont ( ) Sets the font

fillOval ( ) Draws a filled oval


Using Methods of Graphics Class
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>
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

Y 400 280 220 140 60 60 100 220

• The variation of Y , when X is increased can be


shown graphically using the following program
Applet to draw a line graph
Import java.awt.*;
Import java.applet.*;
Public class TableGraph 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.
• 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

Turnover 110 150 100 170


(Rs crores)

• These values may be placed in an HTML file as


PARAM attributes and then used in an applet
for displaying a bar chart
import java.awt.*;
import java.applet.*;
public class BarChart extends Applet
{
int n=0;
String label[];
int value[];
public void init() {
setBackground(Color.pink);
try {
int 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<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”>

<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>
<param name=Columns value=4>
</applet>
</html>

You might also like