Results 1 to 12 of 12
  1. #1
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default HELP NEEDED with J2ME program

    I'm trying to make a simple program that changes the background color slowly but constantly but wit no success :(

    here's my code:




    CLASS hello.java

    Java Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
        public class hello extends MIDlet implements CommandListener{
    		
    	    HelloCanvas myCanvas;
    	    private Command exitCommand = new Command( "Exit", Command.EXIT, 99 );
    	    private Command toggleCommand = new Command( "Toggle_Msg", Command.SCREEN, 1 );
    		
    	    public hello(){
    	    	
    	        myCanvas = new HelloCanvas();
    	        myCanvas.addCommand( exitCommand );
    	        myCanvas.addCommand( toggleCommand );
    	        myCanvas.setCommandListener( this );
    	}
    	    
    	    public void startApp() throws MIDletStateChangeException{
    	    	
    	    	Display.getDisplay( this ).setCurrent( myCanvas );
    	    	myCanvas.repaint();
    	    }
    	    
    	    public void destroyApp( boolean unconditional ) throws MIDletStateChangeException{
    	    }
    	    
    	    public void pauseApp(){
    	    }
    	    
    	    public void commandAction( Command c, Displayable s ){
    	    	
    	    	if( c == toggleCommand ){
    	    		
    	    		myCanvas.toggleHello();
    	    	}else if( c == exitCommand ){
    	    		
    	    		try{
    	    			
    	    			destroyApp( false );
    	    			notifyDestroyed();
    	    		}catch ( MIDletStateChangeException ex ){
    	    		}
    	    	}
    	    }
    }

    CLASS HelloCanvas.java


    Java Code:
    import javax.microedition.lcdui.*;
    
    public class HelloCanvas extends Canvas{
    
    	boolean mySayHello = true;
    	
    	void toggleHello(){
    		
    		mySayHello = !mySayHello;
    		repaint();
    	}
    	
    	public void paint( Graphics g ){
        		
    		int width = getWidth();
    		int height = getHeight();
    		int z = 0;
    		for( z = 0; z <= 510; z++ ){
    			while( z != 254 ){
    			    g.setColor( z, 0, 0 );
    		        g.fillRect( 0, 0, width, height );
    		        for( int i = 0; i <= 100; i++){		    	
    		    	    for( int j = 0; j <= 100; j++ ){	
    		    	    }
    		        }
    			}
    		    
    		    if( z == 254 ){
    		    	
    		    	for( int x = 255; x >=0; x--){
    		    		
    		    		g.setColor( x, 0, 0 );
    		    		g.fillRect( 0, 0, width, height );
    		    	}
    		    }
    		    repaint();
    		}
    		
    		if ( mySayHello ){
    			
    			Font font = g.getFont();
    			int fontHeight = font.getHeight();
    			int fontWidth = font.stringWidth( "Hello World" );
    			g.setColor( 0, 0, 0 );
    			g.setFont(font);
    			g.drawString( "Hello world", (width - fontWidth)/2 , ( height - fontHeight )/2 , g.TOP | g.LEFT );
    		}
    	}
    }
    Heres how it looks within the IDE NetBeans : D

    HELP NEEDED with  J2ME program-j2me.jpg

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: HELP NEEDED with J2ME program

    Are you sure you did this on NetBeans? Seems to me it is Eclipse.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: HELP NEEDED with J2ME program

    When you do,

    Java Code:
    g.fillRect( 0, 0, width, height );
    check the width and height is set.

  4. #4
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: HELP NEEDED with J2ME program

    LMAO it's Eclipse not NetBeans xDD

    width and height are already set in the following lines:


    Java Code:
    public void paint( Graphics g ){
        		
    		int width = getWidth();
    		int height = getHeight();
    Last edited by just_in_deed; 03-04-2012 at 04:51 AM.

  5. #5
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: HELP NEEDED with J2ME program

    I shortened the code just to paint the background black and the TEXT white and it works but that's not what I want the code to do I want it to slowly change the background color.

    he'res the shortened code:


    Java Code:
    public void paint( Graphics g ){
        		
    		int width = getWidth();
    		int height = getHeight();
    		g.setColor( 0, 0, 0 );
    		g.fillRect(0, 0, width, height);
    		
    		if ( mySayHello ){
    			
    			Font font = g.getFont();
    			int fontHeight = font.getHeight();
    			int fontWidth = font.stringWidth( "Hello World" );
    			g.setColor( 255, 255, 255 );
    			g.setFont(font);
    			g.drawString( "Hello world", (width - fontWidth)/2 , ( height - fontHeight )/2 , g.TOP | g.LEFT );
    		}
    	}
    }
    HELP NEEDED with  J2ME program-777.jpg

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: HELP NEEDED with J2ME program

    Never call repaint() from inside a paint(...) override. Run the loop that changes the color parameters on a background Thread and call repaint() every time the color is to be changed. Possibly, use Timer/TimerTask for updating the color parameters.

    More information that will further your understanding:
    Networking, User Experience, and Threads
    Using Threads in J2ME Applications

    Take the time to read and understand the code in those articles.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: HELP NEEDED with J2ME program

    this is the renewed code USING threads but still not working as espected:


    Java Code:
    import javax.microedition.lcdui.*;
    
    public class HelloCanvas extends Canvas implements Runnable{
    
    	int x = 1;
    	
    	Thread thread = null;
    	boolean mySayHello = true;
    	
    	public void init(){
    
    	    try{
    		
    		}catch ( Exception e ){
    
    		    e.printStackTrace();
    		}
     	}
    
    	public void start(){
    
    	    if( thread == null ){
    
    	        thread = new Thread( this );
    		thread.start();    
    	    }
    	}
    
    	public void stop(){
    		
    	    thread = null;
    	}
    
    	public void run(){
    
    		int z = 0;
            
    	    while( true ){
    
    		try{
    		    
    		    for( z = 1; z <= 255; z++ ){
    			
    		        x++;
    		        Thread.sleep( 100 );
    		    }
    		        }catch ( InterruptedException e ) {
    		        }
    		    repaint();
    	        }
    	}
    
    	public void toggleHello(){
    		
    		mySayHello = !mySayHello;
    	}
    	
    	public void paint( Graphics g ){
    		
    	    int width = getWidth();
    	    int height = getHeight();
    	    g.setColor( x, 0, 0 );
    	    g.fillRect( 0, 0, width, height );
    		
    		if ( mySayHello ){
    			Font font = g.getFont();
    			int fontHeight = font.getHeight();
    			int fontWidth = font.stringWidth( "Hello World" );
    			g.setColor( 255, 255, 255 );
    			g.setFont(font);
    			g.drawString( "Hello world", (width - fontWidth)/2 , ( height - fontHeight )/2 , g.TOP | g.LEFT );
    		}
    	}
    }
    HELP NEEDED with  J2ME program-example.jpg
    Last edited by just_in_deed; 03-04-2012 at 08:25 AM.

  8. #8
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: HELP NEEDED with J2ME program

    HELP NEEDED with  J2ME program-doubt.jpg

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: HELP NEEDED with J2ME program

    So declare it as an instance field and not as a method-local variable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: HELP NEEDED with J2ME program

    ok I implemented the use of instance field but it's not repainting???


    Java Code:
    import javax.microedition.lcdui.*;
    
    public class HelloCanvas extends Canvas implements Runnable{
    
    	
    	
    	Thread thread = null;
    	boolean mySayHello = true;
    	
    	public void init(){
    
    	    try{
    		
    		}catch ( Exception e ){
    
    		    e.printStackTrace();
    		}
     	}
    
    	public void start(){
    
    	    if( thread == null ){
    
    	        thread = new Thread( this );
    		thread.start();    
    	    }
    	}
    
    	public void stop(){
    		
    	    thread = null;
    	}
    
    	public void run(){
    
    
    	}
    	
    	public int process(){
    		
    		int x = 1;
    		int z = 0;
    	    while( true ){
    
    		try{
    		    
    		    for( z = 1; z <= 255; z++ )
    		    	
    		        x ++;
    		    	Thread.sleep( 100 );
    		        repaint();
    		        return x;
    		        }catch ( InterruptedException e ) {
    		        }repaint();
    	        }
    	}
    
    	
    	
    	public void toggleHello(){
    		
    		mySayHello = !mySayHello;
    	}
    	
    	public void paint( Graphics g ){
    		
    		if ( mySayHello ){
    		
    		    HelloCanvas c = new HelloCanvas();
    	        int width = getWidth();
    	        int height = getHeight();
    	        g.setColor( c.process() , 0, 0 );
    	        g.fillRect( 0, 0, width, height );
    		}
    	}
    }

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: HELP NEEDED with J2ME program

    There's only one line of code inside the for (z = 1; z <= 255; z++) loop. Learn to follow accepted coding conventions and you won't make a silly mistake like that again.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: HELP NEEDED with J2ME program

    Thanks alot the program is running fine now : D full speed ( ^.^ )

Similar Threads

  1. Help needed with simple program
    By Nelitzeb in forum New To Java
    Replies: 3
    Last Post: 12-14-2011, 03:58 PM
  2. Calender Program Help Needed!!
    By DavidEvans in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 06:37 PM
  3. Help needed with 'shop' program
    By jkhamler in forum New To Java
    Replies: 1
    Last Post: 12-06-2009, 05:35 PM
  4. Programmer needed for J2ME project
    By Sber in forum Reviews / Advertising
    Replies: 0
    Last Post: 11-04-2009, 02:51 PM
  5. Help needed writing a program...
    By Francis in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 02:03 PM

Posting Permissions

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