🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
3 messages
3 total messages Started by Melanie Miller Tue, 02 Mar 2004 20:51
Send help: JBuilder 9: display .jpg file with JBuilder using components.
#1260
Author: Melanie Miller
Date: Tue, 02 Mar 2004 20:51
24 lines
1865 bytes
--------------------------------------------------------------------------
      Hi there,
      I'm new to JBuilder 9 and have had school experience with using JAVA.

      I am trying to display a .jpg file.

      I already have loaded it with:
      -------------------------
      Image image = null;
      // Read from a file
      File file = new File("C:\\FILES\\martian_girlfriend.jpg");
      image = ImageIO.read(file);
      -------------------------- 

      How do I now display the loaded image? I already have gotten it to
work
      and load in a separate frame, but I am looking to load it into another
      SWING component like JScollPane or other. This might not be possible
      since I'm such a Newbie and do not know the full power of Swing.

      So please send help.
      Thanks,
      Melanie.
Re: Send help: JBuilder 9: display .jpg file with JBuilder using components.
#1261
Author: John McGrath [TeamB]
Date: Wed, 03 Mar 2004 09:13
34 lines
2039 bytes
On 3/2/2004 at 3:51:23 PM, Melanie Miller wrote:

> I am trying to display a .jpg file.
> 
> I already have loaded it with:
> -------------------------
> Image image = null;
> // Read from a file
> File file = new File("C:\\FILES\\martian_girlfriend.jpg");
> image = ImageIO.read(file);
> -------------------------- 
> 
> How do I now display the loaded image? 

The easiest way to display an image is using a JLabel.  You could
certainly use the code above to load the image, but there is an easier
approach using the ImageIcon class:

   Icon icon = new ImageIcon( "C:\\FILES\\martian_girlfriend.jpg" );
   JLabel label = new JLabel( icon );

Note that this assumes that you want to load the image from a *file*.
If the image is a static part of your application, you will probably
want to store the image along with your code (typically in a jar file),
and then create the Icon using the ImageIcon(URL) constructor.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: Send help: JBuilder 9: display .jpg file with JBuilder using components.
#1263
Author: Melanie Miller
Date: Wed, 03 Mar 2004 16:35
68 lines
3118 bytes
Yup.

Got it work late last night.
Here's the code for the other newsbies:

 void jButton1_actionPerformed(ActionEvent e)
  {
    Image image = null;

    ImageIcon icon=new ImageIcon
        (Toolkit.getDefaultToolkit().createImage("C:\\FILES\\finger1.jpg"));

    jLabel1.setIcon(icon);
    this.repaint();

   Image image1 = null;
   try {
      // Read from a file
      File file = new File("C:\\FILES\\martian_girlfriend.jpg");
      image1 = ImageIO.read(file);
   } catch (IOException ee) {
  }

  // Use a label to display the image
  JFrame frame = new JFrame();
  JLabel label = new JLabel(new ImageIcon(image1));
  frame.getContentPane().add(label, BorderLayout.CENTER);
  frame.pack();
  frame.setVisible(true);
  }
}

"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message
news:4045a1d1@newsgroups.borland.com...
> On 3/2/2004 at 3:51:23 PM, Melanie Miller wrote:
>
> > I am trying to display a .jpg file.
> >
> > I already have loaded it with:
> > -------------------------
> > Image image = null;
> > // Read from a file
> > File file = new File("C:\\FILES\\martian_girlfriend.jpg");
> > image = ImageIO.read(file);
> > -------------------------- 
> >
> > How do I now display the loaded image?
>
> The easiest way to display an image is using a JLabel.  You could
> certainly use the code above to load the image, but there is an easier
> approach using the ImageIcon class:
>
>    Icon icon = new ImageIcon( "C:\\FILES\\martian_girlfriend.jpg" );
>    JLabel label = new JLabel( icon );
>
> Note that this assumes that you want to load the image from a *file*.
> If the image is a static part of your application, you will probably
> want to store the image along with your code (typically in a jar file),
> and then create the Icon using the ImageIcon(URL) constructor.
>
> -- 
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
>     http://www.JPMcGrath.net/newsgroups/e-mail.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads