Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Window with image

Thread view: 
Cricca - 17 Feb 2005 16:14 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).

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.