Connecting Tech Pros Worldwide Forums | Help | Site Map

J2ME - Better way to write this ?

Newbie
 
Join Date: Jan 2008
Posts: 2
#1: Jan 29 '08
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.  

hsn's Avatar
hsn hsn is offline
Familiar Sight
 
Join Date: Sep 2007
Location: Dubai-UAE
Posts: 237
#2: Jan 29 '08

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.
Newbie
 
Join Date: Jan 2008
Posts: 2
#3: Jan 29 '08

re: J2ME - Better way to write this ?


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
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jan 29 '08

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.
Reply


Similar Java bytes