Tuesday, October 30, 2007

MIDTERM

Launch in MPowerPlayer













import java.util.Enumeration;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Screen;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
* functionality: browsing, entry, deletion, and searching
*/
public class NewIdeaMIDletOne extends MIDlet implements CommandListener,
ItemStateListener {

private RecordStore ideas;
private static final int T_LEN = 100;
private static final int I_LEN = 200;
final private static int ERROR = 0;
final private static int INFO = 1;
private Display display;
private Alert alert;
private Command cmdAdd;
private Command cmdBack;
private Command cmdCancel;
private Command cmdExit;
private Command cmdSelect;
private Command cmdSearch;
private Command cmdDelete;
private List mainScr;
private String[] mainScrChoices = { "Search", "Add New", "Browse", "Delete", "Options" };
private Form searchScr;
private TextField s_title;
private TextField s_idea;
private Form entryScr;
private TextField e_idea;
private TextField e_title;
private int e_id;
private List nameScr;
private Form optionScr;
private ChoiceGroup sortChoice;
private int sortOrder = 1;
private List deleteScr;

public NewIdeaMIDletOne() {
display = Display.getDisplay(this);

cmdAdd = new Command("Add", Command.OK, 1);
cmdBack = new Command("Back", Command.BACK, 2);
cmdCancel = new Command("Cancel", Command.BACK, 2);
cmdExit = new Command("Exit", Command.EXIT, 2);
cmdSelect = new Command("Select", Command.OK, 1);
cmdDelete = new Command("Delete", Command.OK, 1);
cmdSearch = new Command("Search", Command.SCREEN, 3);

alert = new Alert("", "", null, AlertType.INFO);
alert.setTimeout(2000);

try {
ideas = RecordStore.openRecordStore("IdeaStorage", true);
} catch (RecordStoreException e) {
ideas = null;
}
}

protected void startApp() {
if (ideas == null) {
displayAlert(ERROR, "Could not open list of ideas", null);
} else {
genMainScr();
}
}

protected void pauseApp() {
}

protected void destroyApp(boolean unconditional) {
if (ideas != null) {
try {
ideas.closeRecordStore();
} catch (Exception e) {
}
}
}

private void displayAlert(int type, String msg, Screen s) {
alert.setString(msg);

switch (type) {
case ERROR:
alert.setTitle("Error!");
alert.setType(AlertType.ERROR);
break;
case INFO:
alert.setTitle("Info");
alert.setType(AlertType.INFO);
break;
}
display.setCurrent(alert, s == null ? display.getCurrent() : s);
}

private void midletExit() {
destroyApp(false);
notifyDestroyed();
}

private Screen genMainScr() {
if (mainScr == null) {
mainScr = new List("Menu", List.IMPLICIT, mainScrChoices, null);
mainScr.addCommand(cmdSelect);
mainScr.addCommand(cmdExit);
mainScr.setCommandListener(this);
}
display.setCurrent(mainScr);
return mainScr;
}

private Screen genOptionScr() {
if (optionScr == null) {
optionScr = new Form("Options");
optionScr.addCommand(cmdBack);
optionScr.setCommandListener(this);

sortChoice = new ChoiceGroup("Sort by", Choice.EXCLUSIVE);
sortChoice.append("Title", null);
sortChoice.append("Idea", null);
sortChoice.setSelectedIndex(sortOrder, true);
optionScr.append(sortChoice);
optionScr.setItemStateListener(this);
}
display.setCurrent(optionScr);
return optionScr;
}

private Screen genSearchScr() {
if (searchScr == null) {
searchScr = new Form("Search");
searchScr.addCommand(cmdBack);
searchScr.addCommand(cmdSearch);
searchScr.setCommandListener(this);
s_title = new TextField("Title:", "", I_LEN, TextField.ANY);
s_idea = new TextField("Idea:", "", T_LEN, TextField.ANY);
searchScr.append(s_idea);
searchScr.append(s_title);
}

s_idea.delete(0, s_idea.size());
s_title.delete(0, s_title.size());
display.setCurrent(searchScr);
return searchScr;
}

private Screen genEntryScr() {
if (entryScr == null) {
entryScr = new Form("Add new");
entryScr.addCommand(cmdCancel);
entryScr.addCommand(cmdAdd);
entryScr.setCommandListener(this);

e_title = new TextField("Title:", "", T_LEN, TextField.ANY);
e_idea = new TextField("Idea:", "", I_LEN, TextField.ANY);
entryScr.append(e_title);
entryScr.append(e_idea);
}

e_title.delete(0, e_title.size());
e_idea.delete(0, e_idea.size());

display.setCurrent(entryScr);
return entryScr;
}

private Screen genNameScr(String title, String f, String l, boolean local) {
SimpleComparator sc;
SimpleFilter sf = null;
RecordEnumeration re;

if (local) {
sc = new SimpleComparator(
sortOrder == 0 ? SimpleComparator.SORT_BY_TITLE
: SimpleComparator.SORT_BY_IDEA);

if (f != null || l != null) {
sf = new SimpleFilter(f, l);
}

try {
re = ideas.enumerateRecords(sf, sc, false);
} catch (Exception e) {
displayAlert(ERROR, "Could not create enumeration: " + e, null);
return null;
}

nameScr = null;
if (re.hasNextElement()) {
nameScr = new List(title, List.IMPLICIT);
nameScr.addCommand(cmdBack);
nameScr.setCommandListener(this);

try {
while (re.hasNextElement()) {
byte[] b = re.nextRecord();
nameScr.append(SimpleRecord.getTitle(b) + " - "
+ SimpleRecord.getIdea(b) + " " , null);
}
} catch (Exception e) {
displayAlert(ERROR, "Error while building idea list: " + e,
null);
return null;
}
display.setCurrent(nameScr);

} else {
displayAlert(INFO, "No ideas found", null);
}
}
return nameScr;
}

/* ********************************************************************************* */

private Screen genRemoveScr(String title, String f, String l, boolean local) {
SimpleComparator sc;
SimpleFilter sf = null;
RecordEnumeration re;

if (local) {
sc = new SimpleComparator(
sortOrder == 0 ? SimpleComparator.SORT_BY_TITLE
: SimpleComparator.SORT_BY_IDEA);

if (f != null || l != null) {
sf = new SimpleFilter(f, l);
}

try {
re = ideas.enumerateRecords(sf, sc, false);
} catch (Exception e) {
displayAlert(ERROR, "Could not create enumeration: " + e, null);
return null;
}

deleteScr = null;
if (re.hasNextElement()) {
deleteScr = new List(title, List.IMPLICIT);
deleteScr.addCommand(cmdBack);
deleteScr.addCommand(cmdDelete);
deleteScr.setCommandListener(this);

try {
while (re.hasNextElement()) {
byte[] b = re.nextRecord();
deleteScr.append(SimpleRecord.getTitle(b) + " - "
+ SimpleRecord.getIdea(b) + " " , null);
}
} catch (Exception e) {
displayAlert(ERROR, "Error while building idea list: " + e,
null);
return null;
}
display.setCurrent(deleteScr);

} else {
displayAlert(INFO, "No ideas found", null);
}
}
return deleteScr;
}

/* ********************************************************************************* */

private void addEntry() {

String f = e_title.getString();
String l = e_idea.getString();

byte[] b = SimpleRecord.createRecord(f, l);
try {
ideas.addRecord(b, 0, b.length);
displayAlert(INFO, "Record added", mainScr);
} catch (RecordStoreException rse) {
displayAlert(ERROR, "Could not add record" + rse, mainScr);
}
}

/* ********************************************************************************************** */

private void deleteEntry() {

byte[] b = SimpleRecord.deleteRecord(e_id);
try {
ideas.deleteRecord(b.length);
displayAlert(INFO, "Record deleted", mainScr);
} catch (RecordStoreException rse){
displayAlert(ERROR, "Could not delete record" + rse, mainScr);
}
}

/* **************************************************************************************** */

public void commandAction(Command c, Displayable d) {
if (d == mainScr) {
if (c == cmdExit) {
midletExit();
} else if ((c == List.SELECT_COMMAND) || (c == cmdSelect)) {
switch (mainScr.getSelectedIndex()) {
case 0:
genSearchScr();
break;
case 1:
genEntryScr();
break;
case 2:
genNameScr("Browse", null, null, true);
break;
case 3:
genRemoveScr("Remove", null, null, true);
break;
case 4:
genOptionScr();
break;
default:
displayAlert(ERROR, "Unexpected index!", mainScr);
}
}
} else if (d == nameScr) {
if (c == cmdBack) {
genMainScr();
}
} else if (d == deleteScr) {
if (c == cmdBack) {
genMainScr();
} else if (c == cmdDelete) {
deleteEntry();
}
} else if (d == entryScr) {
if (c == cmdCancel) {
genMainScr();
} else if (c == cmdAdd) {
addEntry();
}
} else if (d == optionScr) {
if (c == cmdBack) {
genMainScr();
}
} else if (d == searchScr) {
if (c == cmdBack) {
genMainScr();
} else if (c == cmdSearch) {

genNameScr("Search Result", s_idea.getString(), s_title
.getString(), c == cmdSearch);
}
}
}

public void itemStateChanged(Item item) {
if (item == sortChoice) {
sortOrder = sortChoice.getSelectedIndex();
}
}
}

