In J2ME programming language canvas class is used to paint and draw the
diagrams. Using the same canvas class we are going to draw a box around the text
in our show text MIDlet Example. We have created a class called CanvasBoxText
that extends the predefined canvas class to draw the box. In the code we have
used different methods to get the values, these are..
int width = getWidth();
int height = getHeight();
g.setColor(255, 0, 0);
g.fillRect(0, 0, width, height);
g.setColor(0, 0, 255);
String sandeep = "SANDEEP";
int w = font.stringWidth(sandeep);
int h = font.getHeight();
int x = (width - w) / 2;
int y = height / 2;
g.drawString(sandeep, x, y, Graphics.TOP | Graphics.LEFT);
g.drawRect(x, y, w, h);
After running the example you will get the output as given below..
In the output you can easily find out the text "SANDEEP" that is
been displayed in the box. As I mentioned earlier, we need a canvas class to
draw such kind of graphics in the J2ME application.