paint() sample program to draw a line in J2ME
By: Reema sen
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();
}
}
}
Archived Comments
1. "paint() sample program to draw a line in J2ME"
Hello Dear,
View Tutorial By: Carlos Alberto Urrea at 2015-02-06 14:00:14
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
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
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
Client Server in J2ME (Socket Programming sample)
Datagrams in J2ME (UDP Programming sample)
POST UTF-8 encoded data to the server in J2ME
Using alerts and tickers in J2ME
Using List to create a Menu and Menu items in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Timer and TimerTask example in J2ME