Image Viewer needs little modification

Hi guys ,

say I am writing a simple image viewer code , which can view image iether by specifiying it’s URL, like that :

Image img = def.getImage(url);

or the other way which I post directly as I see it in the book :

Toolkit def = Toolkit.getDefaultToolkit();
Image img = def.getImage(“MyFile.dat”);

in the second form of getting the image , it is mentioned statically to get the file: MyFile.dat ,

how to make it dinamically ? I mean … inside these getImage function brackets , instead of mentioning the file name so clear , putting a function that will listen to the user choice from some directory and so . how to make it like opening a directory from a menu in the titlebar and choose the file and simply click and choose the picture and it is loaded .

I know some people will replay : Read a book :slight_smile: actually I did , and I apply the same code in the book and yet the program compiled
so fine and works great , yet it doesn’t show the image on the screen .

any help would be greatly appriciated …

newbie-java-developer

http://www.java2s.com/Code/Java/2D-Graphics-GUI/ImageViewer.htm

It uses ImageIcon and a JLabel to be as easy as possible. If you want to do more (like manipulating the image), you have to do it in other ways, probably using ImageIO and Graphics/Graphics2D. See http://www.oracle.com/technetwork/java/painting-140037.html and http://download.oracle.com/javase/tutorial/2d/images/index.html.

But you probably want to start with http://download.oracle.com/javase/tutorial/uiswing/index.html first.

hi cylab ,

that image viewer code from java2s.com website

http://www.java2s.com/Code/Java/2D-Graphics-GUI/ImageViewer.htm

is the exact code I am trying to make it view my image :slight_smile:

it compile and run so fine , but does not view any image I open .

Any error message? What kind of Image are you trying to load? Does it work, if you try to load it directly (put the name inside the code)?

no error message, and that’s what confusing me ???

i am trying to view both gif and jpg images.

Post the complete code you have and your systems’ spec.

Wait, I think he’s going to make his program to open file dynamically and put it on viewer, passing either File or String filename to getImage() method right? then

try to use JFileChooser, it has method to return File object represent the chosen file (in this case, image) to use by your getImage() method.

Hi guys, sorry for late replay :slight_smile:

cylab the code is the same in the page you mentioned early in your replay , that is :

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class ImageViewer extends JFrame implements ActionListener {
public ImageViewer() {
setTitle(“ImageViewer”);
setSize(300, 400);

JMenuBar mbar = new JMenuBar();
JMenu m = new JMenu("File");
openItem = new JMenuItem("Open");
openItem.addActionListener(this);
m.add(openItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
m.add(exitItem);
mbar.add(m);
setJMenuBar(mbar);

label = new JLabel();
Container contentPane = getContentPane();
contentPane.add(label, "Center");

}

public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == openItem) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));

  chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
    public boolean accept(File f) {
      return f.getName().toLowerCase().endsWith(".gif")
          || f.isDirectory();
    }

    public String getDescription() {
      return "GIF Images";
    }
  });

  int r = chooser.showOpenDialog(this);
  if (r == JFileChooser.APPROVE_OPTION) {
    String name = chooser.getSelectedFile().getName();
    label.setIcon(new ImageIcon(name));
  }
} else if (source == exitItem)
  System.exit(0);

}

public static void main(String[] args) {
JFrame frame = new ImageViewer();
frame.show();
}

private JLabel label;

private JMenuItem openItem;

private JMenuItem exitItem;
}

and yes ReBirth , the code already hve the JFileChooser method , I was considering getImage() as an alternative way because the above code didn’t work .

cylab I don’t think my system specifications effect, because … i am trying it on three different systems , both in software and hardware specifictions ( 2 of them are windows and one is linux)

now I am trying to compile this source code in my eclipse compiler ( i use both netbeans and eclipse , but chose eclipse here because the website mentioned so :

http://www.eclipse.org/articles/Article-Image-Viewer/imageviewer.zip

when I tryed to compile it , I got around 100 erors :expressionless: … most of type: unable to resolve type .

I kept searching for solution and it mentioned that you need to update a class path , I didn’t know how to do it … I didn’t find any … straight ahead precidure .

I’m really newbie :slight_smile:

one other very usefull example mentioned in that eclipse website is “image analyzer” source code zipped file , but didn’t know how to download it , it is mentioned there , but no link that I could find

thanks guys your help is really appriciated .

First use code tags to highlight your code. [ code ] [ /code ] without the spaces.
You are just not using the right method from the File class. getSelectedFile() return an object of class File. getName() is returning null. getAbsolutePath() will give you the full path, which is what the constructor for ImageIcon needs.


String name = chooser.getSelectedFile().getName();


String name = chooser.getSelectedFile().getAbsolutePath();

Umm

thanks CaptainJester .

so the first code line is for choosing an image via dialog box , and the second code line is for loading image via string , right ??

where exactly should I decare the “name” , and where should I use it ?

one more question thank you, it might seem silly but , is “name” here represent a variable , object, or a method ??

thnaks again for help :slight_smile:

newbie-java-developer

He was refering to a line in the example source you posted, which simply has a bug. Take another look at the code, find the line with (…).getName() and change it to (…).getAbsolutePath().

Yes I got it now , thank you all guys for your help :slight_smile:

newbie-java-developer

Swing has a file selector window, is that what you need? That would just output a File for you to work with.

I’ll check that too …

thanks Mads

newbie-java-developer