Home J2me Draw Line in J2me

Related Tutorials


 
 

Share on Google+Share on Google+

Draw Line in J2me

Advertisement
In this example we are going to show you how to draw a line using J2ME. Please go through the below given J2ME syntax that includes all the package, methods and class to draw a line.

Draw Line in J2me

     

In this example we are going to show you how to draw a line using J2ME. Please go through the below given J2ME syntax that includes all the package, methods and class to draw a line. 

Basically in J2ME, Canvas class is used to draw circle or line.. that is the only reason of using this canvas class in our J2ME program. We have also created a MIDlet called DrawLine in our application as we can't draw a line without creating it. 

g.drawLine(x, y, x+w, y);
g.drawLine(x+w, y, x+w, y+h); 
g.drawLine(x+w, y+h, x, y+h); 
g.drawLine(x, y+h, x, y);

or the another variation is: 

g.drawLine(x1, y1, x2, y2);

The above variation show the distance from x-axis and y-axis. 

The Application look like as follows:

 

DrawLine.java

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

public class DrawLine extends MIDlet{
  private Display display;

  public void startApp(){
  display = Display.getDisplay(this);
  display.setCurrent (new DrawingCanvas())
  }

  public void pauseApp(){}

  public void destroyApp (boolean forced){}
}

class DrawingCanvas extends Canvas{
  public void paint (Graphics g){
  g.setColor (25500);
  g.fillRect (00, getWidth(), getHeight());
  g.setColor (00255);
  g.fillRect (203020080);
  g.setColor (1280255);
  g.drawLine (00100200);
  }
}

Download Source Code

Advertisement

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: October 24, 2008

Related Tutorials

Discuss: Draw Line in J2me  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Ritanshu Goel
September 27, 2012
J2ME Line Move

Hello, I want to move this line and want to create an arrow on this line to show direction where it is moving. so Plz help me to do this. Thank you.
DMCA.com