Advertisement
Guest User

Untitled

a guest
Jan 6th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.75 KB | None | 0 0
  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.  
  7. package pam_06_01;
  8.  
  9. import javax.microedition.lcdui.Display;
  10. import javax.microedition.midlet.*;
  11.  
  12. /**
  13.  * @author Krystian
  14.  */
  15. public class Midlet extends MIDlet {
  16.  
  17.     public void startApp() {
  18.         Display.getDisplay(this).setCurrent(new MyCanvas(this));
  19.     }
  20.    
  21.     public void pauseApp() {
  22.     }
  23.    
  24.     public void destroyApp(boolean unconditional) {
  25.     }
  26. }
  27.  
  28.  
  29.  
  30. /*
  31.  * To change this license header, choose License Headers in Project Properties.
  32.  * To change this template file, choose Tools | Templates
  33.  * and open the template in the editor.
  34.  */
  35.  
  36. package pam_06_02;
  37.  
  38. import javax.microedition.lcdui.Canvas;
  39. import javax.microedition.lcdui.Command;
  40. import javax.microedition.lcdui.CommandListener;
  41. import javax.microedition.lcdui.Displayable;
  42. import javax.microedition.lcdui.Graphics;
  43.  
  44. /**
  45.  *
  46.  * @author Krystian
  47.  */
  48. public class MyCanvas extends Canvas implements CommandListener {
  49.     Midlet midlet;
  50.     int xPos;
  51.     int yPos;
  52.     int dx = 1, dy = 1;
  53.     int motionX = 1, motionY = 1;
  54.    
  55.     public MyCanvas(Midlet _midlet)
  56.     {
  57.         midlet = _midlet;
  58.         addCommand(new Command("Koniec", Command.EXIT, 0));
  59.         setCommandListener(this);
  60.        
  61.         xPos = getWidth()/2;
  62.         yPos = getWidth()/2;
  63.     }
  64.    
  65.     protected void paint(Graphics g) {
  66.         g.setColor(0xffffff);
  67.         int screenWidth = getWidth();
  68.         int screenHeight = getHeight();
  69.        
  70.         xPos += dx * motionX;
  71.         yPos += dy * motionY;
  72.        
  73.         if (xPos <= 0)
  74.             motionX *= -1;
  75.         if (xPos >= screenWidth - 32)
  76.             motionX *= -1;
  77.         if (yPos <= 0)
  78.             motionY *= -1;
  79.         if (yPos >= screenHeight - 32)
  80.             motionY *= -1;
  81.        
  82.         g.fillRect(0, 0, screenWidth, screenHeight);
  83.        
  84.         if (motionX == 1 && motionY == 1)
  85.             g.setColor(255, 255, 0);
  86.         else if (motionX == -1 && motionY == -1)
  87.             g.setColor(255, 0, 0);
  88.         else if (motionY == 1 && motionX == -1)
  89.             g.setColor(0, 255, 0);
  90.         else if (motionY == -1 && motionX == 1)
  91.             g.setColor(0, 0, 255);
  92.         else
  93.             g.setColor(0, 0, 0);
  94.        
  95.         g.fillRect(xPos, yPos, 32, 32);
  96.        
  97.         repaint();
  98.         serviceRepaints();
  99.     }
  100.        
  101.     public void commandAction(Command c, Displayable d) {
  102.         switch (c.getCommandType()) {
  103.             case Command.EXIT:
  104.                 midlet.destroyApp(false);
  105.                 midlet.notifyDestroyed();
  106.                 break;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement