472,978 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 software developers and data experts.

J2ME - Better way to write this ?

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.IOException;
  3. import javax.microedition.lcdui.Canvas;
  4. import javax.microedition.lcdui.Command;
  5. import javax.microedition.lcdui.CommandListener;
  6. import javax.microedition.lcdui.Display;
  7. import javax.microedition.lcdui.Displayable;
  8. import javax.microedition.lcdui.Font;
  9. import javax.microedition.lcdui.Graphics;
  10. import javax.microedition.lcdui.Image;
  11. import javax.microedition.lcdui.List;
  12. import javax.microedition.midlet.MIDlet;
  13.  
  14. public class MyImages extends MIDlet implements CommandListener {
  15.  
  16.     // The MIDlet's Display object
  17.     private Display mydisplay;
  18.  
  19.     // Flag indicating first call of startApp
  20.     protected boolean started;
  21.  
  22.     // Exit command
  23.     private Command exitCommand;
  24.  
  25.     // Back to examples list command
  26.     private Command backCommand;
  27.  
  28.     // The example selection list
  29.     private List examplesList;
  30.  
  31.     // The Canvases used to demonstrate different Items
  32.     private Canvas[] canvases;
  33.  
  34.     // The example names. Used to populate the list.
  35.     private String[] examples = {
  36.         "Image001", "Image002", "Image003", "Image004", "Image005", "Image006"
  37.     };
  38.  
  39.     protected void startApp() {
  40.         if (!started) {
  41.             started = true;
  42.             mydisplay = Display.getDisplay(this);
  43.  
  44.             // Create the common commands
  45.             exitCommand = new Command("Exit", Command.EXIT, 0);
  46.             backCommand = new Command("Back", Command.BACK, 1);
  47.  
  48.             // Create the canvases
  49.             canvases = new Canvas[examples.length];
  50.             canvases[0] = createDrawImageCanvas(0);
  51.             canvases[1] = createDrawImageCanvas(1);
  52.             canvases[2] = createDrawImageCanvas(2);
  53.             canvases[3] = createDrawImageCanvas(3);
  54.             canvases[4] = createDrawImageCanvas(4);
  55.             canvases[5] = createDrawImageCanvas(5);
  56.  
  57.             // Create the list of examples
  58.             examplesList = new List("Select a picture", List.IMPLICIT);
  59.             for (int i = 0; i < examples.length; i++) {
  60.                 examplesList.append(examples[i], null);
  61.             } 
  62.             examplesList.setCommandListener(this);
  63.  
  64.             // Start with the List
  65.             mydisplay.setCurrent(examplesList);
  66.         }
  67.     }
  68.  
  69.     protected void pauseApp() {
  70.     }
  71.  
  72.     protected void destroyApp(boolean unconditional) {
  73.     }
  74.  
  75.     public void commandAction(Command c, Displayable d) {
  76.         if (d == examplesList) {
  77.             // New example selected
  78.             int index = examplesList.getSelectedIndex();
  79.             mydisplay.setCurrent(canvases[index]);
  80.         } else if (c == exitCommand) {
  81.             // Exit. No need to call destroyApp
  82.             // because it is empty.
  83.             notifyDestroyed();
  84.         } else if (c == backCommand) {
  85.             // Go back to main selection list
  86.             mydisplay.setCurrent(examplesList);
  87.         }
  88.     }
  89.  
  90.  
  91.     // Create the Canvas for the image drawing example
  92.     private Canvas createDrawImageCanvas(int idx) {
  93.  
  94.         switch (idx) {
  95.         case 0 :
  96.         Canvas canvasx0 = new DrawImageCanvas0();        
  97.         canvasx0.addCommand(exitCommand);
  98.         canvasx0.addCommand(backCommand);
  99.         canvasx0.setCommandListener(this);
  100.         return canvasx0;
  101.         case 1 :
  102.         Canvas canvasx1 = new DrawImageCanvas1();        
  103.         canvasx1.addCommand(exitCommand);
  104.         canvasx1.addCommand(backCommand);
  105.         canvasx1.setCommandListener(this);
  106.         return canvasx1;
  107.         case 2 :
  108.         Canvas canvasx2 = new DrawImageCanvas2();        
  109.         canvasx2.addCommand(exitCommand);
  110.         canvasx2.addCommand(backCommand);
  111.         canvasx2.setCommandListener(this);
  112.         return canvasx2;
  113.         case 3 :
  114.         Canvas canvasx3 = new DrawImageCanvas3();        
  115.         canvasx3.addCommand(exitCommand);
  116.         canvasx3.addCommand(backCommand);
  117.         canvasx3.setCommandListener(this);
  118.         return canvasx3;
  119.         case 4 :
  120.         Canvas canvasx4 = new DrawImageCanvas4();        
  121.         canvasx4.addCommand(exitCommand);
  122.         canvasx4.addCommand(backCommand);
  123.         canvasx4.setCommandListener(this);
  124.         return canvasx4;
  125.         case 5 :
  126.         Canvas canvasx5 = new DrawImageCanvas5();        
  127.         canvasx5.addCommand(exitCommand);
  128.         canvasx5.addCommand(backCommand);
  129.         canvasx5.setCommandListener(this);
  130.         return canvasx5;
  131.         }
  132.  
  133.         Canvas canvasx0 = new DrawImageCanvas0();        
  134.         canvasx0.addCommand(exitCommand);
  135.         canvasx0.addCommand(backCommand);
  136.         canvasx0.setCommandListener(this);
  137.         return canvasx0;
  138.  
  139.     } 
  140.  
  141. }
  142.  
  143. // A canvas that illustrates image drawing
  144. class DrawImageCanvas0 extends Canvas {
  145.     static Image image;
  146.  
  147.     public void paint(Graphics g) {
  148.         int width = getWidth();
  149.         int height = getHeight();
  150.         int idx = 1; 
  151.  
  152.         // Fill the background using black
  153.         g.setColor(0);
  154.         g.fillRect(0, 0, width, height);
  155.  
  156.         // Load an image from the MIDlet resources
  157.         if (image == null) {
  158.             try {
  159.                 image = Image.createImage("/earth" + 0 + ".png");
  160.             } catch (IOException ex) {
  161.                 g.setColor(0xffffff);
  162.                 g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
  163.                 return;
  164.             }
  165.         }
  166.  
  167.             g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
  168.  
  169.     }
  170. }
  171.  
  172.  
  173. // A canvas that illustrates image drawing
  174. class DrawImageCanvas1 extends Canvas {
  175.     static Image image;
  176.  
  177.     public void paint(Graphics g) {
  178.         int width = getWidth();
  179.         int height = getHeight();
  180.         int idx = 1; 
  181.  
  182.         // Fill the background using black
  183.         g.setColor(0);
  184.         g.fillRect(0, 0, width, height);
  185.  
  186.         // Load an image from the MIDlet resources
  187.         if (image == null) {
  188.             try {
  189.                 image = Image.createImage("/earth" + 1 + ".png");
  190.             } catch (IOException ex) {
  191.                 g.setColor(0xffffff);
  192.                 g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
  193.                 return;
  194.             }
  195.         }
  196.  
  197.             g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
  198.  
  199.     }
  200. }
  201.  
  202. // A canvas that illustrates image drawing
  203. class DrawImageCanvas2 extends Canvas {
  204.     static Image image;
  205.  
  206.     public void paint(Graphics g) {
  207.         int width = getWidth();
  208.         int height = getHeight();
  209.         int idx = 1; 
  210.  
  211.         // Fill the background using black
  212.         g.setColor(0);
  213.         g.fillRect(0, 0, width, height);
  214.  
  215.         // Load an image from the MIDlet resources
  216.         if (image == null) {
  217.             try {
  218.                 image = Image.createImage("/earth" + 2 + ".png");
  219.             } catch (IOException ex) {
  220.                 g.setColor(0xffffff);
  221.                 g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
  222.                 return;
  223.             }
  224.         }
  225.  
  226.             g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
  227.  
  228.     }
  229. }
  230.  
  231. // A canvas that illustrates image drawing
  232. class DrawImageCanvas3 extends Canvas {
  233.     static Image image;
  234.  
  235.     public void paint(Graphics g) {
  236.         int width = getWidth();
  237.         int height = getHeight();
  238.         int idx = 1; 
  239.  
  240.         // Fill the background using black
  241.         g.setColor(0);
  242.         g.fillRect(0, 0, width, height);
  243.  
  244.         // Load an image from the MIDlet resources
  245.         if (image == null) {
  246.             try {
  247.                 image = Image.createImage("/earth" + 3 + ".png");
  248.             } catch (IOException ex) {
  249.                 g.setColor(0xffffff);
  250.                 g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
  251.                 return;
  252.             }
  253.         }
  254.  
  255.             g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
  256.  
  257.     }
  258. }
  259.  
  260. // A canvas that illustrates image drawing
  261. class DrawImageCanvas4 extends Canvas {
  262.     static Image image;
  263.  
  264.     public void paint(Graphics g) {
  265.         int width = getWidth();
  266.         int height = getHeight();
  267.         int idx = 1; 
  268.  
  269.         // Fill the background using black
  270.         g.setColor(0);
  271.         g.fillRect(0, 0, width, height);
  272.  
  273.         // Load an image from the MIDlet resources
  274.         if (image == null) {
  275.             try {
  276.                 image = Image.createImage("/earth" + 4 + ".png");
  277.             } catch (IOException ex) {
  278.                 g.setColor(0xffffff);
  279.                 g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
  280.                 return;
  281.             }
  282.         }
  283.  
  284.             g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
  285.  
  286.     }
  287. }
  288.  
  289. // A canvas that illustrates image drawing
  290. class DrawImageCanvas5 extends Canvas {
  291.     static Image image;
  292.  
  293.     public void paint(Graphics g) {
  294.         int width = getWidth();
  295.         int height = getHeight();
  296.         int idx = 1; 
  297.  
  298.         // Fill the background using black
  299.         g.setColor(0);
  300.         g.fillRect(0, 0, width, height);
  301.  
  302.         // Load an image from the MIDlet resources
  303.         if (image == null) {
  304.             try {
  305.                 image = Image.createImage("/earth" + 5 + ".png");
  306.             } catch (IOException ex) {
  307.                 g.setColor(0xffffff);
  308.                 g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
  309.                 return;
  310.             }
  311.         }
  312.  
  313.             g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
  314.  
  315.     }
  316. }
  317.  
  318.  
Jan 29 '08 #1
3 2273
hsn
237 100+
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.
Jan 29 '08 #2
its not that complicated
.. u need to install the jdk and the wireless toolkit..
http://today.java.net/pub/a/today/20...ge=1#acquiring

and then skip to sthis part
http://today.java.net/pub/a/today/20...page=3#toolkit

cheers
Jan 29 '08 #3
r035198x
13,262 8TB
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.
Jan 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Oz Mortimer | last post by:
Hi There, Is there any way that I can display an XML page within a J2ME application? Many Thanks Oz.
6
by: Oz Mortimer | last post by:
Hi, Is there any way that I can scroll a canvas - i.e. if there are too many items on the canvas you can still access by pressing down. I know Form does this (apparently) but I need to do it...
0
by: Todd Christy | last post by:
I am building a J2ME application for a BlackBerry handheld device. This application connects to an IIS server via HTTP to execute remote ASPX-based services that collect and return data. We are...
0
by: Dave Rathnow | last post by:
We are looking at using J2ME in a embedded device that will be deployed in an industrial application and I've been doing some research to see how practical J2ME would be for our application. Most...
1
by: Sveta | last post by:
Hi, all! I am new with J2ME. I have application that has form with 2 commands (exit and select). All UI uses are high level API, and it is very important to leave it high-level. This...
1
by: Ralph Yozzo | last post by:
Hi, Does anyone know of a good place to look for a starting point -- I'm building an application on J2ME and I want to have a scripting language to both access my UI and the database RMS. Most...
1
by: Tomislav | last post by:
Hi! I have 3 questions, so here they are: 1.I would like to make an aplicaton wich can run on GSM mobile phones - SonyErricson k700 for instance. Is the J2ME what I am looking for? 2. Is...
1
by: Clay Shirky | last post by:
Can Jython be used to write applications to run in J2ME environments? I ask because Python seems an ideal language for teaching students to write applications for the phone or other mobile devices....
2
by: mayankcdac | last post by:
Hi i am a new guy in j2me field i am working in j2me gaming but i want to switch from gaming to application. i am facing some problems to build j2me application can any one give me any source code(...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.