1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu LWUIT 1.1 for Java ME Developers- P7 pdf

50 321 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 1,44 MB

Nội dung

Chapter 11 [ 287 ] You can modify the MIDlet such that the transition occurs between a form and a dialog. The resulting transition would look like this: Summary This chapter has shown us quite a few things. We have seen how to animate objects by implementing the Animation interface. We have also seen how to use the transitions that come with the LWUIT library—for forms, dialogs, and menus as well as for widgets like labels. We have also authored our own transition. The intricacies of designing transitions that work with dialogs have been dealt with in some detail. There are some more issues that may need to be addressed while building custom transitions. The source code of LWUIT transitions provides extensive insight into these factors, especially from the perspective of handling graphics related issues and is an invaluable resource for a developer. This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Painters All LWUIT components have a multi-layered structure. The rst layer erases a visually obsolete widget, and the subsequent layers then paint the background followed by the constituent parts of the new version. As a matter of fact, the background too can be made up of several layers, and that is not all. After a form has been fully rendered, we can place a layer above it that can be drawn upon regardless of any changes or animations that may be taking place in the form below. Such a layer—known as a GlassPane—is usually transparent or translucent so that the form under it remains visible. The classes that work as a background painter or a glass pane must implement the Painter interface. In case more than one background painter is used, they can be formed into a chain through the PainterChain class so that the background can be rendered layer-by-layer. Similarly, a glass pane also can have many layers. In this chapter, we shall familiarize ourselves with the Painter interface and the PainterChain class. We shall also learn, with the help of examples, how background painters and glass panes can be used. The Painter interface Painter denes the fundamental interface for all objects that are meant to draw backgrounds or to render on a glass pane. This interface declares only one method—public void paint(Graphics g, Rectangle rect)—for drawing inside the bounding rectangle (specied by rect) of a component. The library provides a class that implements Painter and is used as a default background painter for widgets and containers. This is the BackgroundPainter class that has (you guessed it) just the one method paint, which either paints the background image if one has been assigned or lls in the bounding rectangle of the component with the color set in its style. This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Painters [ 290 ] When we want to paint a background ourselves, we can write our own class that implements Painter, and set it as the background painter for the relevant component. The DemoPainter MIDlet, discussed in the next section, shows how this is done. The DemoPainter application This application creates a combo box and uses a theme to set the style for the various elements that are displayed. When the application is compiled without setting a custom background painter, the combo box looks as shown in the following screenshot: The MIDlet code has the following statement commented out in the MIDlet. When uncommented, this statement sets an instance of ComboBgPainter as the background painter for the combo box. combobox.getStyle().setBgPainter(new ComboBgPainter(0x4b338c)); This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 12 [ 291 ] The recompiled application produces the following display showing the new background color: The class responsible for drawing the background is ComboBgPainter, which implements Painter. The constructor for this class takes the color to be used for background painting as its only parameter. The paint method determines the coordinates of the top-left corner of the rectangle to be painted and its dimensions. The rectangle is then lled using the color that was set through the constructor. class ComboBgPainter implements Painter { private int bgcolor; public ComboBgPainter(int bgcolor) { this.bgcolor = bgcolor; } public void paint(Graphics g, Rectangle rect) { g.setColor(bgcolor); int x = rect.getX(); int y = rect.getY(); int wd = rect.getSize().getWidth(); int ht = rect.getSize().getHeight(); g.fillRect(x, y, wd, ht); } } This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Painters [ 292 ] Drawing a multi-layered background In actual practice, there is hardly any point in using a custom painter just to paint a background color, because the setBgColor method of Style will usually do the job. Themes too can be used for setting background colors. However, painters are very useful when intricate background patterns need to be drawn, and especially if multiple layers are involved. PainterChain, described in the next section, is a class designed for handling such requirements. The PainterChain class It is possible to use more than one painter to render different layers of a background. Such a set of painters can be chained together through the PainterChain class. The only constructor of this class has the form public PainterChain(Painter[] chain) where the parameter chain is an array of painters. The contents of chain will be called sequentially during the painting of a background, starting from the element at index 0 to the last one. There are two methods of the PainterChain class that provide support for adding painters to the array underlying the chain. A new painter can be added either to the top (the prependPainter method) or at the end (the addPainter method) of the array. The array itself can be accessed through the getChain method. PainterChain implements Painter so that the setBgPainter method can be used to set a PainterChain as well as a lone painter, which means the paint method also is present here. The function of paint in PainterChain is to call the paint methods of the painter array elements one by one starting at index 0. The DemoPainterChain application that comes up next shows how a chain of painters can be used to draw the multiple layers of a background. The DemoPainterChain application The DemoPainterChain example uses alphaList (the list for DemoList MIDlet in Chapter 5) to show a painter chain in action. After organizing the form and the list, we set up a painter array to hold the three painters that we shall deploy. Painter[] bgPainters = new Painter[3]; Once we have the array, we create three painters and load them into the array. The rst (lowest) painter, which will ll the bounding rectangle for the list with a designated color, goes in at index 0. The next (middle) layer, at index 1, will draw an image at the center of the list. Finally, the topmost layer for writing a text a little below the center line of the list is inserted at index 2. This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 12 [ 293 ] bgPainters[0] = new Eraser(0x334026); try { bgPainters[1] = new ImagePainter(Image.createImage( "/a.png")); } catch(java.io.IOException ioe) { } bgPainters[2] = new TextPainter("This is third layer"); Now we are ready to instantiate a PainterChain object, and install it as a background painter for the list. PainterChain bgChain = new PainterChain(bgPainters); alphaList.getStyle().setBgPainter(bgChain); The list itself will be drawn on top of these three layers, and the background layers will be visible only because the list is translucent as determined by the transparency value 100, set by the AlphaListRenderer instance used to render alphaList. The list now looks as shown in the following screenshot: This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Painters [ 294 ] A close inspection of the screenshot that we have just seen will show that the layers have indeed been drawn in the same sequence as we had intended. The three painters are very similar in structure to the ComboBgPainter class we came across in the previous example. The Eraser class here is virtually identical to ComboBgPainter. The other two classes work in the same way, except for the fact that TextPainter draws a line of text, while ImagePainter draws an image. class TextPainter implements Painter { private String text; TextPainter(String text) { //set the text to be written this.text = text; } public void paint(Graphics g, Rectangle rect) { //get the dimension //of background int wd = rect.getSize().getWidth(); int ht = rect.getSize().getHeight(); //create and set font for text Font textFont = Font.createSystemFont( Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE); g.setFont(textFont); //set text color g.setColor(0x0000aa); //position text slightly below centerline int textX = wd/2 - textFont.stringWidth(text)/2; int textY = ht/2 - textFont.getHeight()/2 + 3; //write text g.drawString(text, textX, textY); } } class ImagePainter implements Painter { private Image bImage; ImagePainter(Image bImage) { //set the image to be drawn this.bImage = bImage; } public void paint(Graphics g, Rectangle rect) { //get the dimensions //of background This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 12 [ 295 ] int wd = rect.getSize().getWidth(); int ht = rect.getSize().getHeight(); //position image at center int imageX = wd/2 - bImage.getWidth()/2; int imageY = ht/2 - bImage.getHeight()/2; //draw image g.drawImage(bImage, imageX, imageY); } } When an image is used on the background of a form, we have seen that it is scaled to occupy the entire form real estate. But if the same image is used as an icon for a label, then it is drawn in its actual size. This task of scaling the image for backgrounds is taken care of by BackgroundPainter, which is used as the default bgPainter. The scaleImage attribute of Style determines whether the background image of a component should be scaled (scaleImage == true) or tiled (scaleImage == false), and its default value is true. When required, scaleImage can be set to any value by calling the setScaleImage method of Style. Before drawing the background image, the default background painter calls the isScaleImage method of Style. If the returned value is true and the dimensions of the background image are not the same as those of the background (specied by the parameter rect of paint method), then the image is scaled to the same size as that of the rectangle to be drawn into. This image is then set as the background image so that the scaling does not have to be done over and over again. In our case, we need not check the scaleImage attribute, as we have already decided that scaling is required. So all we need to do is compare the dimensions of the image with those of the background we are drawing on and scale it if required. Then we save the scaled version so that it will not be necessary to scale it the next time this method is called. In order to see the effect of scaling, we replace the paint method of the ImagePainter class with the following version. The highlighted code does the scaling. public void paint(Graphics g, Rectangle rect) { //get the dimensions and position //of background int imageX = rect.getX(); int imageY = rect.getY(); int wd = rect.getSize().getWidth(); int ht = rect.getSize().getHeight(); //check if image dimension different //from component background dimension if (bImage.getWidth() != wd || bImage.getHeight() != ht) This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Painters [ 296 ] { //scale image and save bImage = bImage.scaled(wd, ht); } //draw image g.drawImage(bImage, imageX, imageY); } The new paint method creates the following list. Note the change in background color, which is now the same as that of the image, as it has been scaled up to cover the original background. Using a glass pane A glass pane is indeed like a fully transparent and distortion free glass sheet placed over a form. We can draw whatever we want on the pane, and only the part of the form under that pattern will be obscured. The rest of the form will be visible. Also, the pattern drawn on the glass pane will not be affected by any transition, animation, or change of any kind that may take place on the form below. In the world of LWUIT, a glass pane is also a painter. However, unlike the painters we have used so far, a glass pane can only be used with a form. Let us see, with the help of the DemoGlassPane example, how to install a glass pane. This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... instance.fileName = newName; } public static void setRecordName(String newName) { instance.recordName = newName; } [ 317 ] This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge E Conway Dr NW, , Atlanta, ,to remove this watermark 4310 on www.verypdf.com 30327 Effects and Logging—Useful Utilities public static String getFileName() {... Split-Merge E Conway Dr NW, , Atlanta, ,to remove this watermark 4310 on www.verypdf.com 30327 Chapter 13 The Log.p method not only records the messages, but it also prints them on the console The messages are shown below We see that all four messages have been printed as the logging level was the default one Before logging a message, Log.p appends the name of the thread, which generated the message... licensed for the sole use by William Anderson on 26th August 2009 Please purchase PDF Split-Merge E Conway Dr NW, , Atlanta, ,to remove this watermark 4310 on www.verypdf.com 30327 Effects and Logging—Useful Utilities The Log.showLog method uses the Log.getLogContent method to retrieve logged information and sets it as the text for a text area This text area is then added to a form, and the form is... Log.showLog methods use the name "log" for the record store to be used for logging into Both these methods call the static method RecordStore openRecordStore with a true second parameter to make sure that a new record store is created if one with specified name cannot be found That is why we can delete the entire record without any fear of a RecordStoreException being thrown Note that for DemoLogger, we assume... shows its use Logging has been around for a long time in the world of Java We have had several logging software products like Ceki Gülcü's log4j (now an Apache Software Foundation project) We also have Sun's own logging API for JDK 1.4 as well as Lumberjack for JDKs 1.2 and 1.3 Through the Log class, LWUIT provides an easy to use and pluggable logging framework for Java ME applications Here we are going... www.verypdf.com 30327 Effects and Logging—Useful Utilities Logging with LWUIT The Log class provides a framework for recording information at runtime By default, the specified information is logged using the Record Management System (RMS) If the device supports the File Connection API defined by JSR 75, then the file system is used for logging The recorded messages can be retrieved and displayed to provide insight... of Log in order to meet the objectives mentioned above The others will be copies of methods with the same name in the superclass Although these methods will be the same as in the superclass, we need the duplicates because we are dealing with static methods, and we need to hide the originals so that the proper references are used In order to understand this, consider the following method: public static... and that is why the method in MyLog has to be called We shall also duplicate the private methods of Log, as the original methods cannot be accessed from MyLog Here we could have used different names for the methods However, having the same method signatures can be a help if one tries to compare the original Log class with MyLog [ 314 ] This material is copyright and is licensed for the sole use by... Please purchase PDF Split-Merge E Conway Dr NW, , Atlanta, ,to remove this watermark 4310 on www.verypdf.com 30327 Chapter 13 In order to meet our objectives, we need to understand the inner workings of the methods of the Log class that logs messages and retrieves them as required Firstly, let's take a look at the methods that do the logging There are two static methods that we can use for logging—public... example However, before that, here is an introduction to the class itself The Log class The functionalities of this class are exposed through a set of static methods These methods can be classified into three groups: • Methods to access logging level: A getter and a setter • Methods for writing into the log file: There are two such methods—one for logging at the default level and the other for logging at . API for JDK 1. 4 as well as Lumberjack for JDKs 1. 2 and 1. 3. Through the Log class, LWUIT provides an easy to use and pluggable logging framework for Java. LWUIT The Log class provides a framework for recording information at runtime. By default, the specied information is logged using the Record Management

Ngày đăng: 26/01/2014, 10:20