/* * @(#)Flip.java */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.*; import java.net.URL; public class Flip extends JApplet implements ItemListener { JCheckBox flipXBox; JCheckBox flipYBox; FlipPanel flipPanel; public void init() { // Load the face image Image img = getImage(getURL("monalisa.jpg")); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(img, 0); tracker.waitForID(0); } catch ( Exception e ) { e.printStackTrace(); } // // Build a user interface // Container mainPane = getContentPane(); // The drawing panel flipPanel = new FlipPanel(img); // Button1 flipXBox = new JCheckBox("Flip X"); flipXBox.setSelected(flipPanel.getFlipX()); flipXBox.addItemListener(this); // Button2 flipYBox = new JCheckBox("Flip Y"); flipYBox.setSelected(flipPanel.getFlipY()); flipYBox.addItemListener(this); // Button array JPanel checkPanel = new JPanel(); checkPanel.setLayout(new GridLayout(1, 2)); checkPanel.add(flipXBox); checkPanel.add(flipYBox); mainPane.setLayout(new BorderLayout()); mainPane.add(checkPanel, BorderLayout.NORTH); mainPane.add(flipPanel, BorderLayout.CENTER); } // This handles the check boxes public void itemStateChanged(ItemEvent e) { Object source = e.getItemSelectable(); if (source == flipXBox) { if (e.getStateChange() == ItemEvent.SELECTED) { flipPanel.setFlipX(true); } else if (e.getStateChange() == ItemEvent.DESELECTED) { flipPanel.setFlipX(false); } } else if (source == flipYBox) { if (e.getStateChange() == ItemEvent.SELECTED) { flipPanel.setFlipY(true); } else if (e.getStateChange() == ItemEvent.DESELECTED) { flipPanel.setFlipY(false); } } } private URL getURL(String filename) { URL codeBase = this.getCodeBase(); URL url = null; try { url = new URL(codeBase, filename); } catch (java.net.MalformedURLException e) { System.out.println("Couldn't create image, badly specified URL: " + url); return null; } return url; } // FlipPanel implements the drawing Panel private class FlipPanel extends JPanel { private BufferedImage face; private boolean flipX=false; private boolean flipY=false; public FlipPanel(Image img) { setBackground(Color.white); int iw = img.getWidth(this); int ih = img.getHeight(this); face = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB); Graphics2D faceg = face.createGraphics(); faceg.drawImage(img,0,0,this); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension d = getSize(); int w = d.width; int h = d.height; // Creates the buffered image. BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D gbi = buffImg.createGraphics(); // Clears the previously drawn image. g2.setColor(Color.white); g2.fillRect(0, 0, w, h); int k, l; int faceWidth = face.getWidth(); int faceHeight = face.getHeight(); // Loop over the colours in the original face for (int i=0;i