0

I am quite new to java from c# background. I am following this tutorial to add an image to my Project and display in a label. In c# we use Picture-box and set the image property, In c# i can also re-size the image in a picture-box at design time by resizing the picture-box.

I have followed the tutorial and added the image to a label but the problem now is that the image is quite big and i want to re-size. I have tried resizing the label but i the image doesn't compress or re-size.

What am i supposed to do to re-size the image?

EDIT :

jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/musiconweb/resources/Music-icon.png"))); // NOI18N

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(40, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(58, 58, 58)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(29, Short.MAX_VALUE))
    );

    pack();
  • There are a number of significant issues. `JLabel` does not provide automatic scaling of an image and providing that functionality to a label is actually problematic, given the overall functionality of the component. If you require a scalable image component, you are most likely going to have make your own. Check out [this example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [this example](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) – MadProgrammer Oct 05 '13 at 00:13
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 05 '13 at 09:12

1 Answers1

1

Try the following:

Change

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/musiconweb/resources/Music-icon.png")));

TO

BufferedImage img = null;
try {
    img = ImageIO.read(new File("/org/me/musiconweb/resources/Music-icon.png"));
} catch (IOException e) {
    e.printStackTrace();
}
BufferedImage dimg = img.getScaledInstance(label.width, label.height,
            Image.SCALE_SMOOTH);

jLabel1.setIcon(new javax.swing.ImageIcon(dimg));

Hope that works. :)

Also, please take into consideration what @MadProgrammer discussed about not using getScaledImage. Though, if I were in your shoes, I would do incremental phases, first trying this out and if this works, go ahead and use the Graphic.scaleImage method.

For more info follow Resize a picture to fit a JLabel

Community
  • 1
  • 1
Neeraj
  • 8,408
  • 8
  • 41
  • 69
  • 1
    You want to take a look at [The Perils of Image.getScaledInstance()](https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html) and make considerations about maintaining the aspect ratio of the image as well - IMHO – MadProgrammer Oct 05 '13 at 00:10
  • I dont undertstand how to use it. What is img? –  Oct 05 '13 at 00:12
  • @PreciousTijesunimi Can you add the exact code you are using to the question. – Neeraj Oct 05 '13 at 00:17
  • @PreciousTijesunimi `img` is most likely a reference to some kind of `java.awt.Image`. You will have needed to create one in order to create an `ImageIcon` which can then be applied to `JLabel` – MadProgrammer Oct 05 '13 at 00:18
  • @MadProgrammer From a quick look of the tutorial I can see that the image source was taken inline jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/myimageapp/newpackage/image.png"))); – Neeraj Oct 05 '13 at 00:20
  • 1
    @Neeraj From the looks of the source I would say the OP is using the Netbeans form designer... – MadProgrammer Oct 05 '13 at 00:24
  • @Neeraj The OP won't be able to make the code changes, the code is a protected block, preventing them from editing the code... – MadProgrammer Oct 05 '13 at 00:33
  • @PreciousTijesunimi I think the editable problem is resolvable. Check this discussion out http://stackoverflow.com/questions/7284960/netbeans-code-gen-woes-how-to-edit-auto-generated-code – Neeraj Oct 05 '13 at 00:35
  • @neeraj That's not a solution, it's a hack and one that could lead to the OP not being able to open the file in the form designer. It's dangerous and bad advice – MadProgrammer Oct 05 '13 at 19:31
  • @MadProgrammer There are some other solutions too. – Neeraj Oct 05 '13 at 22:30
  • @neeraj Trying to modify the protected blocks isn't a solution. As a developer uses these forms I can tell you just what a mess this can make iPod your project to the point of of not working any more. Best to leave them alone - IMHO, instead, interact with components out side of these blocks as normal – MadProgrammer Oct 06 '13 at 02:19