I'm working on an Image editor for a class project. I am stuck trying to display the image onto the panel with a button. My action listener is working fine. I don't know if the paint component is working right. help please.

Here is my code for an Image Viewer Panel:

package ltw_imageeditor_v1;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.image.*;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.Graphics;


/**
 *
 * @author Lawrence Herlinger
 */
public class ImageViewer extends JPanel
{
    private java.awt.Image image;
    private boolean stretched = true;
    private int xCoordinate = 0;
    private int yCoordinate = 0;


//Default no arg constructor
    public ImageViewer()
    {    
    }
//Constructor
    public ImageViewer(Image image)
    {
        this.image = image;
    }

//Get the image
    public java.awt.Image getImage()
    {
       return image;
    }
//set the image
    public void setImage(java.awt.Image image)
    {
        this.image = image;
        this.repaint();
    }
    //overriding the paint Component
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        if(image != null)
            if(isStretched())
                g.drawImage(image, xCoordinate, yCoordinate,
                        getSize().width, getSize().height, this);
            else
                g.drawImage(image, xCoordinate, yCoordinate, this);
    }

    public boolean isStretched()
    {
        return stretched;
    }

    public void setStretched(boolean stretched)
    {
        this.stretched = stretched;
        repaint();
    }

    public int getXCoordinate()
    {
        return xCoordinate;
    }

    public void setXCoordinate(int xCoordinate)
    {
        this.xCoordinate = xCoordinate;
    }

    public int getYCoordinate()
    {
        return yCoordinate;
    }

    public void setYCoordinate(int yCoordinate)
    {
        this.yCoordinate = yCoordinate;
    }

}

Here is the frame I built so far with the action listener in this class. It is a little messy with all the panels...

package ltw_imageeditor_v1;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.event.*;
import java.awt.Color;
import javax.swing.JFileChooser;


/**
 *
 * @author Lawrence Herlinger II
 */
public class IFrame extends JFrame
{

    private ImageIcon imageIcon = new ImageIcon();
    private Image image;   //imageIcon.getImage();
    //viewer.add(image);
    private ImageViewer viewer = new ImageViewer();



    public IFrame()
    {
        //Create Frame
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setTitle("LTW Editor");

        //Create Panels added to a Border Layout
        JPanel toolPanel = new JPanel();
        toolPanel.setLayout(new FlowLayout());
        toolPanel.setBackground(Color.LIGHT_GRAY);
        toolPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout());


        //Create 3 sub Panels
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new GridLayout());

///////////////////  IMAGE PANEL ///////////////////////
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BorderLayout());
        rightPanel.setBackground(Color.DARK_GRAY);

        rightPanel.add(this.viewer, BorderLayout.CENTER);


        //SubPanels of Left Panel
        JPanel subLeft = new JPanel();
        subLeft.setBackground(Color.BLUE);
        JPanel subRight = new JPanel();
        subRight.setBackground(Color.CYAN);

        //add sub panels to left panel
        leftPanel.add(subLeft);
        leftPanel.add(subRight);


        //add sub panels to main panel
        mainPanel.add(leftPanel);
        mainPanel.add(rightPanel);

        //create a Button and add it to the tool panel
        JButton btnGetImage = new JButton("Get Image");
        toolPanel.add(btnGetImage);


        //add the panels to the frame
        frame.add(toolPanel, BorderLayout.SOUTH);
        frame.add(mainPanel, BorderLayout.CENTER);

//add an action listener for the Button
        btnGetImage.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                //Display an Image
                System.out.println("button pressed");
                JFileChooser fileChooser = new JFileChooser();

                if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
                {
                    ImageIcon imageIcon = new ImageIcon(fileChooser.getSelectedFile().getAbsolutePath());
                    Image image = imageIcon.getImage();

                     setImage(image);

                }
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);

    }

    public void setImage(Image image)
    {

       this.image = image;
       this.repaint();
    }


}

any help would be beautiful, I'm new to the forum so i hope I posted this right.

benjaminTribe7,

Use bb code tag to post your source code. Source code must be surrounded with code tags.

Found problems at :

  • Modify a setImage method of IFrame class.
  • main() - Entry point method

    public void setImage(Image image){
    viewer.setImage(image);
    }
    public static void main(String []args) {
    new IFrame();
    }

very sorry for the code posted wrong. This fixed the problem. I was resetting the image variable instead of on the view Panel. thanks for the help.

very sorry for the code posted wrong. This fixed the problem. I was resetting the image variable instead of on the view Panel. thanks for the help.

package swingToolkit;
//Import Java library
import javax.swing.*;
import java.awt.*;
 
 
public class PicturePanel extends JPanel {
 
	//Variables
	private ImageIcon picture;
 
	//Method to get information of the selected file
	PicturePanel (String fileName) {
 
		picture = new ImageIcon (fileName); //Get the filename
		int w = picture.getIconWidth(); //Get the image with
		int h = picture.getIconHeight(); //Get the image height
 
		//Set preferable size for the image (Use the properties for the selected image)
		setPreferredSize(new Dimension(w, h));
		setMinimumSize(new Dimension(w, h));
		setMaximumSize(new Dimension(w, h));
 
	}
 
	//Method to draw the selected image 
	protected void paintComponent(Graphics g) {
		super.paintComponent(g); //We invoke super in order to: Paint the background, do custom painting. 
		g.drawImage(picture.getImage(), 0, 0, this); //Draw the image at its natural state
 
	}
 
}//The end
package swingToolkit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.filechooser.*;
 
public class ShowImage extends JFrame{
 
	//Variables
	JFileChooser fc = new JFileChooser();
	PicturePanel pp = null;
 
	ShowImage () {
		super("Show"); //Title
 
		//Create the GUI
		JPanel top = new JPanel();
		add(top, BorderLayout.NORTH);
		JButton openBtn = new JButton("Open");
		top.add(openBtn);
		openBtn.addActionListener(new Listner());
 
		//Settings for the GUI
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}
 
	class Listner implements ActionListener {
 
		public void actionPerformed(ActionEvent ave) {
 
			int answer = fc.showOpenDialog(ShowImage.this);
			if (answer == JFileChooser.APPROVE_OPTION){
				File f = fc.getSelectedFile();
 
				//Check
				System.out.println(f);
				System.out.println(pp);
 
				if (pp != null)
					remove(pp); //Clean so that we can open another image
 
					//Set the PicturePanel
				pp = new PicturePanel(f.getAbsolutePath());
 
				//Add PicturePanel to frame
				add(pp, BorderLayout.CENTER);
				validate();
				pack();
				repaint();
			}
		}
	}
 
	//Main
	public static void main(String[] args) {
		new ShowImage();
	}
}

i am also working for an image edittor as it is my class project.<EMAIL SNIPPED>.
please send me your perposal on this id

1. Learn how to use code tags
2. Emails in the posts not allowed, read forum rules for both mentioned
3. Don't you think issue would be either solved or forgotten after year???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.