Browse DevX
Sign up for e-mail newsletters from DevX


Get Down with the MIDP Low-level User Interface API : Page 2

MIDP provides a low-level API for fine-grained control over the application user interface. However, applications wanting to make use of these features also inherit a lot of responsibility and risk. Find out the benefits and pitfalls of using the MIDP low-level APIs.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using Font Metrics
Font metrics, along with other font information, is available from the Font class. To get the current font in use, make a call to Graphics.getFont(). The height of the font can be found with a call to Font.getHeight(). The following code improves on the example and shows how font metrics can be used to split the lines into two parts.

public void paint(Graphics graphics) { String text1 = "This is a very boring MIDlet "; String text2 = "that is nearly putting me to sleep!"; graphics.setColor(255,255,255); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setColor(0,0,0); Font font = graphics.getFont(); int fontHeight = font.getHeight(); graphics.drawString(text1, 0, 0, Graphics.TOP|Graphics.LEFT); graphics.drawString(text2, 0, fontHeight, Graphics.TOP|Graphics.LEFT); }

Figure 5. That's a Wrap? This method splits the text into two parts and calls drawString() twice to get a line-wrapping effect.
Figure 5. That's a Wrap? This method splits the text into two parts and calls drawString() twice to get a line-wrapping effect.

At this point, my simple example is becoming more and more complex, and all I have done is print a line of text to the screen. Although Canvas provides a lot of fine-grain control and flexibility in creating user interfaces, you can see how many more details need attending.

These details become even more important to manage when writing applications for multiple devices. Screen size and color support is one of the most likely things to change between devices. Great care must be taken to ensure that a Canvas can adapt to any environment. In other words, guessing about font height, line wrapping, image size, and so forth will probably land you in trouble.

Simple Line Wrapping.
With portability in mind, I'm going to take another look at the example thus far and see what I can do to enhance portability and allow any string to be drawn correctly.

Line-wrapping algorithms can become rather complex in a hurry. To keep this example simple, lines will be wrapped at the character level, rather than the word level. Also, vertical scrolling will not be considered, meaning that this implementation expects the full text to fit on one screen.

This approach examines each character in the string one at a time and determines where to draw the character in relation to all the other characters on the screen. Each character is drawn one a time along the Y access, incrementing the X access by the width of the character each time. When the right edge of the screen is reached, and the next character does not fit in the space remaining, the value of Y is incremented by the font height and X is reset to 0. The result is a character-level line wrapping effect.

The Font class contains a method for measuring the size of a specific character for a given font. When dealing with true-type fonts it is important to understand that characters differ in width. For example "W" is wider than "i." This requires each character to be measured individually using Font.charWidth(). The example enhanced with line wrapping is shown in Listing 1.

My simple, even boring, example has become rather interesting. It illustrates how much fine-grain control is provided by the low-level APIs. However, it also highlights the complexity that ensues in order to do seemingly simple tasks such as line-wrap text. This realization should cause developers to think through the benefits of using the low-level APIs to make sure that the benefits are worth the cost to implement and maintain code of this complexity.

So far, I have demonstrated the basics for drawing text on the screen. However, the Graphics class also defines methods for drawing shapes and images as well. The principles for using these other Graphics methods are essentially the same as what has been demonstrated thus far. So, at this point I want to shift gears and discuss some of the event-handling capabilities of the Canvas class.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date