×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Question How we can call another module from main class

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class Menu extends MIDlet{
    	
    	public static Display display;
    	public CanvasMenu canvas;
    	
    	public Menu()
    	{
    		display=Display.getDisplay(this);
    		canvas=new CanvasMenu(this);
    	}
    	
    	protected void startApp()
    	{
    		display.setCurrent(canvas);
    	}
    	protected void pauseApp()
    	{
    		
    	}
    	protected void destroyApp(boolean uncoditional)
    	{
    		
    	}
    
    	class CanvasMenu extends Canvas implements CommandListener
    	{
    		private Command exit;
    		private Menu menu;
    		//private Font font=Font.getFont(Font.STYLE_BOLD);
    		private String[] options={"New Game","Sound","About Us","Help","Quit"};
    		private int selectedRows=0,gap=30;
    		
    		public CanvasMenu(Menu menu)
    		{
    			this.menu=menu;
    			exit=new Command("Exit", Command.EXIT, 1);
    			addCommand(exit);
    			setCommandListener(this);
    		}
    		
    		protected void paint(Graphics g)
    		{
    			g.setColor(255,255,255);
    			g.fillRect(0, 0, getWidth(), getHeight());
    			g.setColor(0,0,0);
    			g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
    			g.drawString("Main Menu",50,2,Graphics.LEFT|Graphics.TOP);
    			
    			// for displaying menu options
    			g.setColor(0,0,0);
    			g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM));
    			for(int x=0;x<5;x++)
    				g.drawString(options[x], 50, 40+(x)*gap, Graphics.TOP|Graphics.LEFT);
    			
    			//for highlighter
    			g.setColor(23,127,199);
    			g.fillRoundRect(2, 35+(selectedRows)*gap,getWidth()-5,gap-3, 3, 3);
    			//remember
    			//g.fillRoundRect(x,y,width,height,,);
    			
    			g.setColor(0,0,0);
    			g.drawString(options[selectedRows], 50, 40+(selectedRows)*gap, Graphics.TOP|Graphics.LEFT);
    		}
    		
    		public void commandAction(Command choice,Displayable displayable)
    		{
    			if(choice==exit)
    			{
    				destroyApp(false);
    				notifyDestroyed();
    			}
    		}
    		
    		protected void keyPressed(int key)
    		{
    			key=getGameAction(key);
    			
    			switch(key)
    			{
    				case Canvas.UP:
    						selectedRows--;
    						break;
    						
    				case Canvas.DOWN:		
    						selectedRows++;
    						break;		
    						
    				case Canvas.FIRE:
    						//display=Display.getDisplay(this);
    						goToModule();
    						break;
    			}
    			repaint();
    		}
    		
    		protected void goToModule()
    		{
    			switch(selectedRows)
    			{
    				case 0:
    					//NewGame newg=new NewGame(this);
    					display.setCurrent(new NewGame(this));
    					break;
    					
    				case 1:
    					break;
    					
    				case 2:
    					break;
    			}
    		}
    	}
    }
    this is my main class where it display menu screen with the help of canvas class which is as follows

    Menu

    New Game
    Sound
    About Us
    Help
    Quit

    Now i want to do that when i click fire key on "New Game" it should display another screen so for that i have written some code but it gives me error. The code is as follows

    Code:
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;
    
    import Menu.CanvasMenu;
    
    
    public class NewGame{
    
    	//private NewGame newg;
    	private CanvasMenu canvasmenu;
    	
    	//private Display display;
    	
    	public NewGame()
    	{
    		//this.canvas=canvas;
    		canvasmenu=new CanvasMenu(this);
    		//display.setCurrent(canvasmenu);
    	}
    	
    class CanvasMenu extends Canvas
    {
    	private NewGame newg;
    	
    	public CanvasMenu(NewGame newg)
    	{
    		this.newg=newg;
    	}
    	
    	public void paint(Graphics g)
    	{
    		//NewGame newg;
    		String[] options={"Single Player","Multi Player"};
    		int selectedRows=0,gap=30;
    		
    		g.setColor(255,255,255);
    		g.fillRect(0, 0, 60, 60);
    		g.setColor(0,0,0);
    		g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
    		g.drawString("New Game",50,2,Graphics.LEFT|Graphics.TOP);
    		
    		// for displaying menu options
    		g.setColor(0,0,0);
    		g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM));
    		for(int x=0;x<5;x++)
    			g.drawString(options[x], 50, 40+(x)*gap, Graphics.TOP|Graphics.LEFT);
    			
    		//for highlighter
    		g.setColor(23,127,199);
    		g.fillRoundRect(2, 35+(selectedRows)*gap,60-5,gap-3, 3, 3);
    		//remember
    		//g.fillRoundRect(x,y,width,height,,);
    			
    		g.setColor(0,0,0);
    		g.drawString(options[selectedRows], 50, 40+(selectedRows)*gap, Graphics.TOP|Graphics.LEFT);
    	}
    }	
    }
    I m really very much confused in the contructor class of New Game. Please help me regarding this above mentioned code....

  2. #2
    Registered User
    Join Date
    Dec 2009
    Location
    Bangalore/Mysore, Karnataka, India.
    Posts
    43

    Re: How we can call another module from main class

    Hi,
    Can you please quote the error you get.

    Quote Originally Posted by dineshdreamworld View Post
    [CODE]
    display.setCurrent(new NewGame(this));
    As I can see, NewGame constructor doesn't take any parameter.
    Quote Originally Posted by dineshdreamworld View Post
    [CODE]
    public NewGame()
    {
    //this.canvas=canvas;
    canvasmenu=new CanvasMenu(this);
    //display.setCurrent(canvasmenu);
    }
    Hence it should be, display.setCurrent(new NewGame());
    But, this wont solve the problem. Because, setCurrent() takes only 'Displayable' as parameter. NewGame is not a 'Displayable'.

    My suggestion would be to make NewMenu class extend Canvas.

    something like this...
    class NewGame extends Canvas {
    public NewGame(){
    }
    paint(Graphics g){
    //Whatever you wanna paint.
    }
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Re: How we can call another module from main class

    the error is in the NewGame Constructor.

  4. #4
    Regular Contributor
    Join Date
    Feb 2010
    Posts
    69

    Re: How we can call another module from main class

    Quote Originally Posted by dineshdreamworld View Post
    the error is in the NewGame Constructor.
    I Think there is conflict between ur Menu.CanvasMenu class and another CanvasMenu which u have declared in NewGame class.

    in NewGame.java remove import Menu.CanvasMenu; statement because it's not needed there.
    and then try. I hope this will solve your problem.
    Last edited by Enthusiastic; 2010-02-15 at 08:19.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Re: How we can call another module from main class

    Hey thanks for replying. Actually i got the answer. I declared every thing in separate class n that solved my problem

Similar Threads

  1. Replies: 1
    Last Post: 2009-08-06, 16:22
  2. Replies: 2
    Last Post: 2006-08-30, 21:33
  3. possible to enter call cost in main menu?
    By hegdeshashi in forum Symbian
    Replies: 0
    Last Post: 2005-11-15, 12:09
  4. How to call platformRequest() from other class?
    By petertai in forum Mobile Java General
    Replies: 2
    Last Post: 2004-07-19, 10:05
  5. Exceptions while loading application
    By avinash_rs in forum Mobile Java General
    Replies: 0
    Last Post: 2003-06-09, 10:53

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×