You are on page 1of 44

CO5- Develop Program Using

Applet And Graphics

Prof: Naresh R. Shende


HOD, Computer Engineering
Shivajirao S. Jondhle Polytechnic
Building an Applet Code (.java file)

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

Output Hello Java


(0, 0)

100

Hello Java
10
Graphics class and Methods of Graphics class

Java’s Graphics class include methods for drawing


many different types of shapes from lines to
polygon. To draw a shape on the screen we may call
one of the methods available in the 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);

void fillRect(int x1, int y1, int width, int height);


The upper-left corner of the Rectangle is at x1 and y1.

The dimension of the Rectangle is specified by width


and height.
Example:
g.drawRect(10,10,60,50);
g.fillRect(10,10,60,50);
4. drawOval( ) and fillOval( ):

To draw an Ellipses or circles used drawOval( ) method


can be used and to filled an Ellipses or circles used
drawOval method.
The general form of this method is:
Syntax:
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)
The ellipse is drawn within a bounding rectangle whose
upper-left corner is specified by top and left and whose
width and height are specified by width and height to draw
a circle or filled circle, specify the same width and
height.
The following program draws several ellipses and
circle.
Example: g.drawOval(10,10,50,50);
5. drawRoundRect ( ) or fillRoundRect( ):
This method is used to draw and filled rounded
rectangle.
Syntax:
void drawRoundRect (int top, int left, int width, int
height, int xDiam, int yDiam);
void fillRoundRect (int top, int left, int width, int
height, int xDiam, int yDiam);
A rounded rectangle has rounded corners. The first four
paramenter is same as drawRect( ), the next two parameter
is specified the diameter of the rounding arc along the
X axis is specified by xDiam and the diameter of the
rounding arc along the Y axis is specified by yDiam.
Example:
g.drawRoundRect (190, 10, 60, 50, 15, 15);
6. drawArc( ): It is used to draw an arc
void drawArc(int x, int y, int w, int h, int
start_angle, int sweep_angle);

Where x, y starting point, w & h are width and height


of arc, and start_angle is starting angle of arc
sweep_angle is degree around the arc
Example: g.drawArc(10, 10, 30, 40, 40, 90);

7. drawPolygon( ) and fillPolygon( )


Draws and filled a closed polygon defined by arrays of
x and y coordinates. The number of points defined by x
and y is specified by numpoints.
void drawPolygon(int[] xPoints,int[] yPoints, int
nPoints)
Syntax: void fillPolygon (int x[ ], int y[ ], int
numPoints)
Example
import java.applet.*;
import java.awt.*;
/*<applet code=DrawGraphics.class height=500 width=400>
</applet> */
public class DrawGraphics extends Applet
{
public void paint(Graphics g)
{
int x[] = {10, 170, 80,80};
int y[] = {20, 40, 140,20};
int n = 4;
g.drawPolygon(x, y, n);
g.drawRect(10, 150,100, 80);
g.drawOval(10, 250, 100, 80);
g.fillOval(10, 350, 100, 80);
g.drawArc(60,125,80,40,180,180);
}
}
Working with Color class
Color is represented by the Color class.
Color class defines several constants colors (for ex.
Color.black)
we can create your own colors, using one of the color
constructors.
Color(int red, int green, int blue)
It takes three integers combination of red, green, blue
values must be between 0 and 255.
For example: Color c = new Color(255, 100, 100);
Color(float red, float green, float blue)
It takes three float values (between 0.0 and 1.0) of
red, green, and blue. Once you have created a color,
we can use it to set the foreground by using the
setForeground( ) and/or set background color by using
setBackground( ) methods.
The Color class defines several methods to obtain the
red, green, and blue components of a color independently
using
int getRed( )
int getGreen( )
int getBlue( )
To obtain a combined, RGB representation of a color,
Use int getRGB( ) by default, graphics objects are
drawn in the current foreground color.
You can change this color by calling the Graphics
method
setColor( ):
void setColor(Color newColor)
Here, newColor specifies the new drawing color.
You can obtain the current color by calling getColor(),

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

You might also like