Source code: javax/microedition/lcdui/ImageItem.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package javax.microedition.lcdui;
21
22 import com.barteo.emulator.device.DeviceFactory;
23
24
25 public class ImageItem extends Item
26 {
27
28 public static final int LAYOUT_DEFAULT = 0;
29 public static final int LAYOUT_LEFT = 1;
30 public static final int LAYOUT_RIGHT = 2;
31 public static final int LAYOUT_CENTER = 3;
32 public static final int LAYOUT_NEWLINE_BEFORE = 0x100;
33 public static final int LAYOUT_NEWLINE_AFTER = 0x200;
34
35 Image img;
36 int layout;
37 String altText;
38
39
40 public ImageItem(String label, Image img, int layout, String altText)
41 {
42 super(label);
43
44 if (img != null && img.isMutable()) {
45 new IllegalArgumentException();
46 }
47
48 this.img = img;
49 this.layout = layout;
50 this.altText = altText;
51 }
52
53
54 public String getAltText()
55 {
56 return altText;
57 }
58
59
60 public Image getImage()
61 {
62 return img;
63 }
64
65
66 public int getLayout()
67 {
68 return layout;
69 }
70
71
72 public void setAltText(String text)
73 {
74 altText = text;
75 }
76
77
78 public void setImage(Image img)
79 {
80 this.img = img;
81 }
82
83
84 public void setLabel(String label)
85 {
86 super.setLabel(label);
87 }
88
89
90 public void setLayout(int layout)
91 {
92 this.layout = layout;
93 }
94
95
96 int getHeight()
97 {
98 if (img == null) {
99 return super.getHeight();
100 } else {
101 return super.getHeight() + img.getHeight();
102 }
103 }
104
105
106 int paint(Graphics g)
107 {
108 super.paintContent(g);
109
110 if (img != null) {
111 g.translate(0, super.getHeight());
112 if (layout == LAYOUT_DEFAULT || layout == LAYOUT_LEFT) {
113 g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
114 } else if (layout == LAYOUT_RIGHT) {
115 g.drawImage(img, DeviceFactory.getDevice().getDeviceDisplay().getWidth(), 0,
116 Graphics.RIGHT | Graphics.TOP);
117 } else if (layout == LAYOUT_CENTER) {
118 g.drawImage(img, DeviceFactory.getDevice().getDeviceDisplay().getWidth() / 2, 0,
119 Graphics.HCENTER | Graphics.TOP);
120 } else {
121 g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
122 }
123 g.translate(0, -super.getHeight());
124 }
125
126 return getHeight();
127 }
128
129
130 int traverse(int gameKeyCode, int top, int bottom, boolean action)
131 {
132 Font f = Font.getDefaultFont();
133
134 if (gameKeyCode == Canvas.UP) {
135 if (top > 0) {
136 if ((top % f.getHeight()) == 0) {
137 return -f.getHeight();
138 } else {
139 return -(top % f.getHeight());
140 }
141 } else {
142 return Item.OUTOFITEM;
143 }
144 }
145 if (gameKeyCode == Canvas.DOWN) {
146 if (bottom < getHeight()) {
147 if (getHeight() - bottom < f.getHeight()) {
148 return getHeight() - bottom;
149 } else {
150 return f.getHeight();
151 }
152 } else {
153 return Item.OUTOFITEM;
154 }
155 }
156
157 return 0;
158 }
159
160 }