The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
Please vote in the Forum Structure Poll. Polling will close at 2PM EST on January 21, 2025.
Java: Creating an image object (to view images)
Seguerof the VoidSydney, AustraliaRegistered Userregular
I'm trying to put something into my application that will show images (jpgs, gifs, etc). I've taken a look at the API and a few tutorials that mention Image, BufferedImage, etc, but can't get any to work as they should. I also need to be able to resize an image bigger than 310x310 so that the longest side is 310 (resize in ratio). What classes should I be looking at to achieve this?
Thanks.
EDIT: I'm using a GridBagLayout to position the JPanel this image object will reside in. I was previously just using a JLabel and setIcon, but that doesn't let me define a maximum size/etc.
For resizing, just calculate the ratio of w/h, calculate which is larger, and multiply by the correct factor.
Janin on
[SIGPIC][/SIGPIC]
0
Seguerof the VoidSydney, AustraliaRegistered Userregular
edited April 2007
Thanks I'll take a look.
EDIT: I don't know how to go about actually drawing the image. Graphics is abstract so I can't new Graphics();, and drawImage is static, so I can't call it with Graphics.drawImage (like Math.something). How do I actually draw the image?
If you want to draw on a Component once, you can use the getGraphics() method of that particular component to draw on it.
You could make a new class that extends JPanel and draws an Image within its overriden paint(Graphics g) method, or you can just set the minimumSize, prefferedSize or Size of your JLabel you're creating with an ImageIcon, one of them should work even with a FlowLayout.
.:Orion on
0
Seguerof the VoidSydney, AustraliaRegistered Userregular
edited April 2007
The second option there looks like what I need, as I already have a class (ImagePanel) that extends JPanel. How do I override the paint(Graphics g) method, and how can I change what this paints throughout the program? ie. I need to paint more than once, with different images.
all you have to do is define a method as such in your ImagePanel class :
public void paint(Graphics g){
//super.paint(g); --- you might want this, draws the background and other componen
}
What you could do to change the image is to just have an Image as a property of your class, that you can set to something else.
paint(Graphics g) is called whenever the component needs to be refreshed, after a resizing of the window and stuff like that. You can force a paint by calling repaint() on your class.
.:Orion on
0
Seguerof the VoidSydney, AustraliaRegistered Userregular
edited April 2007
Ah I see, thanks. I'll give that a shot.
EDIT: It works, thanks! Took me a bit to get around the logic to resize properly but that's done too
Posts
For resizing, just calculate the ratio of w/h, calculate which is larger, and multiply by the correct factor.
EDIT: I don't know how to go about actually drawing the image. Graphics is abstract so I can't new Graphics();, and drawImage is static, so I can't call it with Graphics.drawImage (like Math.something). How do I actually draw the image?
You could make a new class that extends JPanel and draws an Image within its overriden paint(Graphics g) method, or you can just set the minimumSize, prefferedSize or Size of your JLabel you're creating with an ImageIcon, one of them should work even with a FlowLayout.
public void paint(Graphics g){
//super.paint(g); --- you might want this, draws the background and other componen
}
What you could do to change the image is to just have an Image as a property of your class, that you can set to something else.
paint(Graphics g) is called whenever the component needs to be refreshed, after a resizing of the window and stuff like that. You can force a paint by calling repaint() on your class.
EDIT: It works, thanks! Took me a bit to get around the logic to resize properly but that's done too