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.