Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-23-2008, 05:36 AM
Member
 
Join Date: Mar 2008
Posts: 3
Birkoff is on a distinguished road
How to display image ?
Hello everyone,

I just registered on these forums because it seems a decent place with people that know about stuff.

Before anyone asks: Yes, I did use the search engine and found some stuff (which I couldn't fully understand) related to my problem. Maybe it's because I am not sure what to search for.

Either way ... I am having some difficulties with a college project. In this project we're supposed to make a interface and use some image processing methods over it.
I am having some problems to build up an interface. I have NetBeans-5.5.1 installed and got the basics running but I don't seem able to load and show an image on the screen.
I am not even sure what component I should be using: JPanel, JFrame, JLabel, JInternalFrame, ... ? None of the above =S ?
And I am not sure on how to load the image as well.

Well, any help will be welcome. From tutorials, pdfs, plain text files, pretty much anything (even pointers in to what I should search). If it's gonna help me than I want it =P

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-23-2008, 08:50 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
Some resource links:
How to Use Icons
Trail: 2D Graphics
Code:
import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class LoadAndShow extends JPanel { BufferedImage image; Dimension size = new Dimension(); public LoadAndShow(BufferedImage image) { this.image = image; size.setSize(image.getWidth(), image.getHeight()); } /** * Drawing an image can allow for more * flexibility in processing/editing. */ protected void paintComponent(Graphics g) { // Center image in this component. int x = (getWidth() - size.width)/2; int y = (getHeight() - size.height)/2; g.drawImage(image, x, y, this); } public Dimension getPreferredSize() { return size; } public static void main(String[] args) throws IOException { String path = "images/hawk.jpg"; BufferedImage image = ImageIO.read(new File(path)); LoadAndShow test = new LoadAndShow(image); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new JScrollPane(test)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); //showIcon(image); } /** * Easy way to show an image: load it into a JLabel * and add the label to a container in your gui. */ private static void showIcon(BufferedImage image) { ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon, JLabel.CENTER); JOptionPane.showMessageDialog(null, label, "icon", -1); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-23-2008, 11:10 PM
Member
 
Join Date: Mar 2008
Posts: 3
Birkoff is on a distinguished road
Thanks for the reply.
It took me some time to read all the stuff you posted but I am finally getting somewhere.

I am having a problem using JScrollPane and would like to know if anyone around can help me with it.

Here's how I am drawing the image:
Code:
private void jButton1MouseReleased(java.awt.event.MouseEvent evt) { String path = "/home/birkoff/wallpapers/rei_forever.jpg"; Graphics g = jScrollPane1.getGraphics(); Graphics2D g2D = (Graphics2D) g; try { BufferedImage image = ImageIO.read(new File(path)); ImageIcon icon = new ImageIcon(image); g2D.drawImage(image, (jScrollPane1.getWidth() - image.getWidth())/2, (jScrollPane1.getHeight() - image.getHeight())/2, this); jScrollPane1.paintComponents(g2D); } catch ( Exception e ) { } finally { }
I don't know why but this only draw the border of the image (if I set the jScrollPane1's border to no-boarder then no image is draw at all).

So can anyone please point me to my error ?

Also, if anyone knows how to resize the image so it fits window (or if you know how to enable the scrolls on JScrollPane) please gimme a hand.

There were some code for this on one of the links the previous user posted but I couldn't understand them.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-24-2008, 12:05 AM
Member
 
Join Date: Mar 2008
Posts: 3
Birkoff is on a distinguished road
I just managed to put some scrollbars on the jScrollPane1 but I still can't get it to show the image.

Here's the new code:
Code:
private void jButton1MouseReleased(java.awt.event.MouseEvent evt) { String path = "/home/birkoff/wallpapers/rei_forever.jpg"; Graphics g = jScrollPane1.getGraphics(); Graphics2D g2D = (Graphics2D) g; try { BufferedImage image = ImageIO.read(new File(path)); ImageIcon icon = new ImageIcon(image); Scrollbar columnView = new Scrollbar(Scrollbar.HORIZONTAL); Scrollbar rowView = new Scrollbar(Scrollbar.VERTICAL); jScrollPane1.setColumnHeaderView(columnView); jScrollPane1.setRowHeaderView(rowView); jScrollPane1.setBackground(Color.BLACK); g2D.drawImage(image, (jScrollPane1.getWidth() - image.getWidth())/2, (jScrollPane1.getHeight() - image.getHeight())/2, this); g2D.setPaint(Color.BLACK); jScrollPane1.setOpaque(true); jScrollPane1.paintComponents(g2D); } catch ( Exception e ) { } finally { }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-24-2008, 04:14 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
Code:
private void jButton1MouseReleased(MouseEvent evt) { String path = "/home/birkoff/wallpapers/rei_forever.jpg"; // Using getGraphics is not the way to draw. // Your graphics will not persist with this method. // Use a proper JPanel and override the paintComponent // method as shown above. Graphics g = jScrollPane1.getGraphics(); Graphics2D g2D = (Graphics2D) g; // It is unwise to load images like this. You should load // them at startup and save a reference to them in a variable // as shown above. try { BufferedImage image = ImageIO.read(new File(path)); ImageIcon icon = new ImageIcon(image); // Scrollbar is an AWT component. It is best to not mix // AWT heavyweight components with Swing lightweight // components untill you've had more experience. Scrollbar columnView = new Scrollbar(Scrollbar.HORIZONTAL); Scrollbar rowView = new Scrollbar(Scrollbar.VERTICAL); // ColumnHeader and RowHeader are special component areas // in a JScrollPane. They are not designed to accept // scrollbars. See JScrollPane api for details and the // tutorial page shown below. jScrollPane1.setColumnHeaderView(columnView); jScrollPane1.setRowHeaderView(rowView); // The viewPort is a better choice for background colors. jScrollPane1.setBackground(Color.BLACK); g2D.drawImage(image, (jScrollPane1.getWidth() - image.getWidth())/2, (jScrollPane1.getHeight() - image.getHeight())/2, this); g2D.setPaint(Color.BLACK); // JScrollPane is opaque by default. jScrollPane1.setOpaque(true); // Instead of calling this method we override it in // a JComponent such as JPanel. The example below // shows how to set up a graphic component that does // all the work for you. jScrollPane1.paintComponents(g2D); } catch ( Exception e ) { } finally { } }
It's okay to show an image in event code such as in the mouseReleased method but try to avoid loading an image in event code. You only need to load an image one time. Loading is best done at construction or via a JFileChooser.
For more about JScrollPane see How to Use Scroll Panes.
Code:
import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class AnotherGo { private JScrollPane getContent() { // This image/graphics component knows // how to draw itself and how to report // its display size needs/requirements to // its parent scrollPane/container. AnImagePanel panel = new AnImagePanel(); JScrollPane scrollPane = new JScrollPane(panel); // If you want to configure/customize this // scrollPane this is the place to do so. return scrollPane; } public static void main(String[] args) { AnotherGo test = new AnotherGo(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getContent()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class AnImagePanel extends JPanel { BufferedImage image; Dimension size = new Dimension(); public AnImagePanel() { image = loadImage(); size.setSize(image.getWidth(), image.getHeight()); } protected void paintComponent(Graphics g) { // Center image in this component. int x = (getWidth() - size.width)/2; int y = (getHeight() - size.height)/2; g.drawImage(image, x, y, this); } /** * This method handles the communication of * size requirements with the parent JScrollPane. */ public Dimension getPreferredSize() { return size; } private BufferedImage loadImage() { String path = "images/hawk.jpg"; BufferedImage image = null; try { image = ImageIO.read(new File(path)); } catch(IOException e) { // May as well use what is given... System.out.println("read error:" + e.getMessage()); } return image; } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-09-2008, 01:41 AM
Member
 
Join Date: Jun 2008
Posts: 2
bridleman4 is on a distinguished road
Help!!! Please...
Hi everyone,

I'm new to these forums and this thread is exactly what I have been needing help on! I'm trying to display .png image files on a JPanel. My images are stored in the same directory as the source files. They are images of playing cards and are labeled "1.png", "2.png", etc. I have tried using the ImageIO process used above, the ClassLoader.getResource() method, and the ImageIcon classes, and none of them are working for me. Here is my code:

public void paintImage(Graphics g, Point p, String filename) {
BufferedImage i = null;
try{i = ImageIO.read(new File(filename);}
catch(IOException e) {e.printStackTrace();}
//g.drawString(filename, p.x, p.y + 10);
if(i!=null) {
g.drawImage(i, p.x, p.y, _gameBoard);
}
else System.out.println("i not loaded properly.");
}

It's the image-printing method for my panel, where _gameBoard is the JPanel I am trying to display the image on. Why is the program not reading the file correctly? Thanks.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-09-2008, 05:39 AM
Senior Member
 
Join Date: Jun 2008
Posts: 194
Fubarable is on a distinguished road
Do you know that your filename is in fact correct, that it has all the correct path information? Or if not files, have you tried loading the images as class resources?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-09-2008, 08:58 AM
Member
 
Join Date: Jun 2008
Posts: 2
bridleman4 is on a distinguished road
Thanks for the quick response! I looked into getting class resources. This fixed my problem. The code i used is:

i = getToolkit().getImage(getClass().getResource("1.pn g"));

inside my JApplet. Thanks again!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display image in jsp page(Struts) rama6262 Advanced Java 1 12-21-2007 08:50 AM
can display image in JFrame? xCLARAx AWT / Swing 4 07-26-2007 04:33 PM
How to display an image in JApplet fred Java Applets 1 07-24-2007 03:02 AM
How to display a database-image in a page? simon New To Java 1 07-24-2007 12:56 AM
Simplest way to read and display a jpeg image Hasan New To Java 1 05-31-2007 04:42 PM


All times are GMT +3. The time now is 08:35 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org