private translatecanvas canvas; // canvas
public translate()
{
display = display.getdisplay(this);
canvas = new translatecanvas(this);
}
protected void startapp()
{
display.setcurrent( canvas );
}
protected void pauseapp()
{ }
protected void destroyapp( boolean unconditional )
{ }
public void exitmidlet()
{
destroyapp(true);
notifydestroyed();
}
}
/*--------------------------------------------------
* class translate
*
* draw image using translated coordinates
*-------------------------------------------------*/
class translatecanvas extends canvas implements commandlistener
{
private command cmexit; // exit midlet
private translate midlet;
private image im = null;
private int translatex = 0, translatey = 0;
public translatecanvas(translate midlet)
{
this.midlet = midlet;
// create exit command & listen for events
cmexit = new command("exit", command.exit, 1);
addcommand(cmexit);
setcommandlistener(this);
try
{
// create immutable image
im = image.createimage("/bolt.png");
}
catch (java.io.ioexception e)
{
system.err.println("unable to locate or read .png file");
}
}
protected void paint(graphics g)
{
if (im != null)
{
// clear the background
g.setcolor(255, 255, 255);
g.fillrect(0, 0, getwidth(), getheight());
// translate coordinates
g.translate(translatex, translatey);
// always draw at 0,0
g.drawimage(im, 0, 0, graphics.left | graphics.top);
}
}
public void commandaction(command c, displayable d)
{
if (c == cmexit)
midlet.exitmidlet();
}
protected void keypressed(int keycode)
{
switch (getgameaction(keycode))
{
case up:
// if scrolling off the top, roll around to bottom
if (translatey - im.getheight() < 0)
translatey = getheight() - im.getheight();
else
translatey -= im.getheight();
break;
case down:
// if scrolling off the bottom, roll around to top
if ((translatey + im.getheight() + im.getheight()) > getheight())
translatey = 0;
else
translatey += im.getheight();
break;
case left:
// if scrolling off the left, bring around to right
if (translatex - im.getwidth() < 0)
translatex = getwidth() - im.getwidth();
else
translatex -= im.getwidth();
break;
case right:
// if scrolling off the right, bring around to left
if ((translatex + im.getwidth() + translatex) > getwidth())
translatex = 0;
else
translatex += im.getwidth();
break;
}
repaint();
}
}