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 pam_06_01;
- import javax.microedition.lcdui.Display;
- import javax.microedition.midlet.*;
- /**
- * @author Krystian
- */
- public class Midlet extends MIDlet {
- public void startApp() {
- Display.getDisplay(this).setCurrent(new MyCanvas(this));
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- }
- /*
- * 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 pam_06_02;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Graphics;
- /**
- *
- * @author Krystian
- */
- public class MyCanvas extends Canvas implements CommandListener {
- Midlet midlet;
- int xPos;
- int yPos;
- int dx = 1, dy = 1;
- int motionX = 1, motionY = 1;
- public MyCanvas(Midlet _midlet)
- {
- midlet = _midlet;
- addCommand(new Command("Koniec", Command.EXIT, 0));
- setCommandListener(this);
- xPos = getWidth()/2;
- yPos = getWidth()/2;
- }
- protected void paint(Graphics g) {
- g.setColor(0xffffff);
- int screenWidth = getWidth();
- int screenHeight = getHeight();
- xPos += dx * motionX;
- yPos += dy * motionY;
- if (xPos <= 0)
- motionX *= -1;
- if (xPos >= screenWidth - 32)
- motionX *= -1;
- if (yPos <= 0)
- motionY *= -1;
- if (yPos >= screenHeight - 32)
- motionY *= -1;
- g.fillRect(0, 0, screenWidth, screenHeight);
- if (motionX == 1 && motionY == 1)
- g.setColor(255, 255, 0);
- else if (motionX == -1 && motionY == -1)
- g.setColor(255, 0, 0);
- else if (motionY == 1 && motionX == -1)
- g.setColor(0, 255, 0);
- else if (motionY == -1 && motionX == 1)
- g.setColor(0, 0, 255);
- else
- g.setColor(0, 0, 0);
- g.fillRect(xPos, yPos, 32, 32);
- repaint();
- serviceRepaints();
- }
- public void commandAction(Command c, Displayable d) {
- switch (c.getCommandType()) {
- case Command.EXIT:
- midlet.destroyApp(false);
- midlet.notifyDestroyed();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement