Java session14
-
Upload
jigarthacker -
Category
Education
-
view
850 -
download
0
Transcript of Java session14
Applet & Graphics Programming
Jigar Jobanputra
Methods of Graphics class
• There are number of methods available of Graphic class.
• Lets start with setcolor() method.
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.
Available color constant• Red• Yellow• Blue• Orange• Pink• Cyan• Magenta• Black• White• Gray• LightGray• darkGray
Example…
• g.setColor(Color.blue);• g.drawString(“Hello TY, I am blue”,100,100);• g.setColor(Color.orange);• g.drawLine(50,50,150,50);
drawLine(int x1, int y1, int x2, int y2)
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);
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);}
}