Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Image icon issue  RSS feed

 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my application using the browse button i load the image.
I am creating an instance of ImageIcon by giving the above specific path in the constructor.

"ImageIcon icon1 = new ImageIcon(selectedFile.getAbsolutePath()); "

Then i use this icon instance to paint in a JPanel embeded in the scrollpane.
In the paintComponent method of JPanel I get the width, height of the image icon and set the preferred size
of the panel to display the full image.

Now because of some specific requirement in the project i resize(enlarged/reduce) the same image and try to reload the image through the browse button.
And this resized image should be painted in the JPanel.

How to resize the image : Open in paint and enlarge or reduce the size of the image.


But the image size does not changes in the panel.

But if i close the application and run it again, then the new height & width is taken.

I have gone through the constructor of ImageIcon, it says in the loadImage() method media trakcer already
loaded the image.
I think because of this it is not taking the new resized image.

Can anybody give me a solution to this problem.

I am attaching sample code for this.






import java.awt.image.BufferedImage;
import javax.swing.JFileChooser;
import java.applet.*;
import java.io.File;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.filechooser.FileFilter;

public class ImageSizeCheckFrame extends javax.swing.JFrame {
File selectedFile ;
int counter = 0;
ImageIcon icon;
/** Creates new form ImageSizeCheckFrame */
public ImageSizeCheckFrame() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
prevImgPathLBL = new javax.swing.JLabel();
prevImgWidthLBL = new javax.swing.JLabel();
prevImgHeightLBL = new javax.swing.JLabel();
currImgPathLBL = new javax.swing.JLabel();
currImgWidthLBL = new javax.swing.JLabel();
currImgHeightLBL = new javax.swing.JLabel();
prevImgPathTXT = new javax.swing.JTextField();
prevImgWidthTXT = new javax.swing.JTextField();
prevImgHeightTXT = new javax.swing.JTextField();
currImgPathTXT = new javax.swing.JTextField();
currImgWidthTXT = new javax.swing.JTextField();
currImgHeightTXT = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("Browse");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Calculate");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

prevImgPathLBL.setText("Previous Image path:");

prevImgWidthLBL.setText("Previous Iamge width:");

prevImgHeightLBL.setText("Previous Image height:");

currImgPathLBL.setText("Current Image path:");

currImgWidthLBL.setText("Current Iamge width:");

currImgHeightLBL.setText("Current Image height:");

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(48, 48, 48)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jButton2)
.add(layout.createSequentialGroup()
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(27, 27, 27)
.add(jButton1))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(prevImgPathLBL)
.add(prevImgWidthLBL)
.add(prevImgHeightLBL)
.add(currImgPathLBL)
.add(currImgWidthLBL)
.add(currImgHeightLBL))
.add(30, 30, 30)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(prevImgWidthTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(prevImgPathTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(prevImgHeightTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(currImgPathTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(currImgWidthTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
.add(currImgHeightTXT, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton1))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createSequentialGroup()
.add(jButton2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(prevImgPathLBL))
.add(prevImgPathTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(prevImgWidthLBL)
.add(prevImgWidthTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(7, 7, 7)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(prevImgHeightLBL)
.add(prevImgHeightTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(49, 49, 49)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(currImgPathLBL)
.add(currImgPathTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(currImgWidthLBL)
.add(currImgWidthTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(7, 7, 7)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(currImgHeightLBL)
.add(currImgHeightTXT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(47, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(counter > 0){
prevImgPathTXT.setText(selectedFile.getAbsolutePath());
icon = new ImageIcon(selectedFile.getAbsolutePath());
prevImgWidthTXT.setText(icon.getIconWidth()+"");
prevImgHeightTXT.setText(icon.getIconHeight()+"");
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
ImageFileFilter filter = new ImageFileFilter();
fileChooser.addChoosableFileFilter(filter);
int retVal = fileChooser.showOpenDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION) {
jTextField1.setText(fileChooser.getSelectedFile().getAbsolutePath());
selectedFile = fileChooser.getSelectedFile();
}
counter++;
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
currImgPathTXT.setText(selectedFile.getAbsolutePath());

ImageIcon icon1 = new ImageIcon(selectedFile.getAbsolutePath());

currImgWidthTXT.setText(icon1.getIconWidth()+"");
currImgHeightTXT.setText(icon1.getIconHeight()+"");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
ImageSizeCheckFrame obj = new ImageSizeCheckFrame();
obj.setSize(700, 400);
obj.setVisible(true);

}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel currImgHeightLBL;
private javax.swing.JTextField currImgHeightTXT;
private javax.swing.JLabel currImgPathLBL;
private javax.swing.JTextField currImgPathTXT;
private javax.swing.JLabel currImgWidthLBL;
private javax.swing.JTextField currImgWidthTXT;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel prevImgHeightLBL;
private javax.swing.JTextField prevImgHeightTXT;
private javax.swing.JLabel prevImgPathLBL;
private javax.swing.JTextField prevImgPathTXT;
private javax.swing.JLabel prevImgWidthLBL;
private javax.swing.JTextField prevImgWidthTXT;
// End of variables declaration

}


class ImageFileFilter extends FileFilter{
private String description = "*.gif,*.jpg,*.png,*.jpeg";
private ArrayList list = new ArrayList();
{
list.add(".gif");
list.add(".jpg");
// anurag start
// don't show bmp files for selection
//list.add(".bmp");
list.add(".jpeg");
list.add(".png");
//list.add(".JPG");
//list.add(".JPEG");
// anurag end
}
public String getDescription(){
return description;
}

public boolean accept(File file){
if(file.isDirectory()) return true;
String fileName = file.getName().toLowerCase();
for(int i=0;i<list.size();i++){
String n = (String)list.get(i);
if(fileName.endsWith(n)) return true;
}
return false;
}
}
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags. It will make your code so much easier to read.

I'm moving this thread to the Swing forum.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code only formated. But this does not solve my problem.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satya sahu:
The code only formated. But this does not solve my problem.


Yes. But it does solve the problem for the rest of us as it makes the code readable.
After 80+ posts you should know by now that you should be using the code tags.
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i will take care of this from next time.
But please help me to get the solution.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) You really dont need to resize and generate multiple images. Start with a high resolution image to start with.
2) Override the paintComponent of the component you are using to draw your image on.
3) Check out the Graphics#drawImage method. You can specify the width and height of the image. So if you specify the width and height as the width and height of your component, the image will always be drawn on all the available area, filling it fully.
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the resizing of the image is done for some particular purpose which is done externally, may be using the MS paint. In the application the same image with its previous size is displayed. I know its size is changed. So i browse to the same path and load the same resized image.
The code executed is
ImageIcon icon = new ImageIcon(selectedFile.getAbsolutePath());
mapPanel.repaint();
In paintComponet(Graphics g2D){
imageWidth = icon.getIconWidth();
imageHeight = icon.getIconHeight();
g2D.drawImage(icon.getImage(), 0, 0, (int)imageWidth, (int)imageHeight, this);
}
The paintComponent uses the image's height and width to paint it. And here this fails to get the changed size. It paints the initial size. If i close the application and runs it again, it loads the new sized image. But reopening the client is not a solution.
So how do i solve it ?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You loaded the image.
It was pained.
You resized the image externally.
Now you need to reload the image as the image which you have is the old one.
Once the new image is reloaded, you need to repaint.
That is how you can solve it.

...and this is how you can use the code tags.
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code suggested,i am already doing that. After reloading the image, it is expected to take the new size(height and width) of the image, but it still takes the old size. I have put prints in the paint and i have confirmed this.
It still prints the old sizes. Only if i close the application and restart it again, it will print the new size.

It is failing thats why i am asking for the solution.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ImageIcon caches loaded images, so it will not reload an image with the same url / file reference.

For reloading an image file which is modified while your program is running, use ImageIO.read which returns a BufferedImage that you can pass to the constructor of ImageIcon.

In your case, you can probably use the BufferedImage directly.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!