Fortunately, using images and random numbers is relatively easy. In the remainder of this lab we will create an Applet that displays a image in a random location in the the applet window.
The instructions below ask several questions of you. Be sure that you understand the answers, but there is nothing to hand in.
<applet code=Images.class width=500 height=500> </applet>
import java.awt.*;
import java.applet.Applet;
public class Images extends Applet
{
public void paint(Graphics geoff)
{
int appX = (int)getSize().getWidth();
int appY = (int)getSize().getHeight();
geoff.setColor(Color.blue);
geoff.fillRect(0,0,appX, appY);
geoff.setColor(Color.WHITE);
geoff.fillRect(5,5,appX-10, appY-10);
}
}
|
Image photo;
public void init()
{
photo = getImage(getCodeBase(), "a.jpg");
}
|
geoff.drawImage(photo, 5, 5, this);
|
int xloc = (int)(5 + Math.random()* (appX-10-photo.getWidth(this))); int yloc = (int)(5 + Math.random()* (appY-10-photo.getHeight(this))); geoff.drawImage(photo, xloc, yloc, this); |