Java session14

9
Applet & Graphics Programming Jigar Jobanputra

Transcript of Java session14

Page 1: Java session14

Applet & Graphics Programming

Jigar Jobanputra

Page 2: Java session14

Methods of Graphics class

• There are number of methods available of Graphic class.

• Lets start with setcolor() method.

Page 3: Java session14

setColor(Color c)

• Color can be controlled by accessing the Color class. The Color class yields the class constants shown in the table below. The expression color.red gives the color constant for red.

• Images are drawn in the current color until the color is changed. Changing the color does not affect the color of previously drawn images.

Page 4: Java session14

Available color constant• Red• Yellow• Blue• Orange• Pink• Cyan• Magenta• Black• White• Gray• LightGray• darkGray

Page 5: Java session14

Example…

• g.setColor(Color.blue);• g.drawString(“Hello TY, I am blue”,100,100);• g.setColor(Color.orange);• g.drawLine(50,50,150,50);

Page 6: Java session14

drawLine(int x1, int y1, int x2, int y2)

Page 7: Java session14

drawLine()

Syntax:

drawLine(int x1,int y1, int x2, int y2)

In above code (x1,y1) is the start point of the line, and (x2,y2) is the end point of the line.

Example:Graphics.drawLine(20,100,120,100);

Page 8: Java session14

drawrect()• Syntax:

– drawRect(int x, int y, int width, int height)– Exampleimport java.applet.Applet;import java.awt.graphicsclass drawrectanglesexample extends Applet{

public void paint(Graphics g) {

g.drawRect(10,10,50,100);}

}

Page 9: Java session14