1 /*
2 * Nokia API for MicroEmulator
3 * Copyright (C) 2003 Markus Heberling <markus@heberling.net>
4 *
5 * It is licensed under the following two licenses as alternatives:
6 * 1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7 * 2. Apache License (the "AL") Version 2.0
8 *
9 * You may not use this file except in compliance with at least one of
10 * the above two licenses.
11 *
12 * You may obtain a copy of the LGPL at
13 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14 *
15 * You may obtain a copy of the AL at
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the LGPL or the AL for the specific language governing permissions and
22 * limitations.
23 */
24
25 package com.nokia.mid.ui;
26
27 import javax.microedition.lcdui.Graphics;
28 import javax.microedition.lcdui.Image;
29
30 /** This class is a placeholder for utility methods. It contains methods for converting standard lcdui classes to Nokia UI classes and vice versa, and a method for creating images that are empty with pixels either transparent or colored, and creating mutable images from encoded image byte arrays. */
31 public class DirectUtils
32 {
33
34 /** Converts standard javax.microedition.lcdui.Graphics to DirectGraphics. The returned object refers to the same graphics context. This means that calling draw operations or changing the state, for example, drawing color etc., via the original Graphics reference affect the DirectGraphics object, and vice versa.
35 *
36 * Note that even though the graphics context that the DirectGraphics and Graphics refer to is the same, the object reference returned from this method may or may not be equal compared to the Graphics reference passed to this method. This means that purely casting Graphics object (g) passed in paint method of lcdui Canvas to DirectGraphics may not work ok. The safest way is to always do the conversion with this method.
37 * @param g Graphics object for which DirectGraphics should be returned
38 * @return the DirectGraphics object based on Graphics
39 */
40 public static DirectGraphics getDirectGraphics(Graphics g)
41 {
42 return new DirectGraphicsImp(g);
43 }
44
45 /** Creates a mutable image that is decoded from the data stored in the specified byte array at the specified offset and length. The data must be in a self-identifying image file format supported by the implementation, e.g., PNG.
46 *
47 * Note that the semantics of this method are exactly the same as Image.createImage(byte[],int,int) except that the returned image is mutable.
48 * @param imageData the array of image data in a supported image format
49 * @param imageOffset the offset of the start of the data in the array
50 * @param imageLength the length of the data in the array
51 * @return the created mutable image
52 */
53 public static Image createImage(byte imageData[], int imageOffset, int imageLength)
54 {
55 Image source = Image.createImage(imageData, imageOffset, imageLength);
56 Image target = Image.createImage(source.getWidth(), source.getHeight());
57 target.getGraphics().drawImage(source,0,0,0);
58 return target;
59 }
60
61 /** The method will return a newly created mutable Image with the specified dimension and all the pixels of the image defined by the specified ARGB color. The color can contain alpha channel transparency information.
62 * @param width the width of the new image, in pixels
63 * @param height the height of the new image, in pixels
64 * @param argb the initial color for image<br>Note: This is argb color, but alpha channel is currently
65 * not supported by this emulation.
66 * @return the created image
67 */
68 public static Image createImage(int width, int height, int argb)
69 {
70 Image img = Image.createImage(width, height);
71 Graphics g = img.getGraphics();
72 g.setColor(argb);
73 g.fillRect(0,0, width,height);
74 g.setColor(0);
75 return img;
76 }
77
78 }