Exercise - 13 (Applet)
a) Write a JAVA program to paint like paint brush in applet.
/* JAVA program to paint like paint brush in applet*/
import [Link].*;
import [Link].*;
import [Link].*;
public class Exe13a extends Applet implements MouseMotionListener
{
public void init()
{
addMouseMotionListener(this);
setBackground([Link]);
}
public void mouseDragged(MouseEvent me){
Graphics g=getGraphics();
[Link]([Link]);
[Link]([Link](),[Link](),5,5);
}
public void mouseMoved(MouseEvent me){}
}
/*
<applet code="Exe13a " width="300" height="300">
</applet>
*/
b) Write a JAVA program to display analog clock using Applet.
//JAVA program to display analog clock using Applet
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Exe13b extends Applet implements Runnable
{
int width, height;
Thread t = null;
boolean threadSuspended;
int hours=0, minutes=0, seconds=0;
String timeString = "";
public void init() {
width = getSize().width;
height = getSize().height;
setBackground( [Link] );
}
public void start() {
if ( t == null ) {
t = new Thread( this );
[Link]( Thread.MIN_PRIORITY );
threadSuspended = false;
[Link]();
}
else {
if ( threadSuspended ) {
threadSuspended = false;
synchronized( this ) {
notify();
}
}
}
}
public void stop() {
threadSuspended = true;
}
public void run() {
try {
while (true) {
Calendar cal = [Link]();
hours = [Link]( Calendar.HOUR_OF_DAY );
if ( hours > 12 ) hours -= 12;
minutes = [Link]( [Link] );
seconds = [Link]( [Link] );
SimpleDateFormat formatter = new SimpleDateFormat( "hh:mm:ss", [Link]() );
Date date = [Link]();
timeString = [Link]( date );
// Now the thread checks to see if it should suspend itself
if ( threadSuspended ) {
synchronized( this ) {
while ( threadSuspended ) {
wait();
}
}
}
repaint();
[Link]( 1000 ); // interval specified in milliseconds
}
}
catch (Exception e) { }
}
void drawHand( double angle, int radius, Graphics g ) {
angle -= 0.5 * [Link];
int x = (int)( radius*[Link](angle) );
int y = (int)( radius*[Link](angle) );
[Link]( width/2, height/2, width/2 + x, height/2 + y );
}
void drawWedge( double angle, int radius, Graphics g ) {
angle -= 0.5 * [Link];
int x = (int)( radius*[Link](angle) );
int y = (int)( radius*[Link](angle) );
angle += 2*[Link]/3;
int x2 = (int)( 5*[Link](angle) );
int y2 = (int)( 5*[Link](angle) );
angle += 2*[Link]/3;
int x3 = (int)( 5*[Link](angle) );
int y3 = (int)( 5*[Link](angle) );
[Link]( width/2+x2, height/2+y2, width/2 + x, height/2 + y );
[Link]( width/2+x3, height/2+y3, width/2 + x, height/2 + y );
[Link]( width/2+x2, height/2+y2, width/2 + x3, height/2 + y3 );
}
public void paint( Graphics g ) {
[Link]( [Link] );
drawWedge( 2*[Link] * hours / 12, width/5, g );
drawWedge( 2*[Link] * minutes / 60, width/3, g );
drawHand( 2*[Link] * seconds / 60, width/2, g );
[Link]( [Link] );
[Link]( timeString, 10, height-10 );
}
}
/*
<applet code="Exe13b" width="300" height="300">
</applet>
*/