Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

why isnt the image drawn?

im trying to create a game but i have a problem,the images are not drawn.

here is the code

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;

/**
 *
 * @author  omashhour
 * @version
 */
public class slime extends MIDlet implements CommandListener{
    private Command exitCommand; // The exit command
    Display display;
    gamecore gc;    
   
    public slime(){
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.SCREEN, 2);
       
        gc=new gamecore();
    }
    public void startApp() {
       
        display.setCurrent(gc);
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    }
   
    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
}

class gamecore extends Canvas implements Runnable{
   
    Thread thread;
    Image offImage;
    Graphics og;
    Player p;

    public gamecore(){
       
        p=new Player();
       
        thread=new Thread();
        thread.start();
       
       
       
    }
   
     public void paint(Graphics g) {
        if(og==null){
            offImage=Image.createImage(getWidth(),getHeight());
            og=offImage.getGraphics();
        }
        p.draw(og);
        g.drawImage(offImage,0,0,Graphics.TOP|Graphics.LEFT);

     }
     
     public void run(){
         while(true){
           
            try{
                thread.sleep(20);
            }
            catch(Exception e){
            }
            repaint();
         }
         
     }
     
}

class Player extends gamecore{
   
    Image player;
   
    public Player(){
        player=loadimage("/gondy.png");
    }
   
    public Image loadimage(String file){
        Image image=null;
        try{
            image=Image.createImage(file);
        }
        catch(Exception e){
        }
        return image;
    }
   
    public void draw(Graphics g){
        g.drawImage(player,30,30,Graphics.TOP|Graphics.LEFT);        
    }
   
}
0
omashhour
Asked:
omashhour
  • 3
  • 3
1 Solution
 
nesnemisCommented:
Hi omashhour,

maybe you should check if you get an exception, file not found or something.
 
try{
            image=Image.createImage(file);
        }
        catch(Exception e){
                //check if any exception
                System.out.println(e.getMessage());
        }


nesnemis
0
 
petmagdyCommented:
Hi omashhour,

where is the png file inside ur J2ME application jar?

Cheers!
0
 
petmagdyCommented:
aaahhhh, in ur midlet

>>         gc=new gamecore();
it should be :

gc = new Player();
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
omashhourAuthor Commented:
i get no exception  and the i tried gc =new player(); it didnt work.
btw i will make another class called cpu  it will also have a draw method ,so i think both draw methods in cpu ad player should be called in the draw method inside gamecore, but i dont know how
0
 
petmagdyCommented:
u didn't answer my question:

>> where is the png file inside ur J2ME application jar?

usually u put the images inside a directory from the code root called res

and call like this
loadimage("/res/gondy.png");


also please try on Player class and don't make Player class extends game core for testing purpose
0
 
omashhourAuthor Commented:
the image is in the same folder as the java file i still didnt create a jar file
0
 
omashhourAuthor Commented:
thanks it worked when i didnt extend the class :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now