Results 1 to 6 of 6
Thread: Image Dimensions in JFrame
- 03-29-2012, 01:11 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 20
- Rep Power
- 0
Image Dimensions in JFrame
Here is my code:
Where and what do I need to put to change the size of the image as it is over sized.Java Code:import java.awt.*; import javax.swing.*; public class ImageViewer extends JFrame { private ImageIcon image1; private JLabel label1; ImageViewer() { setLayout(new FlowLayout()); image1 = new ImageIcon(getClass().getResource("Snow.jpg")); label1 = new JLabel(image1); add(label1); } public static void main (String args[]) { ImageViewer gui = new ImageViewer(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setVisible(true); gui.pack(); gui.setTitle("Image Program"); } }
Thanks,
Ryan
- 03-29-2012, 01:42 PM #2
Re: Image Dimensions in JFrame
An idea:
Read the image file into an image object, Create a BufferedImage and get its Graphics context. Use the Graphics class's drawImage method to draw the image read from the file into the BufferedImage with the new size. Use the new image in the GUIIf you don't understand my response, don't ignore it, ask a question.
- 03-29-2012, 02:30 PM #3
Re: Image Dimensions in JFrame
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-29-2012, 02:35 PM #4
Re: Image Dimensions in JFrame
... or use getScaledInstance(...).
If you control the image, it might be better to create an appropriately sized version in a decent image editor.
If reducing image sizes for display is a recurring need, you could try extending ImageIcon and overriding the 3 Icon methods to do what you need. It's not difficult provided you do your background reading so you know exactly what you're going to do before you tackle the code.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-29-2012, 02:53 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 20
- Rep Power
- 0
Re: Image Dimensions in JFrame
Thanks for the replies. How do I go about writing a BufferedImage code? I've never written one before.
Also where on the code would I putgetScaledInstance()
- 03-29-2012, 03:25 PM #6
Similar Threads
-
Please explain to where container dimensions come from
By NoSwing in forum AWT / SwingReplies: 2Last Post: 03-13-2011, 04:29 AM -
JFrame + image
By Blacknight in forum New To JavaReplies: 2Last Post: 05-07-2009, 05:34 AM -
java applet viewer dimensions
By yuriythebest in forum Java AppletsReplies: 1Last Post: 11-08-2008, 10:44 PM -
How can i get image dimensions using java?
By Thilkumar82 in forum Advanced JavaReplies: 0Last Post: 09-02-2008, 09:12 AM -
how to set full screen dimensions
By valery in forum New To JavaReplies: 1Last Post: 08-03-2007, 06:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks