import java.util.Vector;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.*;
public class Main extends MIDlet implements Runnable{
Display display;
DrawCanvas canvas;
Thread thread;
Vector balls;
PlayerBar player;
PlayerBar enemy;
public Main() {
display = Display.getDisplay(this);
canvas = new DrawCanvas(this);
display.setCurrent(canvas);
balls = new Vector();
balls.addElement(new Ball(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getWidth()/15, canvas.getWidth()/15));
player = new PlayerBar(0, 0, canvas.getWidth()/3, canvas.getHeight()/18);
enemy = new PlayerBar(0, canvas.getHeight()-canvas.getHeight()/18, canvas.getWidth()/3, canvas.getHeight()/18);
thread = new Thread(this);
thread.start();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
public void render(Graphics g) {
for(int i = 0; i < balls.size(); i++) {
Ball b = (Ball) balls.elementAt(i);
b.render(g);
}
player.render(g);
enemy.render(g);
}
public void run() {
while(true) {
canvas.repaint();
for(int i = 0; i < balls.size(); i++) {
Ball b = (Ball) balls.elementAt(i);
if(b.getX()+b.getWidth() > player.getX() && b.getX() < player.getX()+player.getWidth() && b.getY() < player.getY()+player.getHeight() && b.getY() > player.getY()) {
b.bounce(Ball.yDir);
}
if(b.getX()+b.getWidth() > enemy.getX() && b.getX() < enemy.getX()+enemy.getWidth() && b.getY()+b.getHeight() > enemy.getY() && b.getY()+b.getHeight() < enemy.getY()+enemy.getHeight()) {
b.bounce(Ball.yDir);
}
if(b.getX() < 0 || b.getX()+b.getWidth() > canvas.getWidth()) {
b.bounce(Ball.xDir);
}
if(b.getY() < 0 || b.getY()+b.getHeight() > canvas.getHeight()) {
b.bounce(Ball.yDir);
}
b.tick();
}
Ball b = (Ball) balls.elementAt(0);
enemy.setX(b.getX()+b.getWidth()/2-enemy.getWidth()/2);
try { Thread.sleep(10); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
public void setMouse(int x, int y) {
player.setX(x-player.getWidth()/2);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class DrawCanvas extends Canvas{
Main main;
public DrawCanvas(Main main) {
this.main = main;
setFullScreenMode(true);
}
protected void paint(Graphics g) {
g.setColor(13158600);
g.fillRect(0, 0, getWidth(), getHeight());
main.render(g);
}
protected void pointerPressed(int x, int y) {
main.setMouse(x,y);
}
protected void pointerReleased(int x, int y) {
main.setMouse(x,y);
}
protected void pointerDragged(int x, int y) {
main.setMouse(x,y);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
import javax.microedition.lcdui.Graphics;
public class PlayerBar {
int x, y, width, height;
public PlayerBar(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void render(Graphics g) {
g.setColor(255);
g.fillRect(x, y, width, height);
g.setColor(16777215);
g.drawRect(x, y, width, height);
}
public void setX(int x) {
this.x = x;
}
public int getWidth() {
return width;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getHeight() {
return height;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Random;
import javax.microedition.lcdui.Graphics;
public class Ball {
double x,y;
int width,height;
double speed;
int dirX, dirY;
public final static int xDir = 0;
public final static int yDir = 1;
public Ball(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
Random r = new Random();
if(r.nextInt() > 5) {
dirX = 1;
} else {
dirX = -1;
}
r = new Random();
if(r.nextInt() < 5) {
dirY = 1;
} else {
dirY = -1;
}
speed = 1;
}
public void render(Graphics g) {
g.setColor(16766720);
g.fillRoundRect((int)x, (int)y, width, height, width, height);
g.setColor(16777215);
g.drawRoundRect((int)x, (int)y, width, height, width, height);
g.drawString(String.valueOf(speed), 10,150,0);
}
public void tick() {
speed += 0.0001;
x += dirX*speed;
y += dirY*speed;
}
public void bounce(int dir) {
if(dir == xDir) {
dirX *= -1;
}
if(dir == yDir) {
dirY *= -1;
}
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getX() {
return (int)x;
}
public int getY() {
return (int)y;
}
}