class Query implements RecordEnumeration {
private Enumeration resultsEnumeration;

public boolean hasNextElement() {
return resultsEnumeration.hasMoreElements();
}

public byte[] nextRecord() {
return (byte[]) resultsEnumeration.nextElement();
}

public boolean hasPreviousElement() {
return false;
}

public void destroy() {
}

public boolean isKeptUpdated() {
return false;
}

public void keepUpdated(boolean b) {
return;
}

public int nextRecordId() {
return 0;
}

public int numRecords() {
return 0;
}

public byte[] previousRecord() {
return null;
}

public int previousRecordId() {
return 0;
}

public void rebuild() {
return;
}

public void reset() {
return;
}
}


class SimpleFilter implements RecordFilter {

private String first;

private String last;

public SimpleFilter(String f, String l) {
first = f.toLowerCase();
last = l.toLowerCase();
}

public boolean matches(byte[] r) {

String f = SimpleRecord.getTitle(r).toLowerCase();
String l = SimpleRecord.getIdea(r).toLowerCase();

return f.startsWith(first) && l.startsWith(last);
}
}

class SimpleComparator implements RecordComparator {

public final static int SORT_BY_TITLE = 1;

public final static int SORT_BY_IDEA = 2;

private int sortOrder = -1;

SimpleComparator(int s) {
switch (s) {
case SORT_BY_TITLE:
case SORT_BY_IDEA:
this.sortOrder = s;
break;
default:
this.sortOrder = SORT_BY_IDEA;
break;
}
}

public int compare(byte[] r1, byte[] r2) {

String n1 = null;
String n2 = null;

if (sortOrder == SORT_BY_TITLE) {
n1 = SimpleRecord.getTitle(r1).toLowerCase();
n2 = SimpleRecord.getTitle(r2).toLowerCase();
} else if (sortOrder == SORT_BY_IDEA) {
n1 = SimpleRecord.getIdea(r1).toLowerCase();
n2 = SimpleRecord.getIdea(r2).toLowerCase();
}

int n = n1.compareTo(n2);
if (n < 0) {
return RecordComparator.PRECEDES;
}
if (n > 0) {
return RecordComparator.FOLLOWS;
}

return RecordComparator.EQUIVALENT;
}
}

