Draw Dots at Random Locations
in an Applet
import
java.applet.Applet;
import
java.awt.Dimension;
import java.awt.Graphics;
public class
DrawDotsAtRandomLocationsExample extends Applet implements Runnable
{
Thread t;
public void init()
{
t = new Thread(this);
t.start();
}
public void run()
{
try
{
while(true)
{
repaint();
Thread.sleep(200);
}
}
catch(Exception e)
{
}
}
public void
update(Graphics g)
{
paint(g);
}
public void
paint(Graphics g)
{
Dimension d =
getSize();
int x =
(int)(Math.random() * d.width);
int y =
(int)(Math.random() * d.height);
g.fillRect(x,y,2,2);
}
}
Draw Line in Applet Window
import
java.applet.Applet;
import
java.awt.Graphics;
public class
DrawLineExample extends Applet
{
public void
paint(Graphics g)
{
g.drawLine(10,10,50,50);
g.drawLine(10,50,10,100);
g.drawLine(10,10,50,10);
}
}
Draw Oval & Circle in
Applet Window
import
java.applet.Applet;
import java.awt.Color;
import
java.awt.Graphics;
public class
DrawOvalsExample extends Applet
{
public void
paint(Graphics g)
{
setForeground(Color.red);
g.drawOval(10,10,50,100);
g.fillOval(100,20,50,100);
}
}
Draw Rectangle & Square in
Applet Window
import
java.applet.Applet;
import
java.awt.Graphics;
public class
DrawRectanglesExample extends Applet
{
public void
paint(Graphics g)
{
g.drawRect(10,10,50,100);
g.drawRect(100,100,50,50);
}
}
Draw Rounded Corner Rectangle
& Square in Applet
import
java.applet.Applet;
import java.awt.Color;
import
java.awt.Graphics;
public class
DrawRoundedRectExample extends Applet
{
public void
paint(Graphics g)
{
setForeground(Color.red);
g.drawRoundRect(10,10,50,100,10,10);
g.drawRoundRect(100,100,50,50,10,10);
g.fillRoundRect(200,20,50,100,10,10);
g.fillRoundRect(200,200,50,50,10,10);
}
}
Draw Smiley In Applet
import java.awt.*;
import java.applet.*;
public class Smiley
extends Applet
{
public void
paint(Graphics g)
{
Font f = new
Font("Helvetica", Font.Bold,20);
g.setFont(f);
g.drawString("Keep
Smiling!!!", 50, 30);
g.drawOval(60, 60,
200, 200);
g.fillOval(90, 120,
50, 20);
g.fillOval(190, 120,
50, 20);
g.drawLine(165, 125,
165, 175);
g.drawArc(110, 130,
95, 95, 0, -180);
}
}
Fill Rectangle & Square in
Applet Window
import
java.applet.Applet;
import java.awt.Color;
import
java.awt.Graphics;
public class FilledRectangleExample
extends Applet
{
public void
paint(Graphics g)
{
setForeground(Color.red);
g.fillRect(10,10,50,100);
g.fillRect(100,100,50,50);
}
}
No comments:
Post a Comment