Sample program to draw a rectangle in J2ME

By: Sam Chen Emailed: 1767 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

You can draw two kinds of rectangles: regular and rounded. Rectangles, like all geometric drawing, can be drawn in different colors by specifying the color of the graphics pen. 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Command;

/**
Draws rectangles on a Canvas using the drawing methods
in the javax.microedition.lcdui.Graphics class.
@see javax.microedition.lcdui.Graphics
 
*/
public class RectangleDemo extends Canvas
implements CommandListener
{
// Constant representing the color white.
private static final int WHITE =
0xFF << 16 | 0xFF << 8 | 0xFF;
private Command back = new Command("Back",
Command.BACK,
1);
private Display display =
Display.getDisplay(GraphicsDemo.getInstance());

/**
No-arg constructor. Calls the Canvas no-arg
constructor.
*/
public RectangleDemo()
{
super();
addCommand(back);
setCommandListener(this);
display.setCurrent(this);
}
/**
Paints the clip rectangle white, effectively erasing
whatever was displayed on the Canvas previously.
*/
protected void paintClipRect(Graphics g)
{
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipH = g.getClipHeight();
int clipW = g.getClipWidth();
int color = g.getColor();
g.setColor(WHITE);
g.fillRect(clipX, clipY, clipW, clipH);
g.setColor(color);
}

/**
Paints the look of this Canvas subclass.
*/
public void paint(Graphics g)
{
paintClipRect(g);
int width = getWidth();
int height = getHeight();
int x0 = 5;
int y0 = 5;
int barW = 10;
int initHeight = height - 10;
int deltaH = 10;
g.drawRect(x0, y0, barW, initHeight);
g.fillRect(x0 + barW, y0 + deltaH, barW,
initHeight - deltaH + 1);
g.drawRect(x0 + barW * 2, y0 + deltaH * 2,
barW, initHeight - deltaH * 2);
g.setColor(255, 00, 00);
g.fillRect(x0 + barW * 3, y0 + deltaH * 3,
barW, initHeight - deltaH * 3 + 1);
g.setColor(0, 0, 0);
g.drawRect(x0 + barW * 4, y0 + deltaH * 4,
barW, initHeight - deltaH * 4);
g.fillRect(x0 + barW * 5, y0 + deltaH * 5,
barW, initHeight - deltaH * 5 + 1);
g.drawRect(x0 + barW * 6, y0 + deltaH * 6,
barW, initHeight - deltaH * 6);
g.fillRect(x0 + barW * 7, y0 + deltaH * 7,
barW, initHeight - deltaH * 7 + 1);
}
public void commandAction(Command c, Displayable d)
{
if (c == back)
{
GraphicsDemo.getInstance().display();
}
}
}


J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Sam Chen
Extract filename from full path using perl
Encrypting Passwords in Tomcat using Servlets
malloc, calloc - Storage Management - in C
Character Pointers and Functions in C
Declarations in C
strcpy() and strncpy() sample program in C++
Sample program to draw a rectangle in J2ME
Design Patterns for Properties in a Java Bean
PushbackInputStream example program in Java
FileInputStream - sample program in Java

More Tutorials in J2ME
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Using List to create a Menu and Menu items in J2ME
Using alerts and tickers in J2ME
J2ME Canvas sample to show games programming in J2ME
Timer and TimerTask example in J2ME
List of GPRS Access points for all countries

More Latest News
Most Viewed Articles (in J2ME )
Sample program to draw a rectangle in J2ME
What is J2ME?
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
Y.S. Sun Green Building Research Center
'LINK.EXE' is not recognized as an internal or ext
Sample Java program shows how to Read a file over a network using J2ME midlet
Sample J2ME code that shows various functionality of RMS.
A sample J2ME midlet that shows an example of using a Timer and a TimerTask.
GUI components and menu based J2ME Applications.
Your first J2ME program and a midlet lifecycle explained.
java.lang.SecurityException: MIDP lifecycle does not support system exit.
How to get the CLDC and MIDP version from a J2ME program
DateField sample program in J2ME
StringItem sample program in J2ME
Most Emailed Articles (in J2ME)
How to load J2ME applications to the IDEN handsets
GUI components and menu based J2ME Applications.
Ticker sample program in J2ME
Using List to create a Menu and Menu items in J2ME
What is J2ME?
'LINK.EXE' is not recognized as an internal or ext
DateField sample program in J2ME
Sample program to draw a rectangle in J2ME
Types of configurations in J2ME
Types of Profiles in J2ME
A sample J2ME Auto-resume program.
getPosition() the right strategy in getting GPS
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
What is J2ME?