j2me BouncyBall


















package com.boball.demo;

import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.*;
/**
 * @see J2ME & Threading Task
 */
public class BounceBall extends MIDlet implements CommandListener {
    /**
     * First of all, we create a display and command objects.
     * @see Display display, Command Exit
     */
    private Display theDisplay;
    private final static Command _cExit = new Command("Exit", Command.EXIT, 1);
    private final static Command _cClear = new Command("Clear", Command.SCREEN, 1);
    /**
     * Then, we will create a class to move ball on canvas
     * @see BounceCanvas class.
     */
    private BounceCanvas canvas;
    public BounceBall() {
        theDisplay = Display.getDisplay(this);
        canvas = new BounceCanvas();
        canvas.addCommand(_cExit);
        canvas.addCommand(_cClear);
        canvas.setCommandListener(this);
    }

    protected void destroyApp(boolean unconditional) {
        canvas.erase();
    }

    protected void pauseApp() {
    }

    protected void startApp() {
        theDisplay.setCurrent(canvas);
    }

    public void commandAction(Command c, Displayable d) {
        try {
            if (c == _cClear) {
                canvas.erase();
            } else if (c == _cExit) {
                destroyApp(false);
                notifyDestroyed();
            }
        } catch (Exception e) {
            debugStr(e);
        }
    }

    public void debugStr(Exception ex) {
        System.out.println("Error: " + ex.toString());
    }

    class BounceCanvas extends Canvas{
        private Random numberGen = new Random();
        public Vector balls = new Vector();

        class Ball extends Thread {
            // current & former positions:
            private int xc, yc, oldxc, oldyc;
            // x & y speeds:
            private int dx = 3, dy = 3;
            // halting machinery:
            private boolean halt = false;

            public void stop() {
                halt = true;
            }

            public Ball() {
                try {
                    xc = Math.abs(numberGen.nextInt()) % getWidth();
                    yc = Math.abs(numberGen.nextInt()) % getHeight();
                } catch (Exception e) {
                    debugStr(e);
                }
            }

            private void move() {
                oldxc = xc;
                oldyc = yc;
                xc += dx;
                yc += dy;
                if (xc <= 0 || getWidth() <= xc) {
                    dx = -dx;
                }
                if (yc <= 0 || getHeight() <= yc) {
                    dy = -dy;
                }
            }

            public void run() {
                while (!halt) {
                    move();
                    repaint(oldxc - 5, oldyc - 5, 10, 10);
                    // sleep for 10 msecs to be cooperative:
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        debugStr(e);
                    }
                }
            }

            public void paint(Graphics g) {
                try {
                    Image image = Image.createImage("/images/miniapple.jpg");
                    g.setColor(0xffffff);
                    // hata var //g.fillRect(xc, yc, getWidth(), getHeight());
                    g.drawImage(image, xc, yc, Graphics.HCENTER | Graphics.VCENTER);
                } catch (Exception ex) {
                    debugStr(ex);
                }
            }
        }

        public void erase() {
            try {
                for (int i = 0; i < balls.size(); i++) {
                    Ball b = (Ball) balls.elementAt(i);
                    b.stop();
                }
                balls.removeAllElements();
                repaint();
            } catch (Exception e) {
                debugStr(e);
            }
        }

        public void paint(Graphics g) {
            try {
                /* drawind text part*/
                g.setColor(0x00ffffff);
                g.fillRect(0, 0, getWidth(), getHeight());
                int w = getWidth();
                int h = getHeight();
                g.setColor(0xffffff);
                g.fillRect(0, 0, w, h);
                g.setColor(0x000000);
                Font mFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
                String s = "Please Push The 5 to Start";
                int stringWidth = mFont.stringWidth(s);
                int stringHeight = mFont.getHeight();
                int x = (w - stringWidth) / 2;
                int y = h / 2;
                Image image = Image.createImage("/images/bigapple.jpg");
                g.drawImage(image, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                /* drawind image part*/
                g.setFont(mFont);
                g.drawString(s, x, y, Graphics.TOP | Graphics.LEFT);
                g.drawRect(x, y, stringWidth, stringHeight);

                for (int i = 0; i < balls.size(); i++) {
                    Ball next = (Ball) balls.elementAt(i);
                    next.paint(g);
                }
            } catch (Exception e) {
                debugStr(e);
            }
        }

        public void keyPressed(int keyCode) {
            try {
                if (keyCode == Canvas.KEY_NUM5) {
                    Ball b = new Ball();
                    balls.addElement(b);
                    b.start();
                }
            } catch (Exception e) {
                debugStr(e);
            }
        }
    }
}

No comments: