Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rotating image in JLabel

 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple application that I want to distribute to colleagues. However, I'm having a few problems.

The app loaded an image in to an IconImage and displayed it in a JLabel. However, it seems to me that the IconImage isn't that great for rotating. So, I started looking at BufferedImage and then loading that into something like a Graphics object. However, I'm getting stuck with the concepts of casting between these objects and wouldn't mind if someone could point me in the right direction for the most efficient way of doing it (which objects to load the image file in to and then how to cast it in to a JLabel friendly object).

I have a lot of code - not sure if it's useful here, so I've pasted it in to a paste bin for brevity. At the moment, this is throwing errors to do with a BufferedImage being passed to a setPane method which only accepts an Image object. You don't have to correct my code, but just point me in the right direction of what objects to load it into and how to cast them - thanks.

OpenImage.java - http://pastebin.com/gffZyTPv
OpenImagePanel.java - http://pastebin.com/XNbMS9P1

Thanks
Adam
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UseOneThreadPerQuestion. If you wanted to have your original thread in this forum you should have asked for it to be moved. Since this is a better location I will close the other one.
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks - I did search for a FAQ on etiquette (e.g. posting code etc.), but couldn't find one - the FAQ is mostly about java - is there a general link I should be looking at?
 
Rob Camick
Ranch Hand
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it seems to me that the IconImage isn't that great for rotating


What exactly do you mean by rotating?

Do you want it to continually rotate like an animated gif?

Do you want it to rotate when a button is clicked?

Maybe Rotated Icon will give you some ideas. You can replace the existing icon on the label with a newly rotated icon.
 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HowToAskQuestionsOnJavaRanch
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
it seems to me that the IconImage isn't that great for rotating


What exactly do you mean by rotating?

Do you want it to continually rotate like an animated gif?

Do you want it to rotate when a button is clicked?

Maybe Rotated Icon will give you some ideas. You can replace the existing icon on the label with a newly rotated icon.


I want to rotate the image through 90 degress - colleauges sometimes take digital photographs that need rotating before publishing to the web. I will have a look at rotated icon, as long as I can write it back out to a file, then it should do the job.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Cripps wrote:I want to rotate the image through 90 degress - colleauges sometimes take digital photographs that need rotating before publishing to the web. I will have a look at rotated icon, as long as I can write it back out to a file, then it should do the job.


If your concern is rotating a BufferedImage, consider using AffineTransformOp -- that's what it's for. Here's an example of its use.
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Adam Cripps wrote:I want to rotate the image through 90 degress - colleauges sometimes take digital photographs that need rotating before publishing to the web. I will have a look at rotated icon, as long as I can write it back out to a file, then it should do the job.


If your concern is rotating a BufferedImage, consider using AffineTransformOp -- that's what it's for. Here's an example of its use.


This was my preferred method of doing it - Rotating a BufferedImage - however, I found little resources on the web for putting a BufferedImage in a JPanel - or casting it to an ImageIcon so that I could do it via that method. If anyone knows how to do that, I would be very grateful.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found little resources on the web for putting a BufferedImage in a JPanel

There's an example on this very site, and at least a few dozen more on other sites. What search terms did you use?

or casting it to an ImageIcon

You don't cast a BufferedImage to an ImageIcon, you construct an ImageIcon, passing the BufferedImage to its constructor.
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
I found little resources on the web for putting a BufferedImage in a JPanel

There's an example on this very site, and at least a few dozen more on other sites. What search terms did you use?

I used "creating imageicon from bufferedimage" in google. I hadn't searches these forums, but will give it a go now.

or casting it to an ImageIcon

You don't cast a BufferedImage to an ImageIcon, you construct an ImageIcon, passing the BufferedImage to its constructor.



Ah, this last point makes sense, but it's one I didn't try as when I checked the api, I couldn't find a constructor that matches a BufferedImage - which one fits this scenario?

java.lang.ImageIcon
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BufferedImage extends Image.
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:BufferedImage extends Image.


Sorry to be clear, if two different classes extend from a super class, you can create a class from either one?

It seems to me that ImageIcon extends java.lang.Object - so how are the two related?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that any method or constructor that has Object as a parameter can take ImageIcon instances as arguments for that parameter. Likewise, any method or constructor that has Image as parameter (like one of ImageIcon's constructors) can take a BufferedImage as argument for that parameter.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, more generally, inheritance (extends) defines a is-a relationship. So, a BufferedImage is-a Image and can be used wherever Image is required.

Note that the opposite does not hold true; any arbitrary instance of Image may or may not be a BufferedImage.
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Or, more generally, inheritance (extends) defines a is-a relationship. So, a BufferedImage is-a Image and can be used wherever Image is required.

Note that the opposite does not hold true; any arbitrary instance of Image may or may not be a BufferedImage.


Thanks very much - this really furthers my understanding of java - and I've managed to get it working - not only putting the BufferedImage into a JLabel, but also rotating it - although now I have a new problem with rotating rectangular images.

Thanks again everyone.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic