Results 1 to 19 of 19
  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Trouble With drawImage()

    Java Code:
    pnt.drawImage(image,0,0,w.b);
    Java Code:
    pnt.drawImage(image.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,w.b);
    pnt ----- Graphics2D
    image --- BufferedImage
    w.b ----- JPanel being drawn on


    I literally copied and pasted from code in the w.b object and changed "this" to "w.b" (both refer to the same thing). It throws the same error no matter which of the two codes I use.

    What is wrong?

    (I hate Image SOOO much )

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Trouble With drawImage()

    What error?
    We're not mind readers.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Trouble With drawImage()

    I am not certain you needed a new thread for this since it appeared to be a continuation of something earlier (but I'm not a moderator so I could be wrong).
    Resize BufferedImage

    In any event, are you actually implementing an image observer? Normally I just use null unless I need to monitor the progress. It is the graphics context which determines the destination of the drawn image, not the image observer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Quote Originally Posted by Tolls View Post
    What error?
    We're not mind readers.
    Sorry. Will remember next time.

    Quote Originally Posted by jim829 View Post
    I am not certain you needed a new thread for this since it appeared to be a continuation of something earlier (but I'm not a moderator so I could be wrong).
    Resize BufferedImage

    In any event, are you actually implementing an image observer? Normally I just use null unless I need to monitor the progress. It is the graphics context which determines the destination of the drawn image, not the image observer.

    Regards,
    Jim
    It is similar but not the same, so I didn't really know where to put it.

    Putting null worked, thanks! Is there a time when using "this" would be better?
    In another part of my code, "this" works fine. Should I change it to null or does it matter?

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Trouble With drawImage()

    JPanel (as well as many other Swing classes) implement the ImageObserver interface so it is okay to put it there. I just usually put null there unless I need status updates on the image to guide subsequent processing. Since you didn't provide an error message I can't tell why it was not working properly for you.

    Check out Images (The Java™ Tutorials > 2D Graphics > Overview of the Java 2D API Concepts) for more info on image processing.

    Regards,
    Jim
    Last edited by jim829; 04-12-2013 at 02:41 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Thanks.

    NEW QUESTION: I have a Buffered image taken from a png. The png has transparent color. When I paint the image, the transparent part gets painted black. How do I fix this? (It's a very simple image, so editing it is no problem)

    ALSO: What do I add a KeyListener to? JFrame, JPanel (used for drawing), something else?
    Last edited by Prime624; 04-12-2013 at 07:08 AM.

  7. #7
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Quote Originally Posted by Prime624 View Post
    Thanks.

    NEW QUESTION: I have a Buffered image taken from a png. The png has transparent color. When I paint the image, the transparent part gets painted black. How do I fix this? (It's a very simple image, so editing it is no problem)

    ALSO: What do I add a KeyListener to? JFrame, JPanel (used for drawing), something else?
    I'm pretty new, but I find this resource invaluable: Java Platform SE 7 From the following I'd say you can add it to the class.

    KeyListener:

    The listener interface for receiving keyboard events (keystrokes). The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).

    The listener object created from that class is then registered with a component using the component's addKeyListener method. A keyboard event is generated when a key is pressed, released, or typed. The relevant method in the listener object is then invoked, and the KeyEvent is passed to it.
    Last edited by NotTooOld; 04-12-2013 at 07:50 AM.

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Trouble With drawImage()

    For your issue with transparency. Change the BufferedImage 'type' to include the alpha values. These values govern the transparency of each pixel.

    Regards.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Trouble With drawImage()

    So you are reading an existing image but not creating one. I tried this several times for different .png files and had no problem reading the image and displaying it properly. Usually this occurs when I try to convert an image from one format to another. Could you attach the image in question? Or at least one that exhibits the same behavior?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Trouble With drawImage()-mariostanding.png Simple 12X16 pixel pic of Mario. Created with Paint.net, by erasing the whole canvas then painting over. (I also tried painting over the transparent squares with transparent white and black. Nothing changed.) It shows up transparent every other time I look at it, but the transparent part becomes black in my program.
    Trouble With drawImage()-marioinprogram.png
    Java Code:
    mario=new BufferedImage((int)width,(int)height,BufferedImage.TYPE_INT_RGB);
            pnt=mario.createGraphics();
            try {
                image = ImageIO.read(new File("src\\mariowun\\pic\\marioStanding.png"));
            } catch (IOException e) {
            }
            pnt.drawImage(image.getScaledInstance((int)width,(int)height,Image.SCALE_SMOOTH),0,0,null);
    Thanks for the help.

    EDIT: Using TYPE_4BYTE_ABGR fixed it.
    Last edited by Prime624; 04-13-2013 at 07:53 PM.

  11. #11
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Quote Originally Posted by Ronin View Post
    For your issue with transparency. Change the BufferedImage 'type' to include the alpha values. These values govern the transparency of each pixel.

    Regards.
    Sounds promising. Could you please be a little more specific? Thanks.

    EDIT: Do you mean change "BufferedImage.TYPE_INT_RGB" to something else? What would I change it to?

    EDIT2: Awesome! I used TYPE_4BYTE_ABGR and it worked. Thanks.
    Last edited by Prime624; 04-13-2013 at 07:53 PM.

  12. #12
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Quote Originally Posted by NotTooOld View Post
    I'm pretty new, but I find this resource invaluable: Java Platform SE 7 From the following I'd say you can add it to the class.

    KeyListener:

    The listener interface for receiving keyboard events (keystrokes). The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).

    The listener object created from that class is then registered with a component using the component's addKeyListener method. A keyboard event is generated when a key is pressed, released, or typed. The relevant method in the listener object is then invoked, and the KeyEvent is passed to it.
    I looked at the articles, and I still have no clue what is wrong. I usually do fine using KeyListeners.

    Java Code:
    w.b.addKeyListener(this);
    w.b.addMouseListener(this);
    this is the main class that does stuff. Everything is done from it.
    w is a JFrame that has a JButton in the SOUTH and a JPanel in the CENTER.
    b is the JPanel in the center of w. It is just for drawing with paintComponent.

    this implements the listeners. My mouseListener works fine.

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Trouble With drawImage()

    I am curious how this worked since if you are reading an existing png image the type would normally be set by the returned BufferedImage via ImageIO.read(). Unless the png was created wrong in the first place. Or perhaps I am mis-understanding something. In fact, TYPE_4BYTE_ABGR is the type that was returned on all the png images I loaded.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Quote Originally Posted by jim829 View Post
    I am curious how this worked since if you are reading an existing png image the type would normally be set by the returned BufferedImage via ImageIO.read(). Unless the png was created wrong in the first place. Or perhaps I am mis-understanding something. In fact, TYPE_4BYTE_ABGR is the type that was returned on all the png images I loaded.

    Regards,
    Jim
    Weird.

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Trouble With drawImage()

    When you asked your second question I didn't realize (or notice) that you were copying the image to a BufferedImage before painting. I don't believe it is necessary. What happens if you do this?

    Java Code:
    BufferedImage image;  // instance field
    
    getImage();
    
    
    public void getImage() {
      try {
          image = ImageIO.read(new File("src\\mariowun\\pic\\marioStanding.png"));
      } catch (IOException e) {
          e.printStackTrace();
      }
       repaint();
    }
    
    
    public void paintComponent(Graphics g) {
            if (image == null)
                  return;
            g.drawImage(image.getScaledInstance((int)width,(int)height,Image.SCALE_SMOOTH),0,0,null);
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Works! Thanks. I assume it would be better to use this code than my old code?

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Trouble With drawImage()

    Probably only because you are needlessly copying a BufferedImage to another BufferedImage. But your experience with your previous code will come in handy in the future because sometimes you want to do just that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Trouble With drawImage()

    Awesome.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Trouble With drawImage()

    Quote Originally Posted by jim829 View Post
    Probably only because you are needlessly copying a BufferedImage to another BufferedImage. But your experience with your previous code will come in handy in the future because sometimes you want to do just that.

    Regards,
    Jim
    And not only that, but copying it to a BufferedImage which has been defined as RGB, not ARGB...hence the loss of transparency.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Problem with drawImage
    By matevos in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2012, 11:44 PM
  2. Graphics.drawImage() question
    By sakratt in forum AWT / Swing
    Replies: 0
    Last Post: 03-28-2012, 11:52 PM
  3. NullPointerException on Graphics.drawImage
    By Jaeela in forum New To Java
    Replies: 5
    Last Post: 10-05-2011, 03:36 AM
  4. DrawImage with transparency ?
    By mrhid6 in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2011, 12:58 AM
  5. drawImage im 13 plz help
    By pseudohawtcakes in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 09:07 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
  •