The several drawImage() methods in java.awt. Graphics do "nothing if img is null As a result, setting the image to null is sufficient. You're clearing the background explicitly, but super.
PaintComponent(g) is an alternative that clears the panel to the background color.
The several drawImage() methods in java.awt. Graphics do "nothing if img is null. " As a result, setting the image to null is sufficient.
You're clearing the background explicitly, but super. PaintComponent(g) is an alternative that clears the panel to the background color. Addendum: You may also want to study the examples found in the articles How to Use File Choosers and Working with Images.
Addendum: I used a different layout and added the image to a JScrollPane. I also had setImage() return a result to let the Display know what happened. Addendum: This newer, simpler revision extends JFileChooser to handle approve and cancel directly.
Import java.awt. *; import java.awt.image. BufferedImage; import java.io.
*; import javax.imageio. ImageIO; import javax.swing. *; import javax.swing.filechooser.
FileNameExtensionFilter; /** @see http://stackoverflow.com/questions/4053090 */ public class ImageDisplay extends JFrame { private static final String title = "Select a file"; private MyImagePanel imagePanel = new MyImagePanel(); private JLabel result = new JLabel(title, JLabel. CENTER); private MyChooser fileChooser = new MyChooser(); public ImageDisplay(String name) { super(name); this. SetDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); this.addWidgets(); this.pack(); this. SetVisible(true); } private void addWidgets() { FileNameExtensionFilter filter = new FileNameExtensionFilter( "Images", "jpg", "JPG", "GIF", "gif", "JPEG", "png", "PNG"); fileChooser. SetFileFilter(filter); this.
Add(fileChooser, BorderLayout. WEST); this. Add(new JScrollPane(imagePanel), BorderLayout.
CENTER); this. Add(result, BorderLayout. SOUTH); } class MyChooser extends JFileChooser { @Override public void approveSelection() { File f = fileChooser.getSelectedFile(); if (imagePanel.
SetImage(f)) { result. SetText(f.getName()); } else { result. SetText(title); } } @Override public void cancelSelection() { imagePanel.
SetImage(null); result. SetText(title); } } class MyImagePanel extends JPanel { private BufferedImage bi; public MyImagePanel() { this. SetPreferredSize(new Dimension(500, 700)); } /** Return true if read() succeeded.
*/ public boolean setImage(File f) { try { bi = ImageIO. Read(f); } catch (Exception e) { bi = null; } if (bi! = null) { setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight())); } this.revalidate(); this.repaint(); return bi!
= null; } @Override public void paintComponent(Graphics g) { super. PaintComponent(g); g. DrawImage(bi, 0, 0, null); } } public static void main(String args) { EventQueue.
InvokeLater(new Runnable() { @Override public void run() { new ImageDisplay("Image Demo"). SetVisible(true); } }); } }.
I tried the following..1. Selected an image file->displays ok.. 2. Selected a directory ->the previous image file is cleared from jpanel and an error msg is displayed on text area..This will only happen onlyif display.
DisplayImage("") is called..I know this is a dirty way of doing it..Is there a better way of clearing the image? If I remove the above call ,then the setImage() of ImagePanel is never called when a directory is selected. – markjason72 Oct 30 '10 at 5:50 @markjason72: I've elaborated above.
– trashgod Oct 30 '10 at 19:29.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.