Wednesday, June 8, 2011

J2ME: Graphic Text and Bars



Above J2ME graphic demonstration is a combination between text display, for the menu, and canvas for the graphic, here is the source code, each screen above is on separate file.


/*
* Filename : GraphicDemo.java
*/
package GraphicDemo;

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

public class GraphicDemo extends MIDlet implements CommandListener {

private Command exitCommand; // The exit command
private Display display; // The display for this MIDlet
List lsMain;
GDBars GDBars = null;
GDText GDText = null;
static final Command EXIT_CMD = new Command("Exit", Command.EXIT, 1);
static final Command BACK_CMD = new Command("Back", Command.BACK, 1);

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



lsMain = new List("Graphic Demo menu", Choice.IMPLICIT);

lsMain.append("Bars Demo",null);
lsMain.append("Text Demo",null);

lsMain.addCommand(EXIT_CMD);

lsMain.setCommandListener(this);
GDBars = new GDBars(this, lsMain);
GDText = new GDText(this, lsMain);
}

public void startApp() {
display.setCurrent(lsMain);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void setDisplay(Displayable d)
{
display.setCurrent(d);
}

public void commandAction(Command c, Displayable s) {
if (s instanceof List) {
List obj = (List)s;

if (obj == lsMain) {
if (c == EXIT_CMD)
{
destroyApp(false);
notifyDestroyed();
}
else {
switch(lsMain.getSelectedIndex()) {
case 0:
display.setCurrent(GDBars);
break;
case 1:
display.setCurrent(GDText);
break;

}
}
}
}
}

}



/*
* Filename : GDText.java
*/

package GraphicDemo;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Font;

/**
Draws Text on a Canvas using the drawing methods
in the javax.microedition.lcdui.Graphics class.
@see javax.microedition.lcdui.Graphics

*/
public class GDText extends Canvas implements CommandListener
{
// Constant representing the color white.
private static final int WHITE = 0xFF << 16 | 0xFF << 8 | 0xFF;

private Command back = new Command("Back",Command.BACK,1);

GraphicDemo graphicdemo; // Reference to display object
Displayable backscreen;
//private Display display = Display.getDisplay(GraphicsDemo.getInstance());

public GDText ( GraphicDemo graphicdemo, Displayable backscreen )
{
super();
this.graphicdemo = graphicdemo;
this.backscreen = backscreen;

addCommand(back);
setCommandListener(this);
}
/**
Paints the clip rectangle white, effectively erasing
whatever was displayed on the Canvas previously.
*/
protected void paintClipRect(Graphics g)
{
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipH = g.getClipHeight();
int clipW = g.getClipWidth();
int color = g.getColor();
g.setColor(WHITE);
g.fillRect(clipX, clipY, clipW, clipH);
g.setColor(color);
}

/**
Paints the look of this Canvas subclass.
*/
public void paint(Graphics g)
{
paintClipRect(g);
int width = getWidth();
int height = getHeight();
g.setFont(Font.getDefaultFont());
g.drawString("Default", 5, 30,Graphics.LEFT | Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString("Large", 5, 53,Graphics.LEFT | Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_ITALIC,Font.SIZE_MEDIUM));
g.drawString("Medium", 5, 71,Graphics.LEFT | Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_UNDERLINED,Font.SIZE_SMALL));
g.drawString("Small", 5, 90,Graphics.LEFT | Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_MEDIUM));
g.drawString("V", width - 10, 20,Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("E", width - 10, 32,Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("R", width - 10, 44,Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("T", width - 10, 56,Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("I", width - 10, 68,Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("C", width - 10, 80,Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("A", width - 10, 92,Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("L", width - 10, 104,Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('B', width - 25, 20,Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('O', width - 25, 32,Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('L', width - 25, 44,Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('D', width - 25, 56,Graphics.RIGHT | Graphics.BOTTOM);

}
public void commandAction(Command c, Displayable d)
{
if (c == back)
{
graphicdemo.setDisplay(backscreen);
}
}
}




/*
* Filename : GDBars.java
*/

package GraphicDemo;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Command;

/**
Draws rectangles on a Canvas using the drawing methods
in the javax.microedition.lcdui.Graphics class.
@see javax.microedition.lcdui.Graphics

*/
public class GDBars extends Canvas implements CommandListener
{
// Constant representing the color white.
private static final int WHITE = 0xFF << 16 | 0xFF << 8 | 0xFF;

private Command back = new Command("Back",Command.BACK,1);

GraphicDemo graphicdemo; // Reference to display object
Displayable backscreen;
//private Display display = Display.getDisplay(GraphicsDemo.getInstance());

public GDBars ( GraphicDemo graphicdemo, Displayable backscreen )
{
super();
this.graphicdemo = graphicdemo;
this.backscreen = backscreen;

addCommand(back);
setCommandListener(this);
}
/**
Paints the clip rectangle white, effectively erasing
whatever was displayed on the Canvas previously.
*/
protected void paintClipRect(Graphics g)
{
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipH = g.getClipHeight();
int clipW = g.getClipWidth();
int color = g.getColor();
g.setColor(WHITE);
g.fillRect(clipX, clipY, clipW, clipH);
g.setColor(color);
}

/**
Paints the look of this Canvas subclass.
*/
public void paint(Graphics g)
{
paintClipRect(g);

int width = getWidth();
int height = getHeight();
int x0 = 5;
int y0 = 5;
int barW = 10;
int initHeight = height - 10;
int deltaH = 10;
g.drawRect(x0, y0, barW, initHeight);
g.fillRect(x0 + barW, y0 + deltaH, barW,
initHeight - deltaH + 1);
g.drawRect(x0 + barW * 2, y0 + deltaH * 2,
barW, initHeight - deltaH * 2);
g.setColor(255, 00, 00);
g.fillRect(x0 + barW * 3, y0 + deltaH * 3,
barW, initHeight - deltaH * 3 + 1);
g.setColor(0, 0, 0);
g.drawRect(x0 + barW * 4, y0 + deltaH * 4,
barW, initHeight - deltaH * 4);
g.fillRect(x0 + barW * 5, y0 + deltaH * 5,
barW, initHeight - deltaH * 5 + 1);
g.drawRect(x0 + barW * 6, y0 + deltaH * 6,
barW, initHeight - deltaH * 6);
g.fillRect(x0 + barW * 7, y0 + deltaH * 7,
barW, initHeight - deltaH * 7 + 1);

}
public void commandAction(Command c, Displayable d)
{
if (c == back)
{
graphicdemo.setDisplay(backscreen);
}
}
}

No comments:

current

last archive