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

2 comments:

  1. hi
    i visit your weblog
    i try to used java like you
    i happy if you slect me as a friend and interactive for java application and share our
    Experience

    ReplyDelete
  2. i want programing with java but i am beginer like you i whappy if you select me as a friend for interactive and share our Experience aboute java
    thanks

    ReplyDelete