KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > microedition > lcdui > Screen


1
2 /*
3  * MicroEmulator
4  * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20  
21 package javax.microedition.lcdui;
22
23 import com.barteo.emulator.device.DeviceFactory;
24
25
26 public abstract class Screen extends Displayable
27 {
28
29     StringComponent title;
30   Ticker ticker;
31     int viewPortY;
32     int viewPortHeight;
33
34
35     Screen(String title)
36     {
37         this.title = new StringComponent(title);
38         viewPortY = 0;
39         viewPortHeight = DeviceFactory.getDevice().getDeviceDisplay().getHeight() - this.title.getHeight() - 1;
40     }
41
42
43   public Ticker getTicker()
44   {
45     return ticker;
46   }
47
48
49   public String getTitle()
50   {
51     return title.getText();
52   }
53
54
55   public void setTitle(String s)
56   {
57     title.setText(s);
58   }
59
60
61   public void setTicker(Ticker ticker)
62   {
63     if (this.ticker != null) {
64       viewPortHeight += this.ticker.getHeight();
65     }
66     this.ticker = ticker;
67     if (this.ticker != null) {
68       viewPortHeight -= this.ticker.getHeight();
69     }
70     repaint();
71   }
72
73
74     abstract int traverse(int gameKeyCode, int top, int bottom);
75
76
77   void keyPressed(int keyCode)
78     {
79     int gameKeyCode = Display.getGameAction(keyCode);
80
81     if (gameKeyCode == Canvas.UP || gameKeyCode == Canvas.DOWN) {
82             viewPortY += traverse(gameKeyCode, viewPortY, viewPortY + viewPortHeight);
83             repaint();
84     }
85     }
86
87
88     void hideNotify()
89     {
90         super.hideNotify();
91     }
92
93
94     void keyRepeated(int keyCode)
95     {
96     keyPressed(keyCode);
97     }
98
99
100     final void paint(Graphics g)
101     {
102     int contentHeight = 0;
103         int translatedY;
104
105         if (viewPortY == 0) {
106             currentDisplay.setScrollUp(false);
107         } else {
108             currentDisplay.setScrollUp(true);
109         }
110
111         g.setGrayScale(255);
112         g.fillRect(0, 0,
113         DeviceFactory.getDevice().getDeviceDisplay().getWidth(),
114         DeviceFactory.getDevice().getDeviceDisplay().getHeight());
115
116         g.setGrayScale(0);
117
118     if (ticker != null) {
119       contentHeight += ticker.paintContent(g);
120     }
121
122     g.translate(0, contentHeight);
123         translatedY = contentHeight;
124
125         contentHeight += title.paint(g);
126     g.drawLine(0, title.getHeight(),
127         DeviceFactory.getDevice().getDeviceDisplay().getWidth(), title.getHeight());
128         contentHeight += 1;
129
130     g.translate(0, contentHeight - translatedY);
131         translatedY = contentHeight;
132
133         g.setClip(0, 0,
134         DeviceFactory.getDevice().getDeviceDisplay().getWidth(),
135         DeviceFactory.getDevice().getDeviceDisplay().getHeight() - contentHeight);
136     g.translate(0, -viewPortY);
137     contentHeight += paintContent(g);
138     g.translate(0, viewPortY);
139
140         if (contentHeight - viewPortY > DeviceFactory.getDevice().getDeviceDisplay().getHeight()) {
141             currentDisplay.setScrollDown(true);
142         } else {
143             currentDisplay.setScrollDown(false);
144         }
145         g.translate(0, -translatedY);
146     }
147
148
149   abstract int paintContent(Graphics g);
150
151
152   void repaint()
153   {
154     super.repaint();
155   }
156
157
158     void showNotify()
159     {
160         viewPortY = 0;
161         
162         super.showNotify();
163   }
164
165 }
166
Popular Tags