paint() sample program to draw a line in J2ME

By: Reema sen Emailed: 1743 times Printed: 2434 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

The paint(Graphics g) method is the highlight of this example. Because Canvas defines this method to be abstract, subclasses must provide a concrete definition. Your program must perform all its drawing in its paint(Graphics g) method on the Graphics object passed to it. You invoke the Graphics class's dedicated drawing routines on this instance that is passed to your canvas.

To draw a line, you must specify the (x, y) coordinates of its start and end points. The (x, y) coordinates are defined relative to the point (0, 0), which, at the time the graphics context is created, represents the pixel at the top-left corner of the display. The x coordinate specifies the horizontal distance to the right from column 0 (the left edge of the display), and the y coordinate specifies the vertical distance down from row 0, which is the top of the display.

Lines are one pixel thick. To create thicker lines, you must draw adjacent lines. Additionally, the middle line appears dashed. You can set the stroke style for any drawing using the setStrokeStyle() method as demonstrated. The exact rendering of lines that use the Graphics.DOTTED stroke style is implementation-dependent.

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 a series of lines to demonstrate the different
types and styles of lines that can be drawn with the
Graphics class.
@see javax.microedition.lcdui.Graphics
*/
public class LineDemo extends Canvas
implements CommandListener
{
// A constant that represents the color white.
private static final int WHITE =
0xFF << 16 | 0xFF << 8 | 0xFF;
private Command back = new Command("Back",
Command.BACK,
1);
private GraphicsDemo gDemo = GraphicsDemo.getInstance();
private Display display = Display.getDisplay(gDemo);
/**
No-arg constructor.
*/
public LineDemo()
{
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();
g.drawLine(20, 10, width - 20, height - 34);
g.drawLine(20, 11, width - 20, height - 33);
g.drawLine(20, 12, width - 20, height - 32);
113
g.drawLine(20, 13, width - 20, height - 31);
g.drawLine(20, 14, width - 20, height - 30);
g.setStrokeStyle(Graphics.DOTTED);
g.drawLine(20, 24, width - 20, height - 20);
g.drawLine(20, 25, width - 20, height - 19);
g.drawLine(20, 26, width - 20, height - 18);
g.setStrokeStyle(Graphics.SOLID);
g.drawLine(20, 36, width - 20, height - 8);
}
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(1)


1. View Comment

"paint() sample program to draw a line in J2ME"

Hello Dear,

Thank you very much for sharing with us this example.
where I can see the GraphicsDemo class that is included in this example. Or as I can include it in this instance?

Thank in advance for your attention.

Best regards,


View Tutorial          By: Carlos Alberto Urrea at 2015-02-06 14:00:14

Your name (required):


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


Your sites URL (optional):


Your comments:



More Tutorials by Reema sen
this keyword sample in Java
Using the DriverManager Class vs Using a DataSource Object for a connection
Steps to get a Free SSL certificate for your Tomcat
fgets(), fputs() - Line Input and Output - sample program in C
Address Arithmetic and pointers in C
Constants and escape sequences in C
Implementing Pure Virtual Functions in C++
strcat() and strncat() sample program in C++
paint() sample program to draw a line in J2ME
Creating and Handling JAR files
SequenceInputStream example program in Java
FilenameFilter - sample program in Java
Transient vs Volatile modifiers in Java
while (1) Loops in C++
Use of 'finally' 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 )
What is J2ME?
Ticker sample program in J2ME
'LINK.EXE' is not recognized as an internal or ext
List of GPRS Access points for all countries
Using List to create a Menu and Menu items in J2ME
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Adding your own Application icon for your J2ME application (jar file)
RMS Basics in J2ME
Types of Profiles in J2ME
What is J2ME?
Download a file over a network in J2ME midlet
Sample Java program shows how to Read a file over a network using J2ME midlet
GUI components and menu based J2ME Applications.
How to get the CLDC and MIDP version from a J2ME program
Gauge sample program in J2ME
Most Emailed Articles (in J2ME)
What is J2ME?
Download a file over a network in J2ME midlet
Sample Java program shows how to Read a file over a network using J2ME midlet
Types of Profiles in J2ME
DateField sample program in J2ME
Ticker sample program in J2ME
Sample program to draw a arc in J2ME
The MIDP Networking Model in J2ME
Using alerts and tickers in J2ME
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Adding your own Application icon for your J2ME application (jar file)
Y.S. Sun Green Building Research Center
'LINK.EXE' is not recognized as an internal or ext
A sample J2ME midlet that shows an example of using a Timer and a TimerTask.
How to get the CLDC and MIDP version from a J2ME program