teaching machines

CS 245 Lecture 6 – Manipulating Images and Custom Painting

February 6, 2014 by . Filed under cs245, lectures, spring 2014.

Agenda

TODO

Code

PhotoBop.java

package lecture06;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.beans.DesignMode;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class PhotoBop extends JFrame {
  private BufferedImage sourceImage;
  private JLabel sourceLabel;
  private JLabel destinationLabel;
  
  public PhotoBop(String path) throws IOException {
    super("PhotoBop");
    
    sourceImage = ImageIO.read(new File(path));
    
    JButton flipButton = new JButton("Flip");
    JButton uglifyButton = new JButton("Uglify");
    
    add(flipButton, BorderLayout.NORTH);
    add(uglifyButton, BorderLayout.SOUTH);
    
    sourceLabel = new JLabel(new ImageIcon(sourceImage));
    destinationLabel = new JLabel();
    
    add(sourceLabel, BorderLayout.WEST);
    add(destinationLabel, BorderLayout.EAST);
    
    flipButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        // INSTEAD OF FILLING THIS CODE UP WITH ALL THE IMAGE
        // MANIPULATION MUMBO JUMBO, I'M GOING TO DEFER
        flip();
      }
    });
    
    uglifyButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        uglify();
      }
    });
    
    setSize(1024, 768);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }
  
  private void flip() {
    BufferedImage destinationImage =
        new BufferedImage(sourceImage.getWidth(),
                          sourceImage.getHeight(),
                          sourceImage.getType());
    for (int c = 0; c < destinationImage.getWidth(); ++c) {
      for (int r = 0; r < destinationImage.getHeight(); ++r) {
        int sourceColorAsInt = sourceImage.getRGB(sourceImage.getWidth() - 1 - c, r);
        destinationImage.setRGB(c, r, sourceColorAsInt);
      }
    }
    
    destinationLabel.setIcon(new ImageIcon(destinationImage));
  }
  
  private void uglify() {
    BufferedImage destinationImage =
        new BufferedImage(sourceImage.getWidth(),
                          sourceImage.getHeight(),
                          sourceImage.getType());
    
    for (int c = 0; c < destinationImage.getWidth(); ++c) {
      for (int r = 0; r < destinationImage.getHeight(); ++r) {
        int sourceColorAsInt = sourceImage.getRGB(c, r);
        Color color = new Color(sourceColorAsInt);
//        color = color.darker();
//        color = color.darker();
//        color = color.darker();
        Color swappedColor = new Color(color.getGreen(), color.getBlue(), color.getRed());
        destinationImage.setRGB(c, r, swappedColor.getRGB());
      }
    }
    
    destinationLabel.setIcon(new ImageIcon(destinationImage));
  }
  
  public static void main(String[] args) throws IOException {
//    JFrame frame = new JFrame("PhotoBop");
    
    new PhotoBop("/Users/johnch/Desktop/nye.jpg");
  }
}

PolylineArtist.java

package lecture06;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PolylineArtist extends JFrame implements MouseListener{
  private DrawingPanel panel;
  private ArrayList<Point> clicks;
  
  public PolylineArtist() {
    panel = new DrawingPanel();
    add(panel, BorderLayout.CENTER);
    
    clicks = new ArrayList<Point>();
    addMouseListener(this);
    
    setSize(1024, 768);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }
  
  class DrawingPanel extends JPanel {
    public void paintComponent(Graphics g) {
//      g.drawOval(10, 10, 100, 30);
      for (Point p : clicks) {
        g.drawOval(p.x, p.y, 10, 10);
      }
    }
  }
  
  public static void main(String[] args) {
    new PolylineArtist();
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    Point p = new Point(e.getX(), e.getY());
    clicks.add(p);
    repaint();
  }

  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
  }

  @Override
  public void mousePressed(MouseEvent e) {
  }

  @Override
  public void mouseReleased(MouseEvent e) {
  }
}

Haiku

“Helpdesk. Lob your prob.”
“My mouse is breathing heavy.”
“Did you overclick?”