473,769 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

J2ME - Better way to write this ?

2 New Member
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 2323
hsn
237 New Member
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
jaxx0rr
2 New Member
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 MVP
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
2661
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
17295
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 using a canvas!. Maybe I can attach my Canvas to a form? Ideas? Oz.
0
3397
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 working on our authentication scheme, and trying to figure out the best approach that will: * Pass the ID and password via HTTP from the J2ME app to an IIS server (preferably encrypted) * Authenticate the ID/password combination against an...
0
2288
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 of the devices I've come across that use J2ME are consumer devices like phones and PDAs. I would be interested in hearing from anyone who might be using J2ME on devices other than consumer type devices. What kind of processor are you using? ...
1
4058
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 application works fine on some devices. But at Samsung mobile phone, I have a problem. In this phone my 2 commands appear under menu command, and then I can
1
3560
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 phones have very limited resources so the interpreter should be small and fast. Does anyone know of a simple scripting language source to handle if ( ) { } else {} and while ( ) { } and expressions and function calls back into Java. I'm using...
1
2235
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 there a way my Java aplicatio can accest SMS inbox on my GSM mobile phone and get data from it? For example lets say I want to make a game. In the game I hava the
1
7956
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. If this is possible, I would be grateful for any pointers to a Jython/J2ME reference. -clay
2
3366
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( any application of j2me which is complete) for study my email: <deleted>
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.