Results 1 to 5 of 5

Thread: Image

  1. #1
    heilla is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default Image

    Hey guys,
    i'm sorry to bother you , but i just can't get my images to work
    Java Code:
    draw(g)
    thats in my paint event

    en this is the draw method
    Java Code:
        public void draw(Graphics g) {
            System.out.println("dqsedze");
            bg=Toolkit.getDefaultToolkit().getImage("C:\\Users\\PHL\\My Documents\\NetBeansProjects\\game2\\star.jpg");
            g.drawImage(bg,0,0,this);
            g.drawImage(a.getImage(),200,200,this);
            setVisible(true);
            
        }
    could anyone please tell me what i'm doing wrong? :s


    ps here is the main paint method

    Java Code:
       public void paint(Graphics g) {
            // create a sprite, add it the cache then return it
            enemy en = new enemy();
            en.drawenemy(g);
            g.drawRect(0, 50, 750, 600);
            Font font1 = new Font("verdana", Font.PLAIN,  22);
            Font font2 = new Font("gothic", Font.PLAIN , 30);
            Font font3 = new Font("verdana" ,Font.BOLD,26);
            g.setFont(font2);
            g.drawString("PI" , 20 , 25);
            g.drawLine(0, 50, 2000, 50);
            g.setFont(font1);
            g.drawString("Stats" , 100 , 25);
            g.drawString("Inventory", 170 , 25);
            g.drawString(s,x,y);
            if (x >= 100 &&y<= 60 && mouseclicked && x<=153 && y>=0){
                    g.drawString("you clicked on Stats",50,50);
                    stats.drawStats(g);
                    x=currentPosx;
                    y=currentPosy;
                    character.drawchar(g);
                    varopen = 1;
                    inventoryO=0;
            }
            else if (x >= 165 &&y<= 60 && mouseclicked && x<=270 && y>=0){
                inventory.drawInvent(g);
                g.drawString("You just clicked on inventory",50,50);
                x=currentPosx;
                y=currentPosy;
                character.drawchar(g);
                inventoryO=1;
            }
            else if (x >= 800 &&y<= 150 && mouseclicked && x<=865 && y>=100 && var1.equals("money")){
                g.drawString("This is money",50,50);
                inventory.drawmoney(g);
                x=currentPosx;
                y=currentPosy;
            }
            else if (y >= 50 || key.equals("left")) {
                character.drawchar(g);
                inventoryO=0;
            }
            else{
                character.drawcharR(g);
            }
            if(inventoryO==1){
            inventory.drawInvent(g);}
            g.drawString(name, x, y);
            inventory.drawInvent(g);
            stats.drawStats(g);
            inventory.drawmoney(g);
    
    
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I haven't gone through all that code, and you may wish to prune out unnecessary code to make it easier for others to analyze it.

    One problem I see is :
    Java Code:
        public void draw(Graphics g) {
            System.out.println("dqsedze");
            bg=Toolkit.getDefaultToolkit().getImage("...."); // *** here ***
            g.drawImage(bg,0,0,this);
            g.drawImage(a.getImage(),200,200,this);
            setVisible(true);
        }
    You are trying to upload an image from within a painting method, and you should try to avoid this. Painting needs to be done as quickly as possible since this is what the user sees as your program responsiveness. So to avoid this, you should upload the image, then call repaint on your image displaying jpanel. If you have few images to upload, then do it once in the beginning, not the gabillion times that the paint method is called.

    Another problem I see is that you are overriding the paint method. If this is a Swing app, you should be overriding the paintComponent method of a JComponent (such as a JPanel). You should probably call super.paint(g) (if in a paint method) or super.paintComponent(g) if in a paintComponent override) and this call should be the first call of your paint/paintComponent override. Also, where do you call draw(g) from within your paint method?

    edit: also, this is a bad idea:
    Java Code:
       public void paint(Graphics g) {
            // create a sprite, add it the cache then return it
            enemy en = new enemy();
            en.drawenemy(g);
    Here you're creating an enemu object from within a paint method, and another rule about painting is that program logic should not go in the paint method. You really don't have much control when this method will be called as your repaint() calls only suggest that it should be called, and the OS might demand that it be called, for which you can do little about. Much better is to do your program logic within your game loop, and call repaint() as needed.
    Last edited by Fubarable; 12-20-2009 at 04:43 PM.

  3. #3
    heilla is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks for your reply,


    so can i call the loading of my images in my init() method and then have them drawn on the screen?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Why not try it? :)

  5. #5
    heilla is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2009, 01:44 AM
  2. Replies: 14
    Last Post: 04-10-2009, 09:46 AM
  3. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 02:25 PM
  4. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 07:17 PM
  5. Replies: 0
    Last Post: 01-08-2008, 06:29 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
  •