Hi all,
I've been trying to paint an image to a JPanel with no success. Could someone help me figure this out?
mport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Vector;
import java.awt.image.*;
public class PhoneInterface1 extends JFrame implements ActionListener
{
private JPanel imagePan = new JPanel();
private JPanel numberPad = new JPanel();
private JTextArea text = new JTextArea();
private JDesktopPane desktop;
private JButton loadButton, closeButton;
private JLabel status;
private Vector theFrames;
private JToolBar theToolBar;
//public JScrollBar scrollBar = new JScrollBar();
public PhoneInterface1()
{....................
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(loadButton))
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File f = chooser.getSelectedFile();
ImageLoader imgldr = new ImageLoader(f);
//PROBLEM HERE!!??
imagePanel img = new imagePanel(imgldr); //???
this.setStatus("Loading Image "+f.getName());
}
}
class ImageLoader extends JFrame
{
private String filename;
private String directory;
private Image theImage;
private int width, height;
private long fileLength;
public ImageLoader(File f)
{
filename = f.getName();
directory = f.getPath();
fileLength = f.length();
this.theImage = this.getToolkit().getImage(directory);
JScrollPane scroll = new JScrollPane(new JLabel(new ImageIcon(this.theImage)));
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add("North", new JLabel(filename));
this.getContentPane().add("Center",scroll);
width = this.theImage.getWidth(null);
height = this.theImage.getHeight(null);
//String details = new String(" Details: Image Size("+width+","+height+") File Size "+fileLength+" bytes");
//this.getContentPane().add("North", new JLabel(details));
this.setSize(300,300);
//this.show();
}
class imagePanel extends JPanel
{ BufferedImage image;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(image != null)
{
g.drawImage(image, 0, 0, this);
}
}
}
I created the class imagePanel to draw an image to a panel. I load an image from file using the ImageLoader class and I want to draw it on the JPanel "imagePan" in the PhoneInterface1 class. I'm having trouble going from an ImageLoader object to drawing the image on imagePan in the PhoneInterface1 class. The problem area is marked by "//PROBLEM HERE!!??" in the code above.
Apologies in advance if this post seems a bit messy, but an expert advice would be grreatly appreciated!