...

View Full Version : Image editing help



sambar89
12-13-2011, 09:11 PM
Hi, i've been tasked with creating a photo album that also lets users edit the photos... so far i've managed to open a file and display it in a JPanel but i'm having trouble actually editing this photo in the JPanel... I've read through countless books and searched the internet in vain.. either they're was no help or i'm just not getting it.. any help would be more than appretiated..



import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.*;
import java.awt.Color;

public class PictureFrame extends JFrame implements ActionListener {

Image img;
BufferedImage greyImage;
JButton getPictureButton;
JButton textButton;
JButton circleButton;
JButton zoomButton;
JButton greyButton;
JButton invertButton;
JButton saveButton;

public static void main(String[] args){
new PictureFrame();
}

public PictureFrame(){
this.setSize(900, 700);
this.setTitle("Photo Album");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel picPanel = new PicturePanel();
this.add(picPanel, BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();
GridLayout buttonPanelLayout = new GridLayout(1, 6);
buttonPanel.setLayout(buttonPanelLayout);
getPictureButton = new JButton("Open image"); // Open picture Button
getPictureButton.setActionCommand("open");
getPictureButton.addActionListener(this);
buttonPanel.add(getPictureButton);

textButton = new JButton("Text"); // Annotate button
textButton.setActionCommand("text");
textButton.addActionListener(this);
buttonPanel.add(textButton);

circleButton = new JButton("Circle"); // Circle button
circleButton.setActionCommand("circle");
circleButton.addActionListener(this);
buttonPanel.add(circleButton);

zoomButton = new JButton("Zoom"); // Zoom button
zoomButton.setActionCommand("zoom");
zoomButton.addActionListener(this);
buttonPanel.add(zoomButton);

greyButton = new JButton("Grey"); // Grey button
greyButton.setActionCommand("grey");
greyButton.addActionListener(this);
buttonPanel.add(greyButton);

invertButton = new JButton("Invert"); // Invert button
invertButton.setActionCommand("invert");
invertButton.addActionListener(this);
buttonPanel.add(invertButton);

saveButton = new JButton("Save"); // Save button
saveButton.setActionCommand("save");
saveButton.addActionListener(this);
buttonPanel.add(saveButton);
this.add(buttonPanel, BorderLayout.SOUTH);

this.setVisible(true);
}

public void actionPerformed(ActionEvent e){

if ((e.getActionCommand()).equals("open")){
String file = getImageFile();
if(file != null){
Toolkit kit = Toolkit.getDefaultToolkit();
img = kit.getImage(file);
img = img.getScaledInstance(700, -1, Image.SCALE_SMOOTH);
this.repaint();
}
}
else if ((e.getActionCommand()).equals("text")){

}
else if ((e.getActionCommand()).equals("circle")){

}
else if ((e.getActionCommand()).equals("zoom")){

}
else if ((e.getActionCommand()).equals("grey")){
BufferedImage greyImage = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_BYTE_GRAY);
greyImage = (BufferedImage) img;
Graphics g = greyImage.getGraphics();
g.drawImage(greyImage, 0, 0, this);
g.dispose();
}
else if ((e.getActionCommand()).equals("invert")){

}
else if ((e.getActionCommand()).equals("save")){

}
}

private String getImageFile(){
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new ImageFilter());
int result = fc.showOpenDialog(null);
File file = null;
if(result == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
return file.getPath();
}
else{
return null;
}
}

private class PicturePanel extends JPanel{
public void paint(Graphics g){
g.drawImage(img, 0, 0, this);
}
}

private class ImageFilter extends javax.swing.filechooser.FileFilter{
public boolean accept(File f){
if(f.isDirectory()){
return true;
}
String name = f.getName();
if(name.matches("*.((.jpg)|(.gif)|(.png))")){
return true;
}
else{
return false;
}
}

public String getDescription(){
return "Image files (*.jpg, *.gif, *.png)";
}
}


}



So here's my code.. at the moment i'm just trying to figure out the convert to greyscale button as that seems the most straight forward in terms of actually understanding how to edit the displayed photo

Thanks alot!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum