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

Chapter 5.2

The document provides an overview of graphics programming in Java, detailing various methods for drawing shapes, lines, and text using the Graphics class. It includes syntax and examples for methods such as drawRect(), fillRect(), drawOval(), and fillOval(), as well as explanations of color manipulation and font usage. Additionally, it presents sample applet programs demonstrating these graphics capabilities.

Uploaded by

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

Chapter 5.2

The document provides an overview of graphics programming in Java, detailing various methods for drawing shapes, lines, and text using the Graphics class. It includes syntax and examples for methods such as drawRect(), fillRect(), drawOval(), and fillOval(), as well as explanations of color manipulation and font usage. Additionally, it presents sample applet programs demonstrating these graphics capabilities.

Uploaded by

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

5.

2 Graphics Programming

5th Unit
Mr. R. M. Patil
SY CO, JPR
2023-24
Graphics Programming

 Graphics is one of the most important features


of Java.

 Java applets can be written to draw lines, arcs,


figures, images and text in different fonts and
styles.

 Different colors can also be incorporated in


display.
Graphics Class  W-14, S-15, W-15,S-16, W-16, S-17, W-17

1. Write the syntax and example for each of following


graphics methods:
a. drawPoly() b. drawRect()
c. drawOval() d. fillOval()

2. Write syntax and example of –


a. drawString() b. drawRect()
c. drawOval() d. drawArc()

2. Explain the following methods of applet class –


a. drawPolygon() b. drawRect()
c. drawRoundRect() d. drawArc()
Graphics Class

The graphics class defines a number of drawing


functions, Each shape can be drawn edge-only or
filled.

 To draw shapes on the screen, we may call one


of the methods available in the graphics class.
Graphics Class

Sr.No Method Description

1. clearRect() Erase a rectangular area of the canvas.


Copies a rectangular area of the canvas
2. copyAre()
to another area
3. drawArc() Draws a hollow arc
4. drawLine() Draws a straight line
5 drawOval() Draws a hollow oval
6 drawPolygon() Draws a hollow polygon
7 drawRect() Draws a hollow rectangle
Draws a hollow rectangle with rounded
8. drawRoundRect()
corners
9. drawString() Display a text string
Sr.No Method Description
10. FillArc() Draws a filled arc
11. fillOval() Draws a filled Oval
12. fillPolygon() Draws a filled Polygon
13. fillRect() Draws a filled rectangle
Draws a filled rectangle with rounded
14. fillRoundRect()
corners
15. getColor() Retrieves the current drawing color
16. getFont() Retrieves the currently used font
Retrieves the information about the
17. getFontMetrics()
current font
18. setColor() Sets the drawing color
19. setFont() Sets the font
import java.applet.Applet;
import java.awt.*;
/*
<applet code="GraphicsDemo.class" width="300" height="300"> </applet>
*/
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);
}
}
Lines
 Lines are drawn by means of the drawLine()
method.
Syntax:

void drawLine (int startX, int startY, int endX, int endY);

drawLine() -
displays a line in the current drawing color that begins at
(start X, start Y) and ends at (endX, end Y).
//Drawing Lines

import java.awt.*;
import java.applet.*;
/*
<applet code=“appl" width=300 Height=250>
</applet>
*/
public class appl extends Applet
{
public void paint(Graphics g)
{
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);
g.drawLine(40,25,250,180);
g.drawLine(5,290,80,19);
}
}
Output
:
Rectangles
The drawRect() and fillRect() methods display
an outlined and filled rectangle, respectively.
Syntax:
void drawRect(int top, int left, int width, int height);

void fillRect(int top, int left, int width, int height);

The upper-left corner of the rectangle is at (top,left).


The dimensions of the rectangle are specified by width and height.
Use drawRoundRect() or fillRoundRect() to draw a rounded
rectangle. A rounded rectangle has rounded corners.

Syntax:

void drawRoundRect(int top, int left, int width, int height, int Xdiam, int Ydiam);

void fillRoundRect(int top, int left, int width, int height, int Xdiam, int Ydiam);

The upper-left corner of the rectangle is at (top,left).


The dimensions of the rectangle are specified by width and height.
The diameter of the rounding arc along the X axis is specified by
Xdiam.
The diameter of the rounding arc along the Y axis is specified by
Ydiam.
import java.awt.*;
import java.applet.*;

/*
<applet code=“appl" width=500 Height=500>
</applet>
*/

public class appl extends Applet


{
public void paint(Graphics g)
{
g.drawRect(10,10,60,50);
g.fillRect(290,280,150,150);
g.drawRoundRect(190,10,60,50,15,15);
g.fillRoundRect(70,90,140,100,30,40);
}
}
Output
:
MSBTE Question : Design an applet program which
displays a rectangle filled with red color and message
as “Hello Third Year Students” in blue color.

S-16, 4 Marks
import java.awt.*;
import java.applet.*;

/*
<applet code=“appl" width=300 Height=300>
</applet>
*/

public class appl extends Applet


{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(50,50,200,200);

g.setColor(Color.blue);
g.drawString(“Hello Third Year Students”, 80,160);
}
}
Ellipse, Circle
 The Graphics class does not contain any method for circles or
ellipses.
 To draw an ellipse, use drawOval().
 To fill an ellipse, use fillOval().

Syntax:
void drawOval(int top, int left, int width, int height);
void fillOval(int top, int left, int width, int height);

- Both methods are used to draw a Ellipse as well as Circle.


- The ellipse is drawn within a bounding rectangle whose upper-left
corner is specified by (top,left) and whose width and height are
specified by width and height.
- To draw a circle, specify a square as the bounding rectangle i.e get
height = width.
import java.awt.*;
import java.applet.*;
/*
<applet code="Ellipses" width=300 Height=300>
</applet>
*/
public class Ellipses extends Applet
{
public void paint(Graphics g)
{
g.drawOval(10,10,60,50);
g.fillOval(100,10,75,50);
g.drawOval(190,10,30,30); //Circle
g.fillOval(70,90,140,100);
g.fillOval(110,200,40,40); //Circle
}
}
Output
:

Circle
Arcs
 An arc is a part of oval.
 Arcs can be drawn with drawArc() and fillArc() methods.

Syntax:

void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)

void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
Syntax:

void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)

void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)

 The arc is bounded by the rectangle whose upper-left corner is


specified by (top,left) and whose width and height are specified by
width and height.

 The arc is drawn from startAngle through the angular distance


specified by sweepAngle.

 Angles are specified in degree. '0o' is on the horizontal, at the 30'


clock's position.

 The arc is drawn conterclockwise if sweepAngle is positive, and


clockwise if sweepAngle is negative.
import java.awt.*;
import java.applet.*;

/*
<applet code="Arcs" width=300 Height=300>
</applet>
*/

public class Arcs extends Applet


{
public void paint(Graphics g)
{
g.drawArc(10,40,70,70,0,75);
g.fillArc(100,40,70,70,0,75);
g.drawArc(10,100,70,80,0,175);
g.fillArc(100,100,70,90,0,270);
g.drawArc(200,80,80,80,0,180);
}
}
Polygons
 Polygons are shapes with many sides.
 It 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 line is the beginning of the third line, and so on.
 Use drawPolygon() and fillPolygon() to draw arbitrarily shaped
figures.

Syntax:

void drawPolygon(int x[] , int y[], int numPoints)

void fillPolygon(int x[] , int y[], int numPoints)


Syntax:

void drawPolygon(int x[] , int y[], int numPoints)

void fillPolygon(int x[] , int y[], int numPoints)

 The polygon's endpoints are specified by the coordinate pairs


contained within the x and y arrays.
 The number of points defined by x and y is specified by
numPoints.

 It is obvious that x and y arrays should be of the same size and


we must repeat the first point at the end of the array for closing the
polygon.
import java.awt.*;
import java.applet.*;

/*
<applet code="Polygon" width=300 Height=300>
</applet>
*/

public class Polygon extends Applet


{
public void paint(Graphics g)
{
int xpoints[]={30,200,30,200,30};
int ypoints[]={30,30,200,200,30};
int num=5;
g.drawPolygon(xpoints,ypoints,num);
}
}
Output
:
MSBTE Question : Write a program to display
triangle filled with red color.

S-16, 4 Marks
import java.awt.*;
import java.applet.*;

/*
<applet code="Polygon" width=300 Height=300>
</applet>
*/

public class Polygon extends Applet


{
public void paint(Graphics g)
{
int xpoints[]={110,180,30,110};
int ypoints[]={30,100,100,30};
g.setColor(Color.red);
g.fillPolygon(xpoints,ypoints,4);
}
}
Colors
 setColor()
 getColor()
 setForeGround()
 setBackGround()

MSBTE Question - With proper syntax and example explain


following graphics methods :
1. setColor()
2. SetForeGround()

- S-18, 4 Marks
Colors
 Color can be controlled by accessing the Color class.

Available Colors
red
yellow
blue
orange
pink
cyan
magenta
black
white
gray
lightGray
darkGray
Colors

 setColor() – Sets this graphics current color to the specified


color.
Example : g.setColor(Color.red);

 getColor() – Gets this graphics contexts current color.


Example : g.setColor(Color.red);
color old = g.getColor();
g.setColor(old);
Colors

setForeGround() – the color become this components


foreground color.
Example : g.setForeground(Color.red);

 setBackGround() – Sets the background color of this


component.
Example : g.setBackground(Color.red);
MSBTE Question – Design an applet which displays three circles
one below the other and fill them red, green, yellow color
respectively.

- W-14,W-15, 4 Marks

MSBTE Question – Design an applet which displays equal size


three rectangle one below the other and fill them with orange, white
and green color respectively.

- 4 Marks
Fonts Class ( W-14 )

MSBTE Question – State the use of Font class. Write syntax to


create an object of Font Class. Describe any 3 methods of Font class
with their syntax and example of each.

- W-14, 8 Marks

 The font class states fonts, which are used to render text in a
visible way.
Variable defined by font class – Name, pointSize, size, style

Variable Meaning
1.protected String name Name of the font
2.protected int size Size of the font in points
3.protected int style Font style
4.protected float pointSize Point size of the font in float
 There are four styles for displaying fonts in Java: plain, bold, italic,
and bold italic.
 Three class constants are used to represent font styles:

 public static final int BOLD: The BOLD constant represents a


boldface font.

 public static final int ITALIC: The ITALIC constant represents an


italic font.

 public static final int PLAIN: The PLAIN constant represents a


plain or normal font.

 The combination BOLD/ITALIC represents a bold italic font.

 PLAIN combined with either BOLD or ITALIC represents bold or


italic, respectively.
 To Set a new font –

public Font (String name, int style, int size);

There is a single constructor for Font.


It requires a name, style, and size.
Name argument represents the name of the font to create.

 Example –

public void init()


{
int fsize=20;
String fname=“Arial”;
Font f = new Font(fname, Font.BOLD, fsize);
}
import java.awt.*;
import java.applet.*;
/*
<applet code=“fontexample" width=300 Height=300>
</applet>
*/
public class fontexample extends Applet
{
int fsize; String fname;
Font f;
public void init()
{
fsize=15;
fname= “Arial”;
f = new Font(fname, Font.BOLD, fsize);
}
}
public void paint(Graphics g)
{
g.drawString(“Welcome”, 20,20);

g.setFont(f);

g.drawString(“Hello Friends”, 30,30);


}

}
Font Methods – S-16, W-16, S-18

Methods Meaning
getFamily() Returns the family of the font.
Returns a font object from the system properties
getFont()
list.
getFontName() Gets the logical name of the font.
getName() The full font name.
getSize() The point size for this font.
getStyle() Returns the style of this font.
It returns an array containing a one point size
getAllFonts() instance of all fonts available in this Graphics
Environment.
getAvailableFont It returns an array containing the name of all
FamilyName() fonts families in the Graphics Environment.
import java.awt.*;
import java.applet.*;
/*
<applet code=“fontexample" width=300 Height=300>
</applet>
*/
public class fontexample extends Applet
{
public void paint(Graphics g)
{
GraphicsEnvironment e =GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontnames = e.getAvailableFontFamilyNames();
g.drawString(“Fonts available on this platform: “,20,20);
int y=20;
for (int i = 0; i < fontnames.length; i++)
{
g.drawString(fontnames[i],20,y);
y+=20;
}
}
}

You might also like