Facebook
From Chunky Pudu, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 260
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mobileapplication11;
  7.  
  8. import java.util.Random;
  9. import javax.microedition.lcdui.*;
  10.  
  11. public class MyCanvas extends Canvas implements CommandListener {
  12.  
  13.     Midlet midlet;
  14.     int xPos;
  15.     int yPos;
  16.     int rColor;
  17.     int gColor;
  18.     int bColor;
  19.     boolean key_command[];
  20.  
  21.     public MyCanvas(Midlet _midlet) {
  22.         midlet = _midlet;
  23.         key_command = new boolean[4];
  24.         for (int i = 0; i < 4; i++) {
  25.             key_command[i] = false;
  26.         }
  27.         addCommand(new Command("Koniec", Command.EXIT, 0));
  28.         setCommandListener(this);
  29.  
  30.         xPos = getWidth() / 2;
  31.         yPos = getHeight() / 2;
  32.  
  33.         rColor = 10;
  34.         gColor = 10;
  35.         bColor = 255;
  36.  
  37.     }
  38.  
  39.     protected void paint(Graphics g) {
  40.  
  41.         Random generator = new Random();
  42.         int r = generator.nextInt(4);
  43.  
  44.         g.setColor(0xffffff);
  45.         int screenWidth = getWidth();
  46.         int screenHeight = getHeight();
  47.  
  48.         g.fillRect(0, 0, screenWidth, screenHeight);
  49.  
  50.         g.setColor(rColor, gColor, bColor);
  51.         g.fillRect(xPos, yPos, 32, 32);
  52.         System.out.println("repaint");
  53.         moveRectangle(r);
  54.  
  55.     }
  56.  
  57.     public void commandAction(Command c, Displayable d) {
  58.         switch (c.getCommandType()) {
  59.             case Command.EXIT:
  60.                 midlet.destroyApp(false);
  61.                 midlet.notifyDestroyed();
  62.                 break;
  63.         }
  64.     }
  65.  
  66.     private void moveRectangle(int r) {
  67.         while (yPos > 0 && yPos < getHeight() && xPos > 0 && xPos < getWidth()) {
  68.  
  69.             switch (r) {
  70.                 case 0:
  71.                     --yPos;
  72.                     break;
  73.                 case 1:
  74.                     ++yPos;
  75.                     break;
  76.                 case 2:
  77.                     --xPos;
  78.                     break;
  79.                 case 3:
  80.                     ++xPos;
  81.                     break;
  82.             }
  83.             repaint();
  84.             serviceRepaints();
  85.         }
  86.     }
  87. }
  88.