Advertisement

11.18.2008 at 12:52PM PST, ID: 23915765
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Help with J2ME GameCanvas load

Asked by Rocker4500 in Java 2 Micro-Edition (J2ME), Game Programming User Interface, Java Programming Language

Tags: ,

Ok, basically what i am doing is creating a game, the game uses a Display manager to display a number of forms and then when the user selects play on the menuscreen it should load the game canvas....however i get an error.

The line it doesnt seem to like, in the Main.java is :   gamescrn = new GameScreen(this);
The error this line gives it:
unreported exception java.lang.exception; must be caught or declared to be thrown

(ignor all the code workings in the GameScreen.java i havnt worked on making ti work  yet)

All i need is to be able to load the game canvas however i cant!  There seems to be some sort of problem between the Main.java and the GameScreen.java


Any help would be much appreciated

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
*****************  Main.java ***********************
 
mport javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
/**
 * @author Gary
 */
public class Main extends MIDlet {
  private Display display; // The display
 
  protected Splash splashscrn; // Canvas to display timer
 
  protected Menu menuscrn; // List to change timer options
  
  protected GameScreen gamescrn; // List to change timer options
  
  
 
 // protected HighScore scorescrn; // Form with gauge to set timer sleep
 
  //protected Instructions instructscrn; // Class to help manage screens
  
  protected DisplayManager displayMgr; // Class to help manage screens
  
 
  public Main()  {  
    display = Display.getDisplay(this);
    splashscrn = new Splash(this);
    menuscrn = new Menu("Menu", List.IMPLICIT, this);
    gamescrn = new GameScreen(this);
 
 
    // Create a display manager object
    displayMgr = new DisplayManager(display, splashscrn);
  }
 
  protected void startApp() {
    // Start with the canvas
    display.setCurrent(splashscrn);
    
    
  }
 
  protected void pauseApp() {
  }
 
  protected void destroyApp(boolean unconditional) {
  }
 
  public void exitMIDlet() {
    destroyApp(true);
    notifyDestroyed();
  }
}
 
 
 
 
 
*************** Menu.java ********************
 
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
/**
 * @author Gary
 */
 
public class Menu extends List implements CommandListener {
   
    private Main midlet; // Main midlet
    private Command cmquickexit;
    private Command cmselect;
    
    public Menu(String title, int listType, Main midlet) {
 
    super(title, listType);
 
    // Save reference to MIDlet so we can
    // access the display manager class
    this.midlet = midlet;
 
    append("Play", null);
    append("High Scores", null);
    append("Instructions", null);
    append("Exit", null);
    
    cmquickexit = new Command("Exit", Command.EXIT, 1);
    cmselect = new Command("Select", Command.ITEM, 2);
    addCommand(cmquickexit);
    addCommand(cmselect);
    setCommandListener(this);
        
    }
    
    public void commandAction(Command c, Displayable s) {
    // Event generated by the implicit list
    if (c == List.SELECT_COMMAND) {
      switch (getSelectedIndex()) {
      case 0:
        // Push current displayable and show the form
        // to adjust the timer sleep
       midlet.displayMgr.pushDisplayable(midlet.gamescrn);
        
          
        
        break;
 
      /**case 1:
        // Start timer and return to previous displayable
        midlet.cvTimer.startTimer();
        midlet.displayMgr.popDisplayable();
        break;
 
      case 2:
        // Stop timer and return to previous displayable
        midlet.cvTimer.stopTimer();
        midlet.displayMgr.popDisplayable();
        break;*/
      }
    
    }
  }
 
    
}
 
 
 
   
****************** DisplayManager.java ***********************
 
import java.util.Stack;
import javax.microedition.lcdui.*;
 
 
class DisplayManager extends Stack {
  private Display display; // Reference to Display object
 
  private Displayable mainDisplayable; // Main displayable for MIDlet
 
  private Alert alStackError; // Alert for error conditions
 
  /*--------------------------------------------------
   * Display manager constructor
   *-------------------------------------------------*/
  public DisplayManager(Display display, Displayable mainDisplayable) {
    // Only one display object per midlet, this is it
    this.display = display;
    this.mainDisplayable = mainDisplayable;
 
    // Create an alert displayed when an error occurs
    alStackError = new Alert("Displayable Stack Error");
    alStackError.setTimeout(Alert.FOREVER); // Modal
  }
 
  /*--------------------------------------------------
   * Push the current displayable onto stack and set
   * the passed in displayable as active
   *-------------------------------------------------*/
  public void pushDisplayable(Displayable newDisplayable) {
    push(display.getCurrent());
    display.setCurrent(newDisplayable);
  }
 
  /*--------------------------------------------------
   * Return to the main displayable object of MIDlet
   *-------------------------------------------------*/
  public void home() {
    while (elementCount > 1)
      pop();
    display.setCurrent(mainDisplayable);
  }
 
  /*--------------------------------------------------
   * Pop displayable from stack and set as active
   *-------------------------------------------------*/
  public void popDisplayable() {
    // If the stack is not empty, pop next displayable
    if (empty() == false)
      display.setCurrent((Displayable) pop());
    else
      // On error show an alert
      // Once acknowledged, set 'mainDisplayable' as active
      display.setCurrent(alStackError, mainDisplayable);
  }
}
 
 
 
 
***************** GameScreen.java *************************
 
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
 
 
public class GameScreen extends javax.microedition.lcdui.game.GameCanvas {
 
  //---------------------------------------------------------
  //   dimension fields
  //  (constant after initialization)
 
  /**
   * the height of the green region below the ground.
   */
  static final int GROUND_HEIGHT = 32;
 
  /**
   * a screen dimension.
   */
  static final int CORNER_X = 0;
 
  /**
   * a screen dimension.
   */
  static final int CORNER_Y = 0;
 
  /**
   * a screen dimension.
   */
  static int DISP_WIDTH;
 
  /**
   * a screen dimension.
   */
  static int DISP_HEIGHT;
 
  /**
   * a font dimension.
   */
  static int FONT_HEIGHT;
 
  /**
   * the default font.
   */
  static Font FONT;
 
  /**
   * a font dimension.
   */
  static int SCORE_WIDTH;
 
  /**
   * The width of the string that displays the time,
   * saved for placement of time display.
   */
  static int TIME_WIDTH;
 
  /**
   * color constant
   */
  public static final int BLACK = 0;
 
  /**
   * color constant
   */
  public static final int WHITE = 0xffffff;
  
 private Main midlet; // Main midlet
 
 
 
  //---------------------------------------------------------
  //   game object fields
 
  /**
   * a handle to the display.
   */
  private Display myDisplay;
 
  
 
  /**
   * the LayerManager that handles the game graphics.
   */
  private LayerManager layermanager;
 
  /**
   * whether or not the game has ended.
   */
  private boolean GameOver;
 
  /**
   * the player's score.
   */
  private int Score = 0;
 
  /**
   * How many ticks we start with.
   */
  private int intGameTicks = 950;
 
  /**
   * this is saved to determine if the time string needs 
   * to be recomputed.
   */
  private int OldGameTicks = intGameTicks;
 
  /**
   * the number of game ticks that have passed.
   */
  private int GameTicks = OldGameTicks;
 
  /**
   * we save the time string to avoid recreating it 
   * unnecessarily.
   */
  private static String InitialString = "1:00";
 
  /**
   * we save the time string to avoid recreating it 
   * unnecessarily.
   */
  private String TimeString = InitialString;
 
  //-----------------------------------------------------
  //    gets/sets
 
  /**
   * This is called when the game ends.
   */
 
 
  //-----------------------------------------------------
  //    initialization and game state changes
 
  /**
   * Constructor sets the data, performs dimension calculations, 
   * and creates the graphical objects.
   */
  public GameScreen(Main midlet) throws Exception {
    super(false);
 
    this.midlet = midlet;
    //myDisplay = Display.getDisplay(midlet);
    
    // calculate the dimensions
    DISP_WIDTH = getWidth();
    DISP_HEIGHT = getHeight();
    Display disp = Display.getDisplay(midlet);
    if(disp.numColors() < 256) {
      throw(new Exception("game requires 256 shades"));
    }
    /*if((DISP_WIDTH < 150) || (DISP_HEIGHT < 170)) {
      throw(new Exception("Screen too small"));
    }
    if((DISP_WIDTH > 250) || (DISP_HEIGHT > 250)) {
      throw(new Exception("Screen too large"));
    }*/
    FONT = getGraphics().getFont();
    FONT_HEIGHT = FONT.getHeight();
    SCORE_WIDTH = FONT.stringWidth("Score: 000");
    if(layermanager == null) {
      layermanager = new LayerManager(CORNER_X, CORNER_Y + FONT_HEIGHT*2, 
	   DISP_WIDTH, DISP_HEIGHT - FONT_HEIGHT*2 - GROUND_HEIGHT);
    } 
  }
 
  
  void setGameOver() {
    GameOver = true;
    midlet.pauseApp();
  }
    
  /**
   * This is called as soon as the application begins.
   */
  void start() {
    GameOver = false;
    myDisplay.setCurrent(this);
    repaint();
  }
 
  /**
   * sets all variables back to their initial positions.
   */
  void reset() {
    layermanager.reset();
    Score = 0;
    GameOver = false;
    GameTicks = intGameTicks;
    OldGameTicks = intGameTicks;
    repaint();
  }
 
  /**
   * clears the key states.
   */
  void flushKeys() {
    getKeyStates();
  }
 
  /**
   * This version of the game does not deal with what happens
   * when the game is hidden, so hopefully it won't be hidden...
   * see the version in the next chapter for how to implement 
   * hideNotify and showNotify.
   */
  protected void hideNotify() {
  }
 
  /**
   * This version of the game does not deal with what happens
   * when the game is hidden, so hopefully it won't be hidden...
   * see the version in the next chapter for how to implement 
   * hideNotify and showNotify.
   */
  protected void showNotify() {
  }
 
  //-------------------------------------------------------
  //  graphics methods
 
  /**
   * paint the game graphic on the screen.
   */
  public void paint(Graphics g) {
    // clear the screen:
    g.setColor(WHITE);
    g.fillRect(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT);
    // color the grass green
    g.setColor(0, 255, 0);
    g.fillRect(CORNER_X, CORNER_Y + DISP_HEIGHT - GROUND_HEIGHT, 
	       DISP_WIDTH, DISP_HEIGHT);
    // paint the layer manager:
    
      layermanager.paint(g);
   
    // draw the time and score
    g.setColor(BLACK);
    g.setFont(FONT);
    g.drawString("Score: " + Score, 
		 (DISP_WIDTH - SCORE_WIDTH)/2, 
		 DISP_HEIGHT + 5 - GROUND_HEIGHT, g.TOP|g.LEFT);
    // write game over if the game is over
    if(GameOver) {
      //midlet.setNewCommand();
      // clear the top region:
      g.setColor(WHITE);
      g.fillRect(CORNER_X, CORNER_Y, DISP_WIDTH, FONT_HEIGHT*2 + 1);
      int goWidth = FONT.stringWidth("Game Over");
      g.setColor(BLACK);
      g.setFont(FONT);
      g.drawString("Game Over", (DISP_WIDTH - goWidth)/2, 
      		   CORNER_Y + FONT_HEIGHT, g.TOP|g.LEFT);
    }
  }
  
  /**
   * a simple utility to make the number of ticks look like a time...
   */
  public String formatTime() {
    if((GameTicks / 16) + 1 != OldGameTicks) {
      TimeString = "";
      OldGameTicks = (GameTicks / 16) + 1;
      int smallPart = OldGameTicks % 60;
      int bigPart = OldGameTicks / 60;
      TimeString += bigPart + ":";
      if(smallPart / 10 < 1) {
	TimeString += "0";
      }
      TimeString += smallPart;
    }
    return(TimeString);
  }
 
  //-------------------------------------------------------
  //  game movements
 
  /**
   * Tell the layer manager to advance the layers and then 
   * update the display.
   */
  void advance() {
    GameTicks--;
    Score += layermanager.advance(GameTicks);
    if(GameTicks == 0) {
      setGameOver();
    }
    // paint the display
    
      paint(getGraphics());
      flushGraphics();
    
  }
 
  /**
   * Respond to keystrokes.
   */
  public void checkKeys() { 
    if(! GameOver) {
      int keyState = getKeyStates();
      if((keyState & LEFT_PRESSED) != 0) {
	layermanager.setLeft(true);
      } 
      if((keyState & RIGHT_PRESSED) != 0) {
	layermanager.setLeft(false);
      }
      if((keyState & UP_PRESSED) != 0) {
	layermanager.shoot();
      } 
    }
  }
 
}
[+][-]11.18.2008 at 08:52PM PST, ID: 22991638

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java 2 Micro-Edition (J2ME), Game Programming User Interface, Java Programming Language
Tags: J2ME, unreported exception java.lang.exception; must be caught or declared to be thrown
Sign Up Now!
Solution Provided By: CodeFish
Participating Experts: 1
Solution Grade: A
 
 
[+][-]12.15.2008 at 07:47AM PST, ID: 23174440

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]12.15.2008 at 07:51AM PST, ID: 23174487

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628