Professional Documents
Culture Documents
import java.awt.*;
import java.util.*;
import java.applet.*;
radius = radiusVal;
color = colorVal;
dimension = dimensionVal;
position = startingPointVal;
startingPoint = startingPointVal;
{
//This method returns the ball radius
return radius;
}
return color;
}
public Dimension getDimension()
return dimension;
return position;
interface PositionChangeable
{
int STEP_X = 20;
int STEP_Y = 10;
void move();
}
int x = (int)(getPosition().getX());
int y = (int)(getPosition().getY());
getPosition().translate(STEP_X*factorX, STEP_Y*factorY);
}
void paint(Graphics g)
{
g.setColor(getColor());
g.fillOval((int)(getPosition().getX()),
(int)(getPosition().getY()),getRadius(),getRadius());
}
}
interface SizeChangeable
{
public abstract void changeSize();
}
interface SizeAndPositionChangeable extends SizeChangeable, PositionChangeable
{
}
}
public void paint(Graphics g)
{
Arrays.sort(vec);
for(int m=0; m<vec.length; m++)
{
((CrazyBall)vec[m]).paint(g);
}
}
public void run()
{
while(goOn)
{
for(int index=0; index<vec.length; index++)
{
vec[index].move();
vec[index].changeSize();
}
repaint();
try
{
Thread.sleep(Moveable.SLEEP_TIME);
}
catch(Exception e) {}
}
}
public void stop()
{
goOn=false;
thread=null;
}
}
3. Create an applet that displays an image of winter in Europe (snow…) and draws
little pieces of falling snow in order to give the filling of falling snow.
4. Create an applet that displays a ball that moves like a billiard ball within a
rectangle. The size of the ball and the dimension of the rectangle should be sent as
parameters from the HTML document. The applet should present the user two buttons:
“PAUSE” and “CONTINUE”. Pressing the “PAUSE” button should pause the ball
moving. Pressing the “CONTINUE” should cause the ball continue its moving. You
should use the methods wait() and notify().
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
if(ballRadiusStr!=null)
{
try
{
ballRadius = Integer.parseInt(ballRadiusStr);
}
catch(NumberFormatException e)
{
e.printStackTrace();
ballRadius = 10;
}
}
else
{
ballRadius = 10;
}
if(widthStr!=null)
{
try
{
width = Integer.parseInt(widthStr);
}
catch(NumberFormatException e)
{
e.printStackTrace();
width = defaultWidth;
}
}
else
{
width = defaultWidth;
}
if(heightStr!=null)
{
try
{
height = Integer.parseInt(heightStr);
}
catch(NumberFormatException e)
{
e.printStackTrace();
height = defaultHeight;
}
}
else
{
height = defaultHeight;
}
//calling repaint
repaint();
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}