aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Rotating image in JLabel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Rotating image in JLabel" Watch "Rotating image in JLabel" New topic
Author

Rotating image in JLabel

Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

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

Joined: Oct 27, 2005
Posts: 19723
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

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

Joined: Jun 13, 2009
Posts: 2203
    
    7
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
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

HowToAskQuestionsOnJavaRanch
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

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

Joined: May 03, 2008
Posts: 4642
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

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

Joined: May 03, 2008
Posts: 4642
    
    5

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

Joined: Oct 01, 2011
Posts: 41

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

Joined: Oct 27, 2005
Posts: 19723
    
  20

BufferedImage extends Image.
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

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

Joined: Oct 27, 2005
Posts: 19723
    
  20

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

Joined: May 03, 2008
Posts: 4642
    
    5

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

Joined: Oct 01, 2011
Posts: 41

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.
 
 
subject: Rotating image in JLabel