Showing posts with label j2me. Show all posts
Showing posts with label j2me. Show all posts

Saturday, December 27, 2008

Playing with Graphics

Now, i want to make an application that draw a line, and i have to matter with MIDlet image class, and MIDlet Graphics. So here it is.

...
public class DrawLine extends MIDlet implements CommandListener
{

Form screen;
Image line;
TextField input_length;
Graphics gr;

/**
* Called by the Application Management Software (AMS) when the MIDlet
* begins execution. The AMS is the software on a device that manages
* the downloading and life-cycle of MIDlets. When this constructor
* returns, the AMS places the MIDlet in the Paused state.
*/
public DrawLine()
{
screen = new Form("Draw.. Line...");
line = Image.createImage(screen.getWidth(),50);
gr = line.getGraphics();
input_length = new TextField("Input Length Here","",10,TextField.NUMERIC);
screen.append(input_length);
screen.append(line);
screen.addCommand(new Command("Draw", Command.OK, 0));
screen.addCommand(new Command("Exit", Command.EXIT, 0));
screen.setCommandListener(this);
}
...
..
public void startApp()
{
Display.getDisplay(this).setCurrent(screen);
}
..
..
public void commandAction(Command c, Displayable d)
{

if(c.getLabel().equals("Exit"))
{
notifyDestroyed();
}
else if(c.getLabel().equals("Draw"))
{
Graphics g = line.getGraphics();
int length = Integer.parseInt(input_length.getString());
screen.delete(1);
g.setColor(255,255,255);
g.drawLine(0,20,g.getClipWidth(),20); // delete previouse image with white color ^_^"
g.setColor(0,0,0);
g.drawLine(0,20,length,20);
input_length.setString("");
screen.append(line);
}

}
}

Compile, Deploy, just input the length to the text box, and then click draw, it should draw a line with your input length in pixels.
After succeed with that, i want to make improvements. I crated a new class called DrawRectangle. I just add a new input field (TextField) because it's a rectangle who have width and height.

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

/**
* Class DrawRectangle - a simple MIDlet application that create rectangle image with user input
*
* @author Keenan M
* @version 1.0 :P
*/

public class DrawRectangle extends MIDlet implements CommandListener
{

Form screen;
Image rectangle;
TextField input_width;
TextField input_height;
Graphics gr;

public DrawRectangle()
{
screen = new Form("Draw.. Rectangle...");
rectangle = Image.createImage(screen.getWidth(),screen.getHeight());
gr = rectangle.getGraphics();
input_width = new TextField("Input Length Here","",10,TextField.NUMERIC);
input_height = new TextField("Input Length Here","",10,TextField.NUMERIC);
screen.append(input_width);
screen.append(input_height);
screen.append(rectangle);
screen.addCommand(new Command("Draw", Command.OK, 0));
screen.addCommand(new Command("Exit", Command.EXIT, 0));
screen.setCommandListener(this);
}

public void startApp()
{
Display.getDisplay(this).setCurrent(screen);
}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
}

public void commandAction(Command c, Displayable d)
{

if(c.getLabel().equals("Exit"))
{
notifyDestroyed();
}
else if(c.getLabel().equals("Draw"))
{
Graphics gr = rectangle.getGraphics();
int width = Integer.parseInt(input_width.getString());
int height = Integer.parseInt(input_height.getString());
screen.delete(2);
/** it's a clearing time */
gr.setColor(255,255,255);
gr.fillRect(1,1,screen.getWidth(),screen.getHeight());
/** end of clearing */
gr.setColor(0,0,0);
gr.drawRect(1,1,width,height);
input_width.setString("");
input_height.setString("");
screen.append(rectangle);
}
}
}

Compile, Deploy, it's work, that code make me draw a rectangle :D.

updated!
Hmm, I think I should rename my Application to Drawer. woke, it's done.
Now, i want to make an application that make a String Image. with method drawString() in the graphics class. there it is.

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

/**
* Class DrawString - Draw string image
*
* @author Keenan
* @version 1.0
*/
public class DrawString extends MIDlet implements CommandListener
{

Form screen;
Image st;
TextField input;
Graphics gr;

public DrawString()
{
screen = new Form("Draw.. String...");
st = Image.createImage(screen.getWidth(),15); // << string
gr = st.getGraphics();
input = new TextField("Input Here","",6+1+6+1+4,TextField.ANY);
screen.append(input);
screen.addCommand(new Command("Draw", Command.OK, 0));
screen.addCommand(new Command("Exit", Command.EXIT, 1));
screen.setCommandListener(this);
}

public void startApp()
{
Display.getDisplay(this).setCurrent(screen);
}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
}

public void commandAction(Command c, Displayable d)
{
if(c.getLabel().equals("Exit"))
{
notifyDestroyed();
}
else if(c.getLabel().equals("Draw"))
{
Graphics g = st.getGraphics();
String v = input.getString();
//screen.delete(1); << so it's like log message
g.setColor(255,255,255);
g.fillRect(0,0,st.getWidth(),st.getHeight());
g.setColor(0,0,0);
g.drawString(v, 2,0,Graphics.LEFT|Graphics.TOP);
input.setString("");
screen.append(st);
}
}

}

Compilelalalala, Deploylalala, it works! :D

Making a Simple Calculator

I want to make a Calculator !


...
....
public class MyCalculator extends MIDlet implements CommandListener
{
...
....

After that, I preparing for the Item that i want to display in the screen
The item is :
3 TextField for inputing first value, second value, and the result.
1 ChoiceGroup for selecting the operator.
2 Command, one for exit, one for Calc(ulating) :D.
So..

..
...
private TextField fv = new TextField("First Value", "", 10, TextField.NUMERIC);
private TextField sv = new TextField("Second Value", "", 10, TextField.NUMERIC);
private TextField r = new TextField("Result", "", 50, TextField.ANY);
String cgOperator[] = {"Divide","Times","Add","Subtract"};
private ChoiceGroup operator = new ChoiceGroup("Operator",ChoiceGroup.EXCLUSIVE, cgOperator, null);
.....
.....

And of course, the commandAction() method..

...
..
public void commandAction(Command c, Displayable d) {
// If it's our exitCommand, destroy ourselves
if(c.getLabel().equals("Exit")) {
notifyDestroyed();
}else if(c.getLabel().equals("Calc")) {
int first_value = Integer.parseInt(fv.getString());
int second_value = Integer.parseInt(sv.getString());
double result = 0;
if(operator.isSelected(0))
{
result = first_value / second_value;
}else if(operator.isSelected(1))
{
result = first_value * second_value;
}else if(operator.isSelected(2))
{
result = first_value + second_value;
}else if(operator.isSelected(3))
{
result = first_value - second_value;

}
r.setString(""+result+""); // <<< (i can't parseDouble() it.. :D)
}
}
...
..

Compile it, Deploy it, and I get my MIDlet Calculator !!! yeah

Thursday, December 25, 2008

Beginning of all : Making Mobile Application with BlueJ !

Hello, my name is Keenan, and i want to learn how to make mobile application. In internet, i always find a great tutorials, great resource, and many more. I do love free, my teacher is internet. For now, I cant making any good mobile application using Java with BlueJ. I want too learn how to make good mobile applications ! I made this blog for posting my progress/improvement of making good mobile application !

So lets get started. You need JRE, download here.
I'm using BlueJ for this. You can download BlueJ here.
You need Wireless Toolkit 2.5.2 too ! download here.
That's it, i think that's is what i need.

I'm ready! did you ?

Okay, my first step, is searching in the bluej website for j2me. I got it here. After done reading that page and try they "command". I now can make simple application.

Now, time to edit the source code, of the Hello MIDlet that I downloaded. After seeking from the javax.microedition.lcdui API (you can see it at WTK 2.5.2 folder(C:\WTK2.5.2_01\docs\api\midp\index.html)). I found an Alert class, maybe i can use it. So, I edited the source code so like this !

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

/**
* Class Hello - A simple Hello World MIDlet
*
* @author Ian Utting
* @version 1.0
*/
public class Hello extends MIDlet implements CommandListener
{
private Form screen;
private Alert dor = new Alert("DOR !");/** HERE !*/
private int a; /**HERE !!!*/

public Hello()
{
screen = new Form("Hello Midlet");
dor.setTimeout(1500); /**HERE !!!*/
dor.setString(a + " Times.."); /**HERE !!!*/
screen.append("Hello small world!");
screen.addCommand(new Command("Exit", Command.EXIT, 0));
screen.setCommandListener(this);
}
..............
.......

Hmm, I think to make application, that every time we click button, the alert will show, and print out many times we click that button ! for that, i make a variable called 'a'. And after that...

...
....
public void commandAction(Command c, Displayable d) {
// If it's our exitCommand, destroy ourselves
if(c.getLabel().equals("Exit")) {
notifyDestroyed();
}
if(c.getLabel().equals("Dor")) { /**I add this !!!*/
Display.getDisplay(this).setCurrent(dor);
a += 1;
dor.setString( a + " Times..");
}
}

I compile that, and then deploy. It's Work.

Now, i want display the clicked times in the form. I think, I will use text field.
I add this to the code

...
.....
public class Hello extends MIDlet implements CommandListener
{
private Form screen;
private TextField tf; /**HERE !!*/
private int a = 0;
...
...

and then, in the Hello constructor

...
.....
public Hello()
{
screen = new Form("Hello Midlet");
tf = new TextField("hehe..","Hello small world!",255,TextField.ANY);
screen.append(tf);
screen.addCommand(new Command("Exit", Command.EXIT, 0));
screen.addCommand(new Command("Dor", Command.ITEM, 0));
screen.setCommandListener(this);
}
...
..

After that, in the command listener.

public void commandAction(Command c, Displayable d) {
// If it's our exitCommand, destroy ourselves
if(c.getLabel().equals("Exit")) {
notifyDestroyed();
}
if(c.getLabel().equals("Dor")) {
a += 1;
tf.setString("Hello small world!"+" \n "+ a +" Times :)");
}
}

Compile, Deploy, It's Work ! :D