final class SimpleRecord {

private final static int TITLE_INDEX = 0;
private final static int IDEA_INDEX = 20;
private final static int FIELD_LEN = 20;
private final static int MAX_REC_LEN = 60;
private static StringBuffer recBuf = new StringBuffer(MAX_REC_LEN);

private SimpleRecord() {
}

private static void clearBuf() {
for (int i = 0; i < MAX_REC_LEN; i++) {
recBuf.insert(i, ' ');
}
recBuf.setLength(MAX_REC_LEN);
}

public static byte[] createRecord(String z_title, String z_idea) {
clearBuf();
recBuf.insert(TITLE_INDEX, z_title);
recBuf.insert(IDEA_INDEX, z_idea);
recBuf.setLength(MAX_REC_LEN);
return recBuf.toString().getBytes();
}
/* **********************************************************************************
public static byte[] removeRecord(int z_id, int z_ida) {
clearBuf();
recBuf.delete(TITLE_INDEX, z_id);
recBuf.delete(IDEA_INDEX, z_ida);
recBuf.setLength(MAX_REC_LEN);
return recBuf.toString().getBytes();
}
********************************************************************************** */


public static String getTitle(byte[] b) {
return new String(b, TITLE_INDEX, FIELD_LEN).trim();
}

public static String getIdea(byte[] b) {
return new String(b, IDEA_INDEX, FIELD_LEN).trim();
}

}

Tuesday, October 16, 2007

Game API



Changes made:
1- Changed color of Enemy
2- Added ears to Enemy
3- Enemy walks backwards
4- Changed speed of Enemy
5- Changed start position of Enemy
6- Changed walking pattern

Problems:
Not sure how to add another character...new Class? new "_characters.addElement"?

Tuesday, October 9, 2007

Wireframe midterm ideas



1- Function: Add
a- Type: Text Form
i- Fields: Title, body
b- Position (x,y)

2- Function: Link
a- Check for minimum of 2 Notes
b- If more than 2 notes, need Form
i- Fields: Link Note # to Note #

3- Function: Undo
a- Store in array order of notes
b- Undo by LIFO

4- Function: Save
a- Form with file name and timestamp

Monday, October 8, 2007

"Could not find jar tool executable" When Packaging

Originally, when I tried to compile the file for deployment, I received this error:


Could not find jar tool executable.
The jar tool requires a full JDK installation.
Specify a full JDK installation in the Java preferences.


Reason:
Could not find jar tool executable.



Solution from http://www.eclipseme.org/docs/faq.html#jar,:


"Could not find jar tool executable" When Packaging


The process of obfuscated packaging requires that the jar file created by the obfuscation tool must be preverified again. The preverifier tool needs to be able to invoke the jar executable tool while preverifying the jar file from the PATH environment. EclipseME searches for the jar executable within the "Installed Java Runtime Environments" in your Eclipse installation. EclipseME must find at least one full Java Development Kit within the Installed JRE's (a subcategory of the Java category). By default, Eclipse will recognize a JRE rather than a full JDK on Windows. To solve this problem, make sure to point the location of the installed JRE instance to the root directory of the JDK directory. For instance on Windows, that might be something like c:\j2sdk1.4.2.


In English:
1- Download and install the j2sdk1.4.2 from Sun
2- Right Click project, go to Properties
3- Run/Debug Settings
4- Highlight Main, Click Edit
5- Click on JRE Tab
6- Click radio button for Alternate JRE
7- Locate installed JRE

Sunday, October 7, 2007

Week 3 Assignments

Animation


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class newCanvas extends MIDlet {

private boolean boolMotion=true;
private int iX=10,iY=60;

Display mDisplay;

public void destroyApp(boolean unconditional){}

public void pauseApp() {}

public void startApp() {
mDisplay = Display.getDisplay(this);
final MyCanvas can = new MyCanvas();
mDisplay.setCurrent(can);
}

}



import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class MyCanvas extends Canvas implements Runnable {
int count, counter=0;
int h, w;
private Random rand;

//constructor - initialize variables
public MyCanvas() {
//get the height of the canvas
h = getHeight();
w = getWidth();

//set text colour to white
Thread th=new Thread(this);
th.start();
}

//drawing on the canvas
public void paint(Graphics g) {
//paint the background
g.setColor(0,0,0);
g.fillRoundRect(0, 0, w, h, 0, 0);

//paint the block
g.setColor(43,128,255);
g.fillRoundRect(0,count,40,40,40,40);
g.setColor(207,128,255);
g.fillRoundRect(count,0,40,40,40,40);
g.setColor(43,128,255);
g.fillRoundRect(190,count,40,40,40,40);
g.setColor(207,128,255);
g.fillRoundRect(count,190,40,40,40,40);
}

//the main loop
public void run() {
while(true) {
count++;
// my bootleg delay
while(counter < 1000000){
counter++;
}
repaint();
count %= h;
}
}
}


Form


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.midlet.MIDlet;

public class CanvasMenu extends MIDlet implements CommandListener {

// The MIDlet's Display object
private Display display;

// Flag indicating first call of startApp
protected boolean started;

// Our Exit command
private Command exitCommand;

protected void startApp() {

//for debugging
System.out.println("I'm starting!!! - " + started);

if (!started) {
display = Display.getDisplay(this);

Canvas canvas = new KeyFinderCanvas();

exitCommand = new Command("Exit", Command.EXIT, 0);
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
display.setCurrent(canvas);
started = true;
System.out.println("Application Started! Yay! - " + started);
}
}

protected void pauseApp() {
}

protected void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
// Exit. No need to call destroyApp
// because it is empty.
notifyDestroyed();
}
}
}



//KeyFinderCanvas is a separate canvas which displays keys and codes
class KeyFinderCanvas extends Canvas {

static int[] keyCodes = {KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9};

int lastKeyCode = 0;

int lastX;

int lastY;

int background = 0x000000;
int keyColor = 0xffffff;

int width = getWidth();
int height = getHeight();


protected void keyPressed(int keyCode) {
lastKeyCode = keyCode;
repaint();
}

protected void keyRepeated(int keyCode) {
lastKeyCode = keyCode;
repaint();
}

protected void keyReleased(int keyCode) {
lastKeyCode = 0;
// repaint();
}


protected void paint(Graphics g) {

//g.setColor(0xAEB5CE);
g.setColor(background, 120, 240);
//this is not smart, instead of doing this - what would you do?
// I would call "get width" and "get height" once, not on each paint
g.fillRect(0, 0, width, height);

g.setColor(0xffffff);
Font font = g.getFont();
font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
g.setFont(font);
g.drawString("1", width/3-10, height/3-20, Graphics.TOP | Graphics.LEFT);
g.drawString("2", width/3+width/3-10, height/3-20, Graphics.TOP | Graphics.LEFT);
g.drawString("3", width-10, height/3-20, Graphics.TOP | Graphics.LEFT);
g.drawString("4", width/3-10, height/3+height/3-20, Graphics.TOP | Graphics.LEFT);
g.drawString("5", width/3+width/3-10, height/3+height/3-20, Graphics.TOP | Graphics.LEFT);
g.drawString("6", width-10, height/3+height/3-20, Graphics.TOP | Graphics.LEFT);
g.drawString("7", width/3-10, height-20, Graphics.TOP | Graphics.LEFT);
g.drawString("8", width/3+width/3-10, height-20, Graphics.TOP | Graphics.LEFT);
g.drawString("9", width-10, height-20, Graphics.TOP | Graphics.LEFT);


if (lastKeyCode == 0) {
g.setColor(background, 120, 240);
}

if (lastKeyCode != 0) {
for (int i = 0; i if (lastKeyCode == keyCodes[i]) {
break;
}
}


// PRESS 1
if (lastKeyCode == KEY_NUM1){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(0, 0, width/3, height/3);
g.setColor(0xffffff);
g.drawString("1", width/3-10, height/3-20, Graphics.TOP | Graphics.LEFT);
}

// PRESS 2
if (lastKeyCode == KEY_NUM2){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(width/3, 0, width/3, height/3);
g.setColor(0xffffff);
g.drawString("2", width/3+width/3-10, height/3-20, Graphics.TOP | Graphics.LEFT);
}

// PRESS 3
if (lastKeyCode == KEY_NUM3){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(width/3+width/3, 0, width/3, height/3);
g.setColor(0xffffff);
g.drawString("3", width-10, height/3-20, Graphics.TOP | Graphics.LEFT);
}

// PRESS 4
if (lastKeyCode == KEY_NUM4){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(0, height/3, width/3, height/3);
g.setColor(0xffffff);
g.drawString("4", width/3-10, height/3+height/3-20, Graphics.TOP | Graphics.LEFT);
}

// PRESS 5
if (lastKeyCode == KEY_NUM5){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(width/3, height/3, width/3, height/3);
g.setColor(0xffffff);
g.drawString("5", width/3+width/3-10, height/3+height/3-20, Graphics.TOP | Graphics.LEFT);
}

// PRESS 6
if (lastKeyCode == KEY_NUM6){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(width/3+width/3, height/3, width/3, height/3);
g.setColor(0xffffff);
g.drawString("6", width-10, height/3+height/3-20, Graphics.TOP | Graphics.LEFT);
}

// PRESS 7
if (lastKeyCode == KEY_NUM7){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(0, height/3+height/3, width/3, height/3);
g.setColor(0xffffff);
g.drawString("7", width/3-10, height-20, Graphics.TOP | Graphics.LEFT);
}

// PRESS 8
if (lastKeyCode == KEY_NUM8){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(width/3, height/3+height/3, width/3, height/3);
g.setColor(0xffffff);
g.drawString("8", width/3+width/3-10, height-20, Graphics.TOP | Graphics.LEFT);
}

// PRESS 9
if (lastKeyCode == KEY_NUM9){
keyColor = keyColor * -25;
if (keyColor <= 0)
{
keyColor=keyColor * -1 + 100;
}
g.setColor(keyColor);
g.fillRect(width/3+width/3, height/3+height/3, width/3, height/3);
g.setColor(0xffffff);
g.drawString("9", width-10, height-20, Graphics.TOP | Graphics.LEFT);
}
}
}
}