Professional Documents
Culture Documents
Coordinate system
Each (x, y) position is a pixel ("picture element").
(0, 0) is at the window's top-left corner.
x increases rightward and the y increases downward.
The rectangle from (0, 0) to (200, 100) looks like this:
(0, 0) x+
x
y+
Y Axis
(0, 0) X Axis
(200, 100)
y
(x, y)
(0, 0) X Axis
Java Coordinate Conventional
System Coordinate
System
Y Axis
Graphics methods
Method name Description
g.drawLine(x1, y1, x2, y2); line between points (x1, y1), (x2, y2)
g.drawOval(x, y, width, height); outline largest oval that fits in a box of
size width * height with top-left at (x, y)
g.drawRect(x, y, width, height); outline of rectangle of size
width * height with top-left at (x, y)
g.drawString(text, x, y); text with bottom-left at (x, y)
g.fillOval(x, y, width, height); fill largest oval that fits in a box of
size width * height with top-left at (x,
y)
g.fillRect(x, y, width, height); fill rectangle of size width * height
with top-left at (x, y)
g.setColor(Color); set Graphics to paint any following
shapes in the given color
Drawing Lines
The drawLine Method is used to draw line.
It takes two pairs of coordinates(x1,y1) and
(x2,y2) as arguments and draws a line between
them.
g.drawLine(x1,y1,x2,y2);
Drawing Rectangles
To draw rectangle a drawRect() method is
used.
It takes four arguments
first two arguments represents x and y
coordinates of top left corner of rectangle and
the remaining two represents the width and
height of rectangle.
Drawing Rectangles
drawRect(int x, int y, int w, int h);
fillRect(int x, int y, int w, int h);
(x, y) (x, y)
h h
w w
Drawing Rounded Rectangles
drawRoundRect() and fillRoundRect()
These two methods are used to draw and fill
rectangle with rounded corner.
These methods takes 6 arguments first four are
similar to drawRect( ) method.
The two extra arguments represents how much of
corners will be rounded.
Drawing Rounded Rectangles
drawRoundRect(int x, int y, int w, int h, int aw, int ah);
fillRoundRect(int x, int y, int w, int h, int aw, int ah);
(x, y)
ah/2
aw/2
w
import java.awt.*;
import java.applet.*;
public class LineRect extends Applet {
public void paint(Graphics g) {
g.drawLine(20,20,60,60);
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);
}}
Drawing Ovals and Circles
The drawOval() and fillOval()
The drawOval() and fillOval() methods are used to
draw circle and ellipse.
The drawOval() method takes four arguments fisrt
two represent the top left corners and other two
represents width and height of oval.
Drawing Ovals and Circles
drawOval(int x, int y, int w, int h);
fillOval(int x, int y, int w, int h);
(x, y)
w
import java.awt.*;
import java.applet.*;
public class Oval extends Applet{
public void paint(Graphics g){
g.drawOval(20,20,200,120);
g.setColor(Color.green);
g.fillOval(70,30,100,100);
}}
/*<applet code=Oval.class height=250 width=200>
</applet>
Drawing Arcs
The drawArc () method is used to draw arc which
takes six arguments,
first four are the same as arguments of drawOval().
and last two represent the starting angle of the arc and
the number of degrees (sweep angle) around the arc.
Java actually formulates the arc as an oval draws only
part of it.
Java Consider the 3 O’clock position as zero degree
position and degrees increase in anti-clockwise
direction.
Drawing Arcs
drawArc(int x, int y, int w, int h, int angle1, int angle2);
fillArc(int x, int y, int w, int h, int angle1, int angle2);
◼ 90
◼ 180 ◼ 0
◼ 180
import java.awt.*;
import java.applet.*;
public class Arc extends Applet{
public void paint(Graphics g){
g.drawArc(30,30,60,60,90,180);
g.fillArc(90,30,60,60,270,180);
}}