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

Appletgraphics

The document discusses graphics programming in Java. It describes the Java coordinate system and various graphics methods for drawing lines, rectangles, ovals, arcs, polygons, and text. It also covers using colors and fonts. Example code is provided to demonstrate different graphics concepts.

Uploaded by

tejasnarwade2k5
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)
19 views

Appletgraphics

The document discusses graphics programming in Java. It describes the Java coordinate system and various graphics methods for drawing lines, rectangles, ovals, arcs, polygons, and text. It also covers using colors and fonts. Example code is provided to demonstrate different graphics concepts.

Uploaded by

tejasnarwade2k5
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/ 26

Graphics Programing

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)
(200,X100)
Axis

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);

Angles are in degree

 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);
}}

/*<applet code=Arc.class height=250 width=200>


</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 and Polylines
 The drawPolygon() method takes three
arguments
 An array of integers containing x coordinates
 An array of integers containing y coordinates
 An integer for the total number of points.
 It is obvious that x and y arrays be of same size and
we must repeat the first point at the end of array for
closing polygon.
Drawing Polygons and Polylines
int[] x = {40, 70, 60, 45, 20};
int[] y = {20, 40, 80, 45, 60};
g.drawPolygon(x,y, x.length); g.drawPolyline(x, y, x.length);

(x[0], y[0]) (x[0], y[0])


(x[1], y[1]) (x[1], y[1])
(x[3], y[3]) (x[3], y[3])

(x[4], y[4]) (x[4], y[4])


(x[2], y[2]) (x[2], y[2])
import java.awt.*;
import java.applet.*;
public class Poly extends Applet{
public void paint(Graphics g){
int x[]={10,170,80,10};
int y[]={20,40,140,20};
int a=x.length;
g.drawPolygon(x,y,a);
}}
/*<applet code=Poly.class height=250 width=200>
</applet>
*/
The Color Class
 Colors are represented as instance of the class Color.
 The Color object is created with a specific RGB value.
 The setColor() is used to set color for painting current
graphics object.
 The RGB(0,0,0) is Black &
 RGB(255,255,255) is white.
 Total colors we can have – 256*256*256=224
Color
 Create new color object using Red-Green-Blue (RGB) values
Color name = new Color(red, green, blue);
 Example:
Color brown = new Color(192, 128, 64);
 Or use a predefined Color class constant (more common)
Color.CONSTANT_NAME

where CONSTANT_NAME is one of:


BLACK,BLUE, CYAN, DARK_GRAY, GRAY, GREEN,
LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED,
WHITE, or YELLOW
Using Colors
 Pass a Color to Graphics object's setColor method
 Subsequent shapes will be drawn in the new color.
g.setColor(Color.BLACK);
g.fillRect(10, 30, 100, 50);
g.drawLine(20, 0, 10, 30);
g.setColor(Color.RED);
g.fillOval(60, 40, 40, 70);
 setBackground Color Method
 The window background color will change.
Color brown = new Color(192, 128, 64);
setBackground(brown);
import java.awt.*;
import java.applet.*;
public class Poly extends Applet {
public void paint(Graphics g){
int x[]={10,170,80,10};
int y[]={20,40,140,20};
int a=x.length;
setBackground(Color.yellow);
g.setColor(Color.red);
g.fillPolygon(x,y,a);
}}
/*<applet code=Poly.class height=250 width=200>
</applet> */
Fonts Class
 Font class is used to create Font Object to set the font
for drawing text, labels etc.
 In java a font is an instance of typeface that specifies:
 The typeface used to display the font.
 Special effects such as italics or Bolding
 The size of the font.
Syntax :
Font f= new Font( name, style, size);
Example:
Font f=new Font(“Arial”, Font.Bold,30);
import java.awt.*;
import java.applet.*;
public class FontDemo extends Applet{
public void paint(Graphics g){
Font f=new Font("Times New Roman", Font.BOLD,30);
Font f1=new Font("Serif",Font.ITALIC,30);
g.setFont(f);
g.drawString("Using font:Times New Roman",10,50);
g.setColor(Color.red);
g.setFont(f1);
g.drawString("Using font: Serif",10,100);
}}
Sample Programs
 Concentric Circle : Circle.java
 ColorBoxes.java
 Number.java
 Shape.java

You might also like