Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 lab6;
- 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) {
- 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");
- }
- protected void keyPressed(int keyCode) {
- moveRectangle(keyCode);
- }
- protected void keyRepeated(int keyCode) {
- moveRectangle(keyCode);
- }
- public void commandAction(Command c, Displayable d) {
- switch (c.getCommandType()) {
- case Command.EXIT:
- midlet.destroyApp(false);
- midlet.notifyDestroyed();
- break;
- }
- }
- private void moveRectangle(int keyCode) {
- switch (keyCode) {
- case KEY_NUM2:
- --yPos;
- rColor = 255;
- gColor = 10;
- bColor = 10;
- break;
- case KEY_NUM8:
- ++yPos;
- rColor = 10;
- gColor = 255;
- bColor = 10;
- break;
- case KEY_NUM4:
- --xPos;
- rColor = 255;
- gColor = 255;
- bColor = 10;
- break;
- case KEY_NUM6:
- ++xPos;
- rColor = 10;
- gColor = 255;
- bColor = 255;
- break;
- }
- repaint();
- serviceRepaints();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement