/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package mobileapplication11; import java.util.Random; import javax.microedition.lcdui.*; public class MyCanvas extends Canvas implements CommandListener { Midlet midlet; int xPos; int yPos; int rColor; int gColor; int bColor; boolean key_command[]; public MyCanvas(Midlet _midlet) { midlet = _midlet; key_command = new boolean[4]; for (int i = 0; i < 4; i++) { key_command[i] = false; } addCommand(new Command("Koniec", Command.EXIT, 0)); setCommandListener(this); xPos = getWidth() / 2; yPos = getHeight() / 2; rColor = 10; gColor = 10; bColor = 255; } protected void paint(Graphics g) { Random generator = new Random(); int r = generator.nextInt(4); g.setColor(0xffffff); int screenWidth = getWidth(); int screenHeight = getHeight(); g.fillRect(0, 0, screenWidth, screenHeight); g.setColor(rColor, gColor, bColor); g.fillRect(xPos, yPos, 32, 32); System.out.println("repaint"); moveRectangle(r); } public void commandAction(Command c, Displayable d) { switch (c.getCommandType()) { case Command.EXIT: midlet.destroyApp(false); midlet.notifyDestroyed(); break; } } private void moveRectangle(int r) { while (yPos > 0 && yPos < getHeight() && xPos > 0 && xPos < getWidth()) { switch (r) { case 0: --yPos; break; case 1: ++yPos; break; case 2: --xPos; break; case 3: ++xPos; break; } repaint(); serviceRepaints(); } } }