You are on page 1of 36

Topics

To draw a

String Line Rectangle ( draw and fill ) Rounded Rectangle ( draw and fill ) Oval ( draw and fill ) Circle ( draw and fill ) Polygon ( draw and fill ) Arc ( draw and fill )

Introduction

The Graphics class defines a number of drawing functions. Each shape can be drawn edge-only or filled. Objects are drawn and filled in the currently selected graphics color, which is black by default. When a graphics object is drawn that exceeds the dimensions of the window, output is automatically clipped.

String

To display strings on the screen, use drawString(); The method drawString is given three parameters between brackets. The first item is the string to be displayed on the screen. The other two numbers specify where the string is to appear.

It represents the top-left corner of the string. However, the second number actually specifies the y-co-ordinate of a line on which the text sits.

String contd...

Some characters, such as 'g' or 'y' will drop down below this imaginary line, as shown in the following example: g.drawString("This is hugely easy",40,60);

Line

Lines are drawn by means of the drawLine( ) method, shown here:

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


startX , startY endX , endY

We start with the top-left corner of the window being assigned the coordinate (0,0). As we move right across the window, the xcoordinate increases.
As we move down the window the y-coordinate increases. What are these coordinates representing? Pixels.

Rectangle

The drawRect ( ) and fi ll Rect( ) methods display an outlined and filled rectangle, respectively. void drawRect(int top, int left, int width, int height) void fillRect(int top, int left, int width, int height)

top , left

height

width

Example
import java.applet.Applet; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics page){ page.drawRect(20, 20, 150, 100); } }

The drawRect method takes in four integer values to specify the rectangle that it will draw:

the x-coordinate of the top-left corner the y-coordinate of the top-left corner the width the height.

We start with the top-left corner of the window being assigned the coordinate (0,0).

As we move right across the window, the x-coordinate increases. As we move down the window the y-coordinate increases. What are these coordinates representing? Pixels. The entire screen is divided into tiny pixels which each represent a small bit of color. Every color on a computer can be expressed as a mixture of 3 primary colors: red, green, and blue.

Since Java is object-oriented we naturally do it using a class called Color. Every object of this type stores it's own RGB values and thus represents a single color public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(20, 20, 150, 100); }

Color.black Color.blue Color.cyan Color.darkGray Color.gray Color.green Color.yellow

Color.lightGray Color.magenta Color.orange Color.pink Color.red Color.white

public void paint(Graphics page) { Color oddGreen = new Color(12, 156, 33); page.setColor(oddGreen); page.fillRect(20, 20, 150, 100); }

Color.getHSBColor(hue, saturation, brightness) The hue parameter is a decimal number between 0.0 and 1.0 which indicates the hue of the color. The saturation is a decimal number between 0.0 and 1.0 which indicates how deep the color should be. Supplying a "1" will make the color as deep as possible, and to the other extreme, supplying a "0," will take all the color out of the mixture and make it a shade of gray.

The brightness is also a decimal number between 0.0 and 1.0 which obviously indicates how bright the color should be. A 1 will make the color as light as possible and a 0 will make it very dark. For eg: setBackground(Color.getHSBColor( 0.5f, 0.8f, 1.0f ) );

import java.applet.Applet; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics page){ page.setColor(Color.red); page.fillRect(20, 20, 150, 100); } }

Output

Rounded Rectangle

To draw a rounded rectangle, use drawRoundRect( ) or fillRoundRect( ), void drawRoundRect(int top, int left, int width, int height,int arcWidth, int arcHeight) void fillRoundRect(int top, int left, int width, int height,int arcWidth, int arcHeight)

PARAMETERS

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.

drawRoundRect
import java.applet.Applet; import java.awt.*; /* <applet code = "FillRect" width=300 height=200> </applet> */ public class FillRect extends Applet { public void paint(Graphics page){ page.setColor(Color.red); page.drawRoundRect(20, 20, 150, 100,50,70); } }

Output

fillRoundRect
import java.applet.Applet; import java.awt.*; /* <applet code = "FillRect" width=300 height=200> </applet> */ public class FillRect extends Applet { public void paint(Graphics page){ page.setColor(Color.green); page.fillRoundRect(20, 20, 150, 100,50,70); } }

OVAL
import java.applet.Applet; import java.awt.*; /* <applet code = "FillRect" width=300 height=200> </applet> */ public class FillRect extends Applet { public void paint(Graphics page){ page.setColor(Color.blue); page.drawOval(40,50,100,80); page.fillOval(180,200,30,60); }

Parameters for oval


x and Y Co-ordinate Width

Height

import java.awt.*; import java.applet.*; /* <applet code="HourGlass" width=500 height=500> </applet> */ public class HourGlass extends Applet { public void paint(Graphics g) { int xpoints[] = {30, 200, 30, 200, 30}; int ypoints[] = {30, 30, 200, 200, 30}; g.drawPolygon(xpoints, ypoints,5); } }

Output
30,30 200,30

30,200

200,200

Example
import java.applet.Applet; import java.awt.*; public class Polygon2 extends Applet { public void paint (Graphics g) { int Cursor=0; int[] XArray = {20, 160, 40, 90, 130}; int[] YArray = {50, 50, 120, 20, 120}; g.drawPolygon (XArray, YArray, 5); while (Cursor++ < 5) { XArray [Cursor] = XArray [Cursor] + 180; } g.fillPolygon (XArray, YArray, 5); } }

90,20

Output 160,50

20,50

40,120

130,120

Drawing Arcs

Arcs can be drawn with drawArc( ) and fillArc( ) 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 t op ,l eft and whose width and height are specified by wid th and hei ght . The arc is drawn from st ar tAngl e through the angular distance specified by sweepAn gl e. Angles are specified in degrees. Zero degrees is on the horizontal, at the three oclock position. The arc is drawn counterclockwise if sweepAngle is positive, and clockwise if sweepAngle is negative.

drawArc()

To draw an arc, use the drawArc command. This is given six numbers. The first four specify the rectangle in which the entire oval is enclosed, even though only part of that oval is to be drawn. The last two numbers specify the starting angle and the arc angle (both in degrees).

Here's an example: drawArc(100,100,80,60,45,120);

Of course, if the start angle is specified as 0 and the ending angle as 360, then the arc drawn is a complete oval.

Example
// Draw Arcs import java.awt.*; import java.applet.*; /*<applet code="Arcs" width=300 height=200> </applet> */ public class Arcs extends Applet { public void paint(Graphics g) { g.fillArc(10, 40, 70, 70, 0, 75); g.setColor(Color.green); g.drawArc(10, 100, 70, 80, 0, 175); }

drawArc() drawLine() drawOval() drawPolygon() drawRect() drawRoundRect() drawString() fillArc() fillOval() fillPolygon() fillRect() fillRoundRect() getColor() getFont() getFontMetrics() setColor()

Draws a hollow arc. Draws a straight line. Draws a hollow oval. Draws a hollow polygon. Draws a hollow rectangle. Draws a hollow rectangle with rounded corners. Displays a text string. Draws a filled arc. Draws a filled oval. Draws a filled polygon. Draws a filled rectangle. Draws a filled rectangle with rounded corners. Retrieves the current drawing color. Retrieves the currently used font. Retrieves information about the current font. Sets the drawing color.

Circle inside a square


import java.awt.*; import java.applet.*; /* <applet code="SquareCirc" width=500 height=500> </applet> */ public class SquareCirc extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(200,300,80,80); g.setColor(Color.blue); g.fillOval(200,300,80,80); }

You might also like