Graphics Programing Coordinate system Each x y position

















![Drawing Polygons and Polylines int[] x = {40, 70, 60, 45, 20}; int[] y Drawing Polygons and Polylines int[] x = {40, 70, 60, 45, 20}; int[] y](https://slidetodoc.com/presentation_image_h2/1de014649660042ae07a417905f150aa/image-18.jpg)








- Slides: 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+ y+ (200, 100)

Graphics methods Method name g. draw. Line(x 1, y 1, x 2, y 2); Description line between points (x 1, y 1), (x 2, y 2) g. draw. Oval(x, y, width, height); outline largest oval that fits in a box of size width * height with top-left at (x, y) g. draw. Rect(x, y, width, height); outline of rectangle of size width * height with top-left at (x, y) g. draw. String(text, x, y); text with bottom-left at (x, y) g. fill. Oval(x, y, width, height); fill largest oval that fits in a box of size width * height with top-left at (x, y) g. fill. Rect(x, y, width, height); fill rectangle of size width * height with top-left at (x, y) g. set. Color(Color); set Graphics to paint any following shapes in the given color

Drawing Lines The draw. Line Method is used to draw line. It takes two pairs of coordinates(x 1, y 1) and (x 2, y 2) as arguments and draws a line between them. g. draw. Line(x 1, y 1, x 2, y 2);

Drawing Rectangles To draw rectangle a draw. Rect() 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 draw. Rect(int x, int y, int w, int h); fill. Rect(int x, int y, int w, int h);

Drawing Rounded Rectangles draw. Round. Rect() and fill. Round. Rect() These two methods are used to draw and fill rectangle with rounded corner. These methods takes 6 arguments first four are similar to draw. Rect( ) method. The two extra arguments represents how much of corners will be rounded.

Drawing Rounded Rectangles draw. Round. Rect(int x, int y, int w, int h, int aw, int ah); fill. Round. Rect(int x, int y, int w, int h, int aw, int ah);

import java. awt. *; import java. applet. *; public class Line. Rect extends Applet { public void paint(Graphics g) { g. draw. Line(20, 60, 60); g. draw. Rect(10, 60, 40, 30); g. fill. Rect(60, 10, 30, 80); g. draw. Round. Rect(10, 100, 80, 50, 10); g. fill. Round. Rect(20, 110, 60, 30, 5, 5); g. draw. Line(100, 10, 230, 140); g. draw. Line(100, 140, 230, 10); }}

Drawing Ovals and Circles The draw. Oval() and fill. Oval() methods are used to draw circle and ellipse. The draw. Oval() method takes four arguments fisrt two represent the top left corners and other two represents width and height of oval.

Drawing Ovals and Circles draw. Oval(int x, int y, int w, int h); fill. Oval(int x, int y, int w, int h);

import java. awt. *; import java. applet. *; public class Oval extends Applet{ public void paint(Graphics g){ g. draw. Oval(20, 200, 120); g. set. Color(Color. green); g. fill. Oval(70, 30, 100); }} /*<applet code=Oval. class height=250 width=200> </applet>

Drawing Arcs The draw. Arc () method is used to draw arc which takes six arguments, first four are the same as arguments of draw. Oval(). 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 anticlockwise direction.

Drawing Arcs draw. Arc(int x, int y, int w, int h, int angle 1, int angle 2); fill. Arc(int x, int y, int w, int h, int angle 1, int angle 2); Angles are in degree n n 90 180 n n 180 0

import java. awt. *; import java. applet. *; public class Arc extends Applet{ public void paint(Graphics g){ g. draw. Arc(30, 60, 90, 180); g. fill. Arc(90, 30, 60, 270, 180); }} /*<applet code=Arc. class height=250 width=200> </applet> */

public void paint(Graphics g){ g. draw. Oval(40, 120, 150); // Head g. draw. Oval(57, 75, 30, 20); g. draw. Oval(110, 75, 30, 20); // Left Eye //Right Eye g. fill. Oval(68, 81, 10); //Pupil (left) g. fill. Oval(121, 81, 10); //Pupil (Right) g. draw. Oval(85, 100, 30); //Nose g. fill. Arc(60, 125, 80, 40, 180); //Mouth g. draw. Oval(25, 92, 15, 30); //Left ear g. draw. Oval(160, 92, 15, 30); //Right ear }

Drawing Polygons and Polylines The draw. Polygon() 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 Drawing Polygons and Polylines int[] x = {40, 70, 60, 45, 20}; int[] y](https://slidetodoc.com/presentation_image_h2/1de014649660042ae07a417905f150aa/image-18.jpg)
Drawing Polygons and Polylines int[] x = {40, 70, 60, 45, 20}; int[] y = {20, 40, 80, 45, 60}; g. draw. Polygon(x, y, x. length); g. draw. Polyline(x, y, x. length);

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. draw. Polygon(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 set. Color() is used to set color for painting current graphics object. The RGB(0, 0, 0) is Black & RGB(255, 255) Total is white. colors we can have – 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, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, or YELLOW

Using Colors Pass a Color to Graphics object's set. Color method Subsequent shapes will be drawn in the new color. g. set. Color(Color. BLACK); g. fill. Rect(10, 30, 100, 50); g. draw. Line(20, 0, 10, 30); g. set. Color(Color. RED); g. fill. Oval(60, 40, 70); set. Background Color Method The window background color will change. Color brown = new Color(192, 128, 64); set. Background(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; set. Background(Color. yellow); g. set. Color(Color. red); g. fill. Polygon(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 The effects such as italics or Bolding 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 Font. Demo extends Applet{ public void paint(Graphics g){ Font f=new Font("Times New Roman", Font. BOLD, 30); Font f 1=new Font("Serif", Font. ITALIC, 30); g. set. Font(f); g. draw. String("Using font: Times New Roman", 10, 50); g. set. Color(Color. red); g. set. Font(f 1); g. draw. String("Using font: Serif", 100); }}

Sample Programs Concentric Circle : Circle. java Color. Boxes. java Number. java Shape. java