How to create different shapes using Applet in Java
1.
import [Link];
import [Link];
public class HelloWorldApplet extends Applet {
public void init() {
// Optional: initialization code
public void paint(Graphics g) {
// Draw a string at position (50, 50)
[Link]("Hello, World Applet!", 50, 50);
}
appletviewer [Link]
<html>
<body>
<h3>My HelloWorld Applet</h3>
<applet code="[Link]" width="200" height="100">
Your browser does not support Java applets.
</applet>
</body>
</html>
2. import [Link].*;
import [Link].*;
public class ShapesApplet extends Applet {
public void paint(Graphics g) {
int xa[] = { 120, 125, 150, 150, 200, 200 };
int ya[] = { 175, 100, 100, 175, 175, 200 };
// draws String
[Link]("This is Shapes Example", 100, 20);
// draws a Line
[Link](90, 135, 90, 180);
// draws a Oval
[Link](0, 10, 100, 100);
// draws a Rectangle
[Link](190, 50, 100, 100);
// fill an Arc
[Link](70, 150, 100, 100, 0, -90);
// draws a Polygon
[Link](xa, ya, 6);
}
}
<html>
<head>
<title>ShapesApplet</title>
</head>
<body>
<applet code="ShapesApplet" width="700" height="500"></applet>
</body>
</html>
Change Background Color
import [Link];
import [Link];
import [Link];
public class ChangeBackgroundApplet extends Applet {
public void init() {
setBackground([Link]);
}
public void paint(Graphics g) {
[Link]("Hello World in blue background", 200, 100);
}
}
<html>
<head>
<title>ChangeBackgroundApplet</title>
</head>
<body>
<applet code="ChangeBackgroundApplet" width="700"
height="500"></applet>
</body>
</html>
//Chess Board Program
import [Link].*;
import [Link].*;
/*<applet code="Chess" width=600 height=600>
</applet>*/
// Extends Applet Class
public class Chess extends Applet {
static int N = 10;
// Use paint() method
public void paint(Graphics g)
{
int x, y;
for (int row = 0; row & lt; N; row++) {
for (int col = 0; col & lt; N; col++) {
// Set x coordinates of rectangle
// by 20 times
x = row * 20;
// Set y coordinates of rectangle
// by 20 times
y = col * 20;
// Check whether row and column
// are in even position
// If it is true set Black color
if ((row % 2 == 0) == (col % 2 == 0))
[Link]([Link]);
else
[Link]([Link]);
// Create a rectangle with
// length and breadth of 20
[Link](x, y, 20, 20);
}
}
}
}
import [Link].*;
import [Link].*;
public class Shapes extends Applet {
int x = 300, y = 100, r = 50;
public void paint(Graphics g) {
[Link](30,300,200,10);
[Link](x-r,y-r,100,100);
[Link](400,50,200,100);
}
}