Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Help with drawing images

  1. #1
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Help with drawing images

    Hi i have been playing around with the 2d graphics stuff in java and have been trying to just draw an image. This is the code i have. My problem is that the image will not draw and i dont if im doing something wrong. I can draw other things so i went wrong loading the file in. Please help.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PaintComponent extends JPanel{
            int x = 150;
            int y = 150;  
            Image img = Toolkit.getDefaultToolkit().getImage("movegame/s1.jpg"); 
            
        public PaintComponent() {
            setBackground(Color.white);
        }
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            g.drawImage(img, x, y, null);
            
            
        }
    }
    P.S this is only the file that paints stuff.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    We need to see how you're using that class. To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    I see three possibilities: the image may not be loaded when it is painted, the panel may not ba added to a visible GUI, the panel may have a size of [0,0]

    db

  3. #3
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default

    okay, thanks here is the code with my panel:
    Java Code:
            PaintComponent panel = new PaintComponent();
            
            Container pane = gui.getContentPane();
            pane.setLayout(new GridLayout(1,1));
            pane.add(panel);
    My JFrame is set to 'true' for setVisible and set to (600,600)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    I gave you a link to the SSCCE page. Read it.

    db

  5. #5
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default

    okay both files:
    Main class:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class MoveGame {
    
    
        public static void main(String[] args) {
            new MoveGame();
            
        }
        
        public MoveGame() {
            
            JFrame gui = new JFrame();
            gui.setTitle("move game");
            gui.setSize(600, 600);
            
            PaintComponent panel = new PaintComponent();
            
            Container pane = gui.getContentPane();
            pane.setLayout(new GridLayout(1,1));
            pane.add(panel);
            
            
            gui.setVisible(true);
            
        }
        }
    Paint component:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PaintComponent extends JPanel{
            int x = 150;
            int y = 150;  
            Image img = Toolkit.getDefaultToolkit().getImage("movegame/s1.jpg"); 
            
        public PaintComponent() {
            setBackground(Color.white);
        }
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            //g.drawRect(150, 150, 300, 300);
            g.drawImage(img, x, y, null);
            
            
        }
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    At the top of the paintComponent override, add this line:
    Java Code:
    System.out.println(img);
    What does that print?

    db

  7. #7
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default

    i get this:
    Java Code:
    sun.awt.image.ToolkitImage@1e0cf70
    in my output.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Next try
    Java Code:
    System.out.println(image.getWidth(this));
    System.out.println(image.getHeighth(this));
    System.out.println(getWidth());
    System.out.println(getHeighth());
    What does that give you?

    db

  9. #9
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default

    sun.awt.image.ToolkitImage@52fe85
    -1
    -1
    592
    566

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    See those -1's? That indicates that the image isn't loaded.

    Either use a MediaTracker (search the net or read the source of ImageIcon) or, easier, load your image with ImageIO#read(...) which blocks until the image is fully loaded.

    Note that ImageIO#read(...) returns a BufferedImage, hence doesn't support animated GIF.

    db
    Fubarable likes this.

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

    Default

    Quote Originally Posted by DarrylBurke View Post
    Note that ImageIO#read(...) returns a BufferedImage, hence doesn't support animated GIF.
    Shoot and gosh darn! I never knew this, and this explains a code problem I had a few years ago. Thanks!

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Thinking about the problem further, adding the panel as an ImageObserver should cause the panel to repaint once the image is loaded by Toolkit.
    Java Code:
    // g.drawImage(img, x, y, null);
    g.drawImage(img, x, y, this);
    Could you please try that and post back?

    db

Similar Threads

  1. drawing images in swing
    By owboateng in forum New To Java
    Replies: 3
    Last Post: 05-09-2011, 03:27 PM
  2. adding drawing images on a "Graphics instance"
    By imorio in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 11:30 PM
  3. Replies: 4
    Last Post: 12-16-2010, 05:32 PM
  4. Help drawing images for pong game
    By aalsaqri in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 03:10 PM
  5. drawing images
    By diggitydoggz in forum New To Java
    Replies: 4
    Last Post: 01-02-2009, 04:15 AM

Posting Permissions

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