package elte.java2_utikalauz5.j2me; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*; import java.io.*; /** Animációs midlet. @link.forrásfájl {@docRoot}/../data/j2me/src SpriteMidlet.java @link.letöltés {@docRoot}/../data/j2me SpriteMidlet.jar @link.letöltés {@docRoot}/../data/j2me SpriteMidlet.jad @since Java 2 Útikalauz programozóknak 5.0 */ public class SpriteMidlet extends MIDlet { Image[] emberKepek = new Image[2]; Image[] korKepek = new Image[1]; Sprite emberSprite; Sprite korSprite; Display display; Canvas canvas; Timer timer = new Timer(); class SpriteMidletCanvas extends Canvas { public void paint(Graphics g) { g.setColor(0xff0000); g.fillRect(0,0, getWidth(), getHeight()); emberSprite.paint(g); korSprite.paint(g); } } class SpriteIdozito extends TimerTask { public void run() { emberSprite.lep(); korSprite.lep(); canvas.repaint(); } } class Sprite { private Image[] kepek; private int aktFazis; private int posX, posY; private int sebessegX, sebessegY; public Sprite(Image[] kepek, int posX, int posY, int sebessegX, int sebessegY ) { this.kepek = kepek; this.posX = posX; this.posY = posY; this.sebessegX = sebessegX; this.sebessegY = sebessegY; } public void paint(Graphics g) { g.drawImage(kepek[aktFazis], posX, posY, g.TOP|g.LEFT); } public void lep() { aktFazis = (aktFazis + 1) % kepek.length; posX += sebessegX; posY += sebessegY; } } public SpriteMidlet() { try { emberKepek[0] = Image.createImage("/kepek/ember1.png"); emberKepek[1] = Image.createImage("/kepek/ember2.png"); korKepek[0] = Image.createImage("/kepek/kor.png"); } catch( IOException e ) { System.out.println("Hiba:"+e); } emberSprite = new Sprite(emberKepek, 20, 20, 0, 0); korSprite = new Sprite(korKepek, 0, 0, 1, 1); canvas = new SpriteMidletCanvas(); timer.scheduleAtFixedRate(new SpriteIdozito(), 1000, 200); } public void startApp() { if( display == null ) { display = Display.getDisplay(this); } display.setCurrent(canvas); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }