package com.robocontrol;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
public class ControlCanvas extends Canvas {
private RoboControl midlet;
private int width, height; // Screen width and height
private int d, r; // Radius and diameter of a ball
protected int xcoord, ycoord; // Upper-left corner of the drawn ball
protected int y_lim, x_lim;
protected int x_off;
private Font font;
private int fontHeight = 0;
private int pressedKey;
private StreamConnection c;
private OutputStreamWriter ow;
private InputStreamReader ir;
public ControlCanvas(RoboControl midlet) {
r = 10;
d = 2*r;
width = getWidth();
height = getHeight();
xcoord = width/2;
ycoord = height/2;
y_lim = height/4;
x_lim = width/4;
x_off = x_lim + x_lim/2;
this.midlet = midlet;
repaint();
}
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, width, height);
g.setColor(255, 0, 0);
g.drawRect(0, 0, width - 1, height - 1);
g.setColor(0, 0, 0);
font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
fontHeight = font.getHeight();
g.drawString("Key Code = " + pressedKey, 0, fontHeight, Graphics.TOP|Graphics.LEFT);
g.drawLine(xcoord-x_lim, ycoord, xcoord+x_lim, ycoord);
g.drawLine(xcoord, ycoord-y_lim, xcoord, ycoord+y_lim);
if (pressedKey == this.KEY_NUM2){
g.fillArc(xcoord - r - x_off, ycoord - r, d, d, 0, 360);
g.fillArc(xcoord-r, ycoord - r - y_lim, d, d, 0, 360);
this.cmdForward();
} else if (pressedKey == this.KEY_NUM8) {
g.fillArc(xcoord - r - x_off, ycoord - r, d, d, 0, 360);
g.fillArc(xcoord - r, ycoord - r + y_lim, d, d, 0, 360);
this.cmdBackward();
} else if (pressedKey == this.KEY_NUM4) {
g.fillArc(xcoord - r - x_off, ycoord - r, d, d, 0, 360);
g.fillArc(xcoord - r - x_lim, ycoord - r, d, d, 0, 360);
this.cmdLeft();
} else if (pressedKey == this.KEY_NUM6) {
g.fillArc(xcoord - r - x_off, ycoord - r, d, d, 0, 360);
g.fillArc(xcoord - r + x_lim, ycoord - r, d, d, 0, 360);
this.cmdRight();
} else if (pressedKey == this.KEY_NUM1) {
g.fillArc(xcoord - r - x_off, ycoord - r - y_lim, d, d, 0, 360);
g.fillArc(xcoord - r, ycoord - r, d, d, 0, 360);
this.cmdForkUp();
} else if (pressedKey == this.KEY_NUM7) {
g.fillArc(xcoord - r - x_off, ycoord - r + y_lim, d, d, 0, 360);
g.fillArc(xcoord - r, ycoord - r, d, d, 0, 360);
this.cmdForkDown();
} else {
g.fillArc(xcoord - r - x_off, ycoord - r, d, d, 0, 360);
g.fillArc(xcoord - r, ycoord - r, d, d, 0, 360);
}
}
protected void keyPressed(int keyCode) {
pressedKey = keyCode;
repaint();
}
protected void keyReleased(int keyCode) {
this.cmdStop();
pressedKey = 0;
repaint();
}
protected void keyRepeated(int keyCode) { }
public void connect(String stringURL) {
try {
c = (StreamConnection)Connector.open(stringURL);
synchronized(this) {
OutputStream os = c.openOutputStream();
ow = new OutputStreamWriter(os, "US-ASCII");
InputStream is = c.openInputStream();
ir = new InputStreamReader(is, "US-ASCII");
}
} catch (Throwable e) {
}
}
public void disconnect() {
try {
ow.close();
ir.close();
c.close();
} catch (Throwable e) {
}
}
public void cmdForkUp() {
try {
ow.write("\rload /flash/fup.bin\r");
ow.flush();
ow.write("run\r");
ow.flush();
} catch (Throwable e) {
}
}
public void cmdForkDown() {
try {
ow.write("\rload /flash/fdown.bin\r");
ow.flush();
ow.write("run\r");
ow.flush();
} catch (Throwable e) {
}
}
public void cmdLeft() {
try {
ow.write("\rload /flash/left.bin\r");
ow.flush();
ow.write("run\r");
ow.flush();
} catch (Throwable e) {
}
}
public void cmdRight() {
try {
ow.write("\rload /flash/right.bin\r");
ow.flush();
ow.write("run\r");
ow.flush();
} catch (Throwable e) {
}
}
public void cmdForward() {
try {
ow.write("\rload /flash/forward.bin\r");
ow.flush();
ow.write("run\r");
ow.flush();
} catch (Throwable e) {
}
}
public void cmdBackward() {
try {
ow.write("\rload /flash/backward.bin\r");
ow.flush();
ow.write("run\r");
ow.flush();
} catch (Throwable e) {
}
}
public void cmdStop() {
try {
ow.write("\rload /flash/stop.bin\r");
ow.flush();
ow.write("run\r");
ow.flush();
} catch (Throwable e) {
}
}
}