Home J2me J2ME Draw Triangle

Related Tutorials


 
 

Share on Google+Share on Google+

J2ME Draw Triangle

Advertisement
As you already aware of the canvas class and it's use in J2ME application, we are using canvas class to draw the triangle on the screen.

J2ME Draw Triangle

     

As you already aware of the canvas class and it's use in J2ME application, we are using canvas class to draw the triangle on the screen. In this example you will learn how to draw a triangle and filling it with particular color.

 

 

 

 

 

 

Output of the draw triangle application will look like as follow...

Source code to draw a triangle in J2ME

file name DrawTraingle.java 

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

public class DrawTraingle extends MIDlet implements CommandListener{
  private Display display;
  private List list;
  private Command ok, exit;
  private CanvasDraw canvasDraw;
  
  public DrawTraingle(){
  canvasDraw = new CanvasDraw();
  display = Display.getDisplay(this);
  list = new List("DrawTraingle", List.IMPLICIT);
  ok = new Command("Draw", Command.OK, 2);
  exit = new Command("Exit", Command.EXIT, 2);
  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){
  int 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);

  g.drawString("Draw Traingle", getWidth () 25, Graphics.HCENTER | Graphics.TOP);
  g.fillTriangle(getWidth() 41009018018090);
  }

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

 

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 19, 2008

Related Tutorials

Discuss: J2ME Draw Triangle  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com