Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why use a Buffered Image insead of an Image?

 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking at the code here which let's you drag an image around the screen:
http://www.java2s.com/Code/Java/2D-Graphics-GUI/Imagewithmousedragandmoveevent.htm

Why did they do this:

Why did they have to go through the trouble of making a bufferedimage and then converting it to a graphic and then drawing the image? Why not just instantiate a new Graphic2D object and use that to draw the image?
 
Campbell Ritchie
Marshal
Posts: 52632
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like something which would sit better on our GUIs forum. Moving discussion.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ofcourse I don't know why the author of that code wrote it that way; maybe that person thought that loading a GIF image with getToolkit().getImage(...) is the easiest way to do it. But it's just as easy with ImageIO, and it will directly return a BufferedImage:

No need to draw the image on a new BufferedImage.
 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, when would you use a BufferedImage instead of just an Image?
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anywhere you require to use API that is present in BufferedImage but not in Image.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!