This discussion is archived
8 Replies Latest reply: Nov 8, 2011 9:27 AM by morgalr RSS

loading images/drawing them (seperate classes)

898297 Newbie
Currently Being Moderated
for whatever reason this isn't drawing at all and yes I do have "blood.png" in the same directory as my src

also I've done g.drawImage with createBufferStrategy(2) before and it worked fine but I can't access it from another class unless it's static (which defeats the purpose)

graphicsTest.java: http://pastebin.com/fh2HhauX

images.java: http://pastebin.com/UDCtWQfS

Edited by: 895294 on 06-Nov-2011 10:33
  • 1. Re: loading images/drawing them (seperate classes)
    morgalr Explorer
    Currently Being Moderated
    When you create a BufferedImage, you need to create the Graphics for it:
      Graphics g = img.getGraphics();
      //should be changed to 
      Graphics2D g = img.createGraphics();
  • 2. Re: loading images/drawing them (seperate classes)
    morgalr Explorer
    Currently Being Moderated
    I also do not see anyplace where you declare Images as static or instanciate Images for this call:
      Images.load("blood.png");
  • 3. Re: loading images/drawing them (seperate classes)
    morgalr Explorer
    Currently Being Moderated
    This also confuses me:
    extends Canvas
    You're viewable is a JFrame which you create in that Class.

    You confuse the issue by making your runable, your display object, and your controller all in the same class: break apart the funtionality and have a control class separate from your display class.
  • 4. Re: loading images/drawing them (seperate classes)
    morgalr Explorer
    Currently Being Moderated
    Also you're using a Canvas class (AWT) and adding the object to a JFrame (SWING), generally you'll want to stay SWING/SWING or AWT/AWT. Swing is already double buffered, by default, so your definition of dubble buffering would be redundant if you went with a Swing based container like a JPanel.
  • 5. Re: loading images/drawing them (seperate classes)
    morgalr Explorer
    Currently Being Moderated
    Also you are calling your Image.load in your renderer inside an infinite loop--not good. I believe ImageIO is smart enough to know you already have the image loaded, but even so, very bad practice.
  • 6. Re: loading images/drawing them (seperate classes)
    DrClap Expert
    Currently Being Moderated
    895294 wrote:
    for whatever reason this isn't drawing at all and yes I do have "blood.png" in the same directory as my src
    Actually you'll want to have that file in your current working directory. Make sure you know what that is.
  • 7. Re: loading images/drawing them (seperate classes)
    morgalr Explorer
    Currently Being Moderated
    For what you are doing here, do you really need or want active rendering? You have it setup now to basically suck all of your video resourses by updating as fast as you machine possibly can.

    Even if you're looking at doing an animation, you'll need to set a timer for a delay to allow processing to take place other than a screen refresh loop.
  • 8. Re: loading images/drawing them (seperate classes)
    morgalr Explorer
    Currently Being Moderated
    Here's a very simple example:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Junk{
      Junk(){
        JFrame f = new JFrame("Junk");
    //    f.setUndecorated(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyJPanel p = new MyJPanel();
        f.add(p);
        f.pack();
        f.setVisible(true);
      }
      public static void main(String[] args){
        new Junk();
      }
      class MyJPanel extends JPanel{
        BufferedImage bi;
        MyJPanel(){
          try{
            bi = ImageIO.read(new URL("http://i52.tinypic.com/i3b40n.png"));
            this.setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
            this.setBackground(Color.YELLOW);
          }catch(Exception e){
            e.printStackTrace();
          }
        }
        public void paintComponent(Graphics g){
          super.paintComponent(g);
          g.drawImage(bi, 0, 0, this);
        }
      }
    }
    This loads from a URL, but as DrClap pointed out, just put the image in the current working directory--if you don't change it, then it's your project root--and things should be good.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points