Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
J2ME Draw Triangle, Rectangle, Arc, Line Round Rectangle Example 
 

In this serious of J2ME tutorial, we are going to describe how to draw a triangle, rectangle, arc, line or a round rectangle in one small application.

 

J2ME Draw Triangle, Rectangle, Arc, Line Round Rectangle Example

                         

In this serious of J2ME tutorial, we are going to describe how to draw a triangle, rectangle, arc, line or a round rectangle in one small application. Although we have already explained you all these examples separately but in this section we are going to merge them in one. For that we have created a list of all the items that will be used to select the particular item to run. So here we are creating a implicit type list called CanvasDrawList and defined all the elements into it. 

Now we need to use the canvas class to draw all the graphic or image. We can call the canvas class in our application through this syntax..

class CanvasDraw extends Canvas implements CommandListener

In this syntax you can see that we are creating a class called CanvasDraw which extends the canvas class through "extend" keyword. 

 

The output of the application will look like as follow....

 

 

 

 

 

Source Code of CanvasDrawExample.java

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

public class CanvasDrawExample extends MIDlet implements CommandListener{
  private Display display;
  private List list;
  private Command ok, exit;
  private CanvasDraw canvasDraw;
  int listItemIndex;

  public CanvasDrawExample(){
    canvasDraw = new CanvasDraw();
    display = Display.getDisplay(this);
    list = new List("CanvasDrawList", List.IMPLICIT);
    ok = new Command("Draw", Command.OK, 2);
    exit = new Command("Exit", Command.EXIT, 2);
    list.append("Draw Lines"null);
    list.append("Draw Rectangle"null);
    list.append("Draw Rounded Rectangle"null);
    list.append("Draw Arc"null);
    list.append("Draw Triangle"null);
    list.addCommand(ok);
    list.addCommand(exit);
    list.setCommandListener(this);
  }
  
  public void startApp(){
    display.setCurrent(list);
  }

  public void pauseApp(){}
  
  public void destroyApp(boolean unconditional){
    notifyDestroyed ();
  }

  public void commandAction(Command c, Displayable d){
    listItemIndex = list.getSelectedIndex();
    if(c == ok){
      display.setCurrent(canvasDraw);
    }else if(c == exit){
      destroyApp(true);
    }
  }

  class CanvasDraw extends Canvas implements CommandListener{
    Command back;

    public CanvasDraw(){
      back = new Command("Back", Command.BACK, 1);
      addCommand(back);
      setCommandListener(this);
    }

    public void paint(Graphics g){
      g.setColor(00255);
      g.fillRect(0,0, getWidth (), getHeight ());
      g.setColor(25500);

      if(listItemIndex == 0){
        g.drawString("Draw line", getWidth () 25, Graphics.HCENTER | 
                Graphics.TOP);

        g.drawLine(getWidth()/4100* getWidth()/4100);
      }

      if(listItemIndex == 1){
        g.drawString("Draw Rectangle", getWidth()/25, Graphics.HCENTER | 
        Graphics.TOP
);

        g.fillRect(getWidth () 4100, getWidth() 240);
      }

      if(listItemIndex==2){
        g.drawString("Draw Rounded Rectangle", getWidth () 25
        Graphics.HCENTER | Graphics.TOP
);

        g.fillRoundRect(getWidth () 4100, getWidth () 2403040);
      }

      if(listItemIndex == 3){
        g.drawString("Draw Arc", getWidth () 25, Graphics.HCENTER | 
        Graphics.TOP
);        

        g.fillArc(getWidth () 41009090180270);
      }

      if(listItemIndex == 4){
        g.drawString("Draw Traingle", getWidth () 25, Graphics.HCENTER | 
        Graphics.TOP
);

        g.fillTriangle(getWidth() 41009090100180);
      }
    }

    public void commandAction (Command c, Displayable d){
      if(c == back){
        display.setCurrent(list);
      }
    }
  }
}

Download Source Code

                         

» View all related tutorials
Related Tags: c j2me diff types type if example to rectangle draw exam raw angle e il pe in different m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.