|
|
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.
|
|

03-23-2008, 05:36 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
|
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.
|
|

03-23-2008, 08:50 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,141
|
|
Some resource links:
How to Use Icons
Trail: 2D Graphics
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);
}
}
|
|

03-23-2008, 11:10 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
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:
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.
|
|

03-24-2008, 12:05 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
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:
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 {
}
|
|

03-24-2008, 04:14 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,141
|
|
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.
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;
}
}
|
|

06-09-2008, 01:41 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 2
|
|
|
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.
|
|

06-09-2008, 05:39 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 194
|
|
|
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?
|
|

06-09-2008, 08:58 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 2
|
|
|
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!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|