Unit 5 - Applet and Graphics Programming1
Unit 5 - Applet and Graphics Programming1
import java.awt. *;
import java. applet. *;
public class AppletClassname extends Applet
{
public void paint (Graphics g)
{
// body of paint method
}
//Other method like init ( ), start( ), stop ( ) etc.
} // end of applet
import java.awt. *;
import java.applet. *;
public class HelloJava extends Applet
{
public void paint (Graphics g)
{
g.drawString(“Hello Java”, 10, 100);
}
}
100
Hello Java
10
Graphics class and Methods of Graphics class
1. drawString ( )
2. drawLine( )
3. drawPolygon( )
4. drawRect ( )
5. drawOval ( )
6. drawArc ( )
1. drawString( )
This method is used to display the string in an
applet window
Syntax:
void drawString(String message, int x, int y);
Where message is the string to be displayed beginning
at x, y
Example: g.drawString(“WELCOME”, 10, 10);
2. drawLine( )
Displays a line in the current drawing color that
begins at x1, y1and ends at x2, y2.
Syntax:
void drawLine(int x1, int y1, int x2, int y2)
Example:
g.drawLine(50,70, 150, 200);
3. drawRect( )and fillRect( ):
This method display an outlined and filled of the
rectangle.
Syntax:
void drawRect(int x1,int y1,int width,int height);
Color getColor( )
import java.awt.*;
import java.applet.*;
/*<applet code="ColorDemo" width=300 height=200> </applet> */
public class ColorDemo extends Applet
{
public void paint(Graphics g)
{
Color c1 = new Color(255, 100, 100);
Color c2 = new Color(100, 255, 100);
Color c3 = new Color(100, 100, 255);
g.setColor(c1);
g.drawLine(0, 0, 100, 100);
setColor(Color.red);
g.drawOval(10, 10, 50, 50);
g.fillOval(70, 90, 140, 100);
g.drawRect(10, 10, 60, 50);
g.setColor(Color.cyan);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
}
}
Working with Fonts
The family name is the general name of the font, such as
Courier.
The logical name specifies a category of font, such as
Monospaced.
The face name specifies a specific font, such as Courier
Italic. Fonts are encapsulated by the Font class.
Font f=new Font(String fontName,int fontStyle,int pointSize)
Here, fontName specifies the name of the desired font. The
style of the font is specified by fontStyle. It may consist of
three constants: Font.PLAIN, Font.BOLD, and Font.ITALIC. To
combine styles,
For example, Font.BOLD | Font.ITALIC specifies a bold,
italics style. The size, in points, of the font is specified by
pointSize.
To use a font that you have created, you must select it
using setFont( ), which is defined by Component. It has this
general form:
void setFont(Font fontObj)
Here, fontObj is the object that contains the desired font.
import java.applet.*;
import java.awt.*;
/* <applet code=FontDemo width=300 height=100> </applet>*/
class FontDemo extends Applet
{
String msg;
public void init( )
{
setBackground (Color.yellow);
setForeground (Color.magneta);
Font f = new Font(“Times New Roman”, Font.ITALIC, 30);
msg = “Welcome to Java Programming”;
setFont(f);
}
public void paint(Graphics g)
{
g.drawString(msg, 100, 60);
}
}