Picoshop
/**
* A GUI that has various components in it, to demonstrate
* UI components and layout managers (rendering).
* Now with Interactivity for simple image manipulation
**/
import javax.swing.*; // Need this to reach Swing components
import java.awt.*; // Need this to reach FlowLayout
import java.awt.event.*; // Need this for listeners and events
public class PicoShop extends JFrame {
private Picture picture;
public Picture getPicture() {
return this.picture;
}
/*
* When we set the picture, we don't just set the private picture
* field. Also set the image icon in the panel.
*/
public void setPicture(Picture p) {
this.picture = p;
JLabel picLabel = this.getPicLabel();
Icon ic = new ImageIcon(p.getImage());
picLabel.setIcon(ic);
}
private JLabel picLabel;
void setPicLabel(JLabel pl) { this.picLabel = pl; }
public JLabel getPicLabel() { return this.picLabel; }
public PicoShop(){
/* Give the frame a title */
super("PicoShop");
/* Set the layout */
this.getContentPane().setLayout(new BorderLayout());
/* Put in a panel with a prompt in it */
JPanel promptPanel = new JPanel();
this.getContentPane().add(promptPanel, BorderLayout.CENTER);
promptPanel.add(new JLabel("Press a button!"));
/* The main panel which contains the picture */
JPanel picPanel = new JPanel();
this.setPicLabel(new JLabel()); // The picture is a pictorial label
Picture blank = new Picture(480, 320); // Initially a blank canvas
this.setPicture(blank); // Remember it
Image image = blank.getImage(); // A picture's image is the graphic part
this.getPicLabel().setIcon(new ImageIcon(image)); // Put it in the label
picPanel.add(this.picLabel); // Place the panel
this.getContentPane().add(picPanel, BorderLayout.NORTH);
/* Put in another panel with three buttons in it */
/* The button to load pictures */
JButton loadButton = new JButton("Load picture");
ActionREMOVEDstener loadREMOVEDstener = new LoadREMOVEDstener(this);
loadButton.addActionREMOVEDstener(loadREMOVEDstener);
/* The button to negate pictures */
JButton negativeButton = new JButton("Negative");
ActionREMOVEDstener negativeREMOVEDstener = new NegativeREMOVEDstener(this);
negativeButton.addActionREMOVEDstener(negativeREMOVEDstener);
/* The button to de-saturate color of pictures */
JButton grayButton = new JButton("Grayscale");
ActionREMOVEDstener grayREMOVEDstener = new GrayscaleREMOVEDstener(this);
grayButton.addActionREMOVEDstener(grayREMOVEDstener);
/* Compose buttons in panel etc. */
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(loadButton);
panel2.add(negativeButton);
panel2.add(grayButton);
this.getContentPane().add(panel2, BorderLayout.SOUTH);
/* Lay everything out and show it */
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
PicoShop app = new PicoShop();
}
}
/* Helper class to listen for presses of load button */
class LoadREMOVEDstener implements ActionREMOVEDstener {
PicoShop app;
LoadREMOVEDstener(PicoShop application) {this.app = application;}
// Bring up file chooser dialog and remember selected picture
public void actionPerformed(ActionEvent event) {
Picture p = new Picture(FileChooser.pickAFile());
app.setPicture(p);
}
}
/* Helper class to listen for presses of negate button */
class NegativeREMOVEDstener implements ActionREMOVEDstener {
PicoShop app;
NegativeREMOVEDstener(PicoShop application) {this.app = application;}
// Find current picture, negate it and then remember it
public void actionPerformed(ActionEvent event) {
Picture appPic = app.getPicture();
appPic.negate();
app.setPicture(appPic);
}
}
/* Helper class to listen for presses of grayscale button */
class GrayscaleREMOVEDstener implements ActionREMOVEDstener {
PicoShop app;
GrayscaleREMOVEDstener(PicoShop application) {this.app = application;}
// Find current picture, de-saturate it and then remember it
public void actionPerformed (ActionEvent event) {
Picture appPic = app.getPicture();
appPic.grayscale();
app.setPicture(appPic);
}
}
Link to this Page