Java Reference
In-Depth Information
A simple approach to master the landing is to let the lander fall to 550 meters and then go to full thrust.
When the speed is reduced to 1 m/s, go to 50% thrust.
A full listing of an improved lunar lander example without inserted text is contained in the next section.
Combined Application Example: A Lunar Lander with Graphical Display
As an example of combining some of the techniques demonstrated in this chapter, we will enhance the
lunar lander example from the previous section by adding a graphical display and slightly modifying
the screen layout depending on the ratio between height and width.
For the external camera view component of the lander, we create a new inner class ExternalView
derived from Canvas . In the paint method, we draw a triangle representing the lander and three lines
representing the engine exhaust, depending on the thrust level. The screen position of the lander is
calculated in the getScrY () method by multiplying the real height with the screen height and then
dividing by the maximum height. The old thrust level and display position are saved in order to be able
to determine if the values have changed in the check method. The check method is called from the
ScreenManager . It forces a repaint only if the thrust level or screen height has changed in order to
avoid unnecessary flickering.
The animation could be improved further by repainting only the area of the ExternalView
component that was actually affected by the move of the lander. Even smoother animation would be
possible by using an offscreen buffer as in the MIDP stopwatch example. However, for games MIDP is
probably the more appropriate profile anyway, so we do not repeat the corresponding code here:
class ExternalView extends Canvas {
int oldY;
long oldThrust;
public void paint (Graphics g) {
int x = getSize().width / 2;
int y = getScrY();
g.drawLine (x-5, y-1, x+5, y-1);
g.drawLine (x-5, y-1, x, y - 10);
g.drawLine (x+5, y-1, x, y - 10);
if (thrust > 10) {
g.drawLine (x-3, y+1, x-3, (int) (y + 1 + thrust /
20));
g.drawLine (x+3, y+1, x+3, (int) (y + 1 + thrust /
20));
g.drawLine (x, y+1, x, (int) (y + 1 + thrust / 15));
}
oldY = y;
oldThrust = thrust;
}
public Dimension getPreferredSize() {
return new Dimension (50, 100);
}
public Dimension getMinimumSize() {
return new Dimension (20, 50);
}
Search WWH ::




Custom Search