J2ME - Better way to write this ? | Newbie | | Join Date: Jan 2008
Posts: 2
| |
This will probably look like a really stupid piece of code.. but it works
My question is, how can I write the last part so there is only one extended Canvas class that can recieve a parameter for the image number
Thanks -
-
import java.io.IOException;
-
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.lcdui.Image;
-
import javax.microedition.lcdui.List;
-
import javax.microedition.midlet.MIDlet;
-
-
public class MyImages extends MIDlet implements CommandListener {
-
-
// The MIDlet's Display object
-
private Display mydisplay;
-
-
// Flag indicating first call of startApp
-
protected boolean started;
-
-
// Exit command
-
private Command exitCommand;
-
-
// Back to examples list command
-
private Command backCommand;
-
-
// The example selection list
-
private List examplesList;
-
-
// The Canvases used to demonstrate different Items
-
private Canvas[] canvases;
-
-
// The example names. Used to populate the list.
-
private String[] examples = {
-
"Image001", "Image002", "Image003", "Image004", "Image005", "Image006"
-
};
-
-
protected void startApp() {
-
if (!started) {
-
started = true;
-
mydisplay = Display.getDisplay(this);
-
-
// Create the common commands
-
exitCommand = new Command("Exit", Command.EXIT, 0);
-
backCommand = new Command("Back", Command.BACK, 1);
-
-
// Create the canvases
-
canvases = new Canvas[examples.length];
-
canvases[0] = createDrawImageCanvas(0);
-
canvases[1] = createDrawImageCanvas(1);
-
canvases[2] = createDrawImageCanvas(2);
-
canvases[3] = createDrawImageCanvas(3);
-
canvases[4] = createDrawImageCanvas(4);
-
canvases[5] = createDrawImageCanvas(5);
-
-
// Create the list of examples
-
examplesList = new List("Select a picture", List.IMPLICIT);
-
for (int i = 0; i < examples.length; i++) {
-
examplesList.append(examples[i], null);
-
}
-
examplesList.setCommandListener(this);
-
-
// Start with the List
-
mydisplay.setCurrent(examplesList);
-
}
-
}
-
-
protected void pauseApp() {
-
}
-
-
protected void destroyApp(boolean unconditional) {
-
}
-
-
public void commandAction(Command c, Displayable d) {
-
if (d == examplesList) {
-
// New example selected
-
int index = examplesList.getSelectedIndex();
-
mydisplay.setCurrent(canvases[index]);
-
} else if (c == exitCommand) {
-
// Exit. No need to call destroyApp
-
// because it is empty.
-
notifyDestroyed();
-
} else if (c == backCommand) {
-
// Go back to main selection list
-
mydisplay.setCurrent(examplesList);
-
}
-
}
-
-
-
// Create the Canvas for the image drawing example
-
private Canvas createDrawImageCanvas(int idx) {
-
-
switch (idx) {
-
case 0 :
-
Canvas canvasx0 = new DrawImageCanvas0();
-
canvasx0.addCommand(exitCommand);
-
canvasx0.addCommand(backCommand);
-
canvasx0.setCommandListener(this);
-
return canvasx0;
-
case 1 :
-
Canvas canvasx1 = new DrawImageCanvas1();
-
canvasx1.addCommand(exitCommand);
-
canvasx1.addCommand(backCommand);
-
canvasx1.setCommandListener(this);
-
return canvasx1;
-
case 2 :
-
Canvas canvasx2 = new DrawImageCanvas2();
-
canvasx2.addCommand(exitCommand);
-
canvasx2.addCommand(backCommand);
-
canvasx2.setCommandListener(this);
-
return canvasx2;
-
case 3 :
-
Canvas canvasx3 = new DrawImageCanvas3();
-
canvasx3.addCommand(exitCommand);
-
canvasx3.addCommand(backCommand);
-
canvasx3.setCommandListener(this);
-
return canvasx3;
-
case 4 :
-
Canvas canvasx4 = new DrawImageCanvas4();
-
canvasx4.addCommand(exitCommand);
-
canvasx4.addCommand(backCommand);
-
canvasx4.setCommandListener(this);
-
return canvasx4;
-
case 5 :
-
Canvas canvasx5 = new DrawImageCanvas5();
-
canvasx5.addCommand(exitCommand);
-
canvasx5.addCommand(backCommand);
-
canvasx5.setCommandListener(this);
-
return canvasx5;
-
}
-
-
Canvas canvasx0 = new DrawImageCanvas0();
-
canvasx0.addCommand(exitCommand);
-
canvasx0.addCommand(backCommand);
-
canvasx0.setCommandListener(this);
-
return canvasx0;
-
-
}
-
-
}
-
-
// A canvas that illustrates image drawing
-
class DrawImageCanvas0 extends Canvas {
-
static Image image;
-
-
public void paint(Graphics g) {
-
int width = getWidth();
-
int height = getHeight();
-
int idx = 1;
-
-
// Fill the background using black
-
g.setColor(0);
-
g.fillRect(0, 0, width, height);
-
-
// Load an image from the MIDlet resources
-
if (image == null) {
-
try {
-
image = Image.createImage("/earth" + 0 + ".png");
-
} catch (IOException ex) {
-
g.setColor(0xffffff);
-
g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
-
return;
-
}
-
}
-
-
g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
-
-
}
-
}
-
-
-
// A canvas that illustrates image drawing
-
class DrawImageCanvas1 extends Canvas {
-
static Image image;
-
-
public void paint(Graphics g) {
-
int width = getWidth();
-
int height = getHeight();
-
int idx = 1;
-
-
// Fill the background using black
-
g.setColor(0);
-
g.fillRect(0, 0, width, height);
-
-
// Load an image from the MIDlet resources
-
if (image == null) {
-
try {
-
image = Image.createImage("/earth" + 1 + ".png");
-
} catch (IOException ex) {
-
g.setColor(0xffffff);
-
g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
-
return;
-
}
-
}
-
-
g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
-
-
}
-
}
-
-
// A canvas that illustrates image drawing
-
class DrawImageCanvas2 extends Canvas {
-
static Image image;
-
-
public void paint(Graphics g) {
-
int width = getWidth();
-
int height = getHeight();
-
int idx = 1;
-
-
// Fill the background using black
-
g.setColor(0);
-
g.fillRect(0, 0, width, height);
-
-
// Load an image from the MIDlet resources
-
if (image == null) {
-
try {
-
image = Image.createImage("/earth" + 2 + ".png");
-
} catch (IOException ex) {
-
g.setColor(0xffffff);
-
g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
-
return;
-
}
-
}
-
-
g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
-
-
}
-
}
-
-
// A canvas that illustrates image drawing
-
class DrawImageCanvas3 extends Canvas {
-
static Image image;
-
-
public void paint(Graphics g) {
-
int width = getWidth();
-
int height = getHeight();
-
int idx = 1;
-
-
// Fill the background using black
-
g.setColor(0);
-
g.fillRect(0, 0, width, height);
-
-
// Load an image from the MIDlet resources
-
if (image == null) {
-
try {
-
image = Image.createImage("/earth" + 3 + ".png");
-
} catch (IOException ex) {
-
g.setColor(0xffffff);
-
g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
-
return;
-
}
-
}
-
-
g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
-
-
}
-
}
-
-
// A canvas that illustrates image drawing
-
class DrawImageCanvas4 extends Canvas {
-
static Image image;
-
-
public void paint(Graphics g) {
-
int width = getWidth();
-
int height = getHeight();
-
int idx = 1;
-
-
// Fill the background using black
-
g.setColor(0);
-
g.fillRect(0, 0, width, height);
-
-
// Load an image from the MIDlet resources
-
if (image == null) {
-
try {
-
image = Image.createImage("/earth" + 4 + ".png");
-
} catch (IOException ex) {
-
g.setColor(0xffffff);
-
g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
-
return;
-
}
-
}
-
-
g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
-
-
}
-
}
-
-
// A canvas that illustrates image drawing
-
class DrawImageCanvas5 extends Canvas {
-
static Image image;
-
-
public void paint(Graphics g) {
-
int width = getWidth();
-
int height = getHeight();
-
int idx = 1;
-
-
// Fill the background using black
-
g.setColor(0);
-
g.fillRect(0, 0, width, height);
-
-
// Load an image from the MIDlet resources
-
if (image == null) {
-
try {
-
image = Image.createImage("/earth" + 5 + ".png");
-
} catch (IOException ex) {
-
g.setColor(0xffffff);
-
g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
-
return;
-
}
-
}
-
-
g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
-
-
}
-
}
-
-
|  | Familiar Sight | | Join Date: Sep 2007 Location: Dubai-UAE
Posts: 237
| | | re: J2ME - Better way to write this ?
hi
me my self i am learning J2ME
and i have a problem with downloading and installing it.
can you tell me how to do it plzzzzzzzzzzzz.
i really need to know how to do it.
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | re: J2ME - Better way to write this ?
You just need to put the image creation code in a method that takes the image name/path as a variable. You then call that method passing it different values for the name/path.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|