Hello !!!
I want create a simple program with a window (with the object Frame, non
Applet).
In this window I want load an image (gif or jpg).
Somebody con help me with an example ?
Thanks
Cricca
mcc@dcs.ed.ac.uk - 17 Feb 2005 22:56 GMT
Hi Cricca,
You might want to try the following. It'll handle different image types
and displays them without the window borders/buttons, you can change
the window attributes, but I think this is what you want.
Hope that helps,
Mark
// Begin code : -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
class ImageWindow extends JWindow{
public static void main(String[] args){
// get the image name from the user (assume it is named in the first
argument)
ImageWindow window = new ImageWindow(args[0]);
}
ImageWindow(String fileName){
ImageArea imageArea = new ImageArea(fileName);
// the following line adds the ImageArea to the windows content pane,
// to add complex objects, it is best to add a JPanel object to the
// content pane, and add all the other things to the JPanel. That way
you can control
// the positions of things easily.
JPanel panel = new JPanel(new BorderLayout());
panel.add(imageArea,BorderLayout.NORTH); // add the ImageArea to the
north of the panel
getContentPane().add(panel);
validate();
pack();
setLocationRelativeTo(null); // put it in the middle of the screen
setVisible(true); // show it to the world
}
private class ImageArea extends Component{
BufferedImage image;
ImageArea(String fileName){
try{
image = ImageIO.read(new File(fileName));
}catch(IOException e){
System.err.println("I could not get the image... sorry!");
System.exit(-1);
}
setSize(new Dimension(image.getWidth(),image.getHeight()));
}
// the following methods tell the size of the component to other
objects
public Rectangle getBounds(){
return getBounds(new Rectangle());
}
public Rectangle getBounds(Rectangle r){
r.setSize(new Dimension(image.getWidth(),image.getHeight()));
return r;
}
public Dimension getMinimumSize(){
return new Dimension(image.getWidth(),image.getHeight());
}
public Dimension getPreferredSize(){
return new Dimension(image.getWidth(),image.getHeight());
}
public void paint(Graphics g){
g.drawImage(image,0,0,null);
}
}
}
Fuddzy - 17 Feb 2005 23:27 GMT
> Hello !!!
>
> I want create a simple program with a window (with the object Frame, non
> Applet).
> In this window I want load an image (gif or jpg).
Cricca
try this
/* This class displays an image of 359 x 305 which is in the 'images'
subfolder folder
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
public class Display_Image extends JPanel {
BufferedImage image;
static int w = 700;
static int h = 600;
public Display_Image() {
try {
image = ImageIO.read(new File("images/drawing
hands359_305.jpg"));
}
catch (IOException ioe)
{
System.out.println(ioe);
System.exit(0);
}
setPreferredSize(new
Dimension(image.getWidth(),image.getHeight()));
}
public void paintComponent(Graphics g) {
/* using the same Paint method in Swing (as in awt) but due to
interaction with borders etc, it is better to override the
paintComponent() instead */
g.drawImage(image,125,75, this);
}
public static void main(String[] args) {
JFrame f = new JFrame("Displaying An Image");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(w,h);
f.getContentPane().add(new Display_Image());
f.setVisible(true);
}
}
Hope this helps
Fuddzie
Cricca - 21 Feb 2005 10:42 GMT
> Hope this helps
>
> Fuddzie
Wonderful
Thanks
Cricca