-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSeamCarverVisualizer.java
122 lines (106 loc) · 3.9 KB
/
SeamCarverVisualizer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/******************************************************************************
* * Compilation: javac SeamCarverVisualizer.java
* * Execution: java SeamCarverVisualizer input.png seamsToRemove [horizontal | yN]
* * Dependencies: SeamCarver.java
* *
* *
* * Read image from file specified as command line argument. Use SeamCarver
* * to remove number of rows or columns specified as command line arguments.
* * Show the sequence of seams being removed.
* *
* * % java SeamCarverVisualizer HJoceanSmall.png 150 y
* *
* ******************************************************************************/
import edu.princeton.cs.algs4.Picture;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class SeamCarverVisualizer {
JFrame frame;
public void visualizeHorizontalCarve(SeamCarver sc, int N) {
for (int i = 0; i < N; i++) {
int[] minSeam = sc.findHorizontalSeam();
Picture p = sc.picture();
paintHorizontalSeam(p, minSeam);
show(p);
sc.removeHorizontalSeam(minSeam);
}
show(sc.picture());
}
public void visualizeVerticalCarve(SeamCarver sc, int N) {
for (int i = 0; i < N; i++) {
int[] minSeam = sc.findVerticalSeam();
Picture p = sc.picture();
paintVerticalSeam(p, minSeam);
show(p);
sc.removeVerticalSeam(minSeam);
}
show(sc.picture());
sc.picture().save("output.png");
}
private void paintHorizontalSeam(Picture p, int[] seam) {
for (int i = 0; i < seam.length; i++) {
p.set(i, seam[i], new Color(255, 0, 0));
}
}
private void paintVerticalSeam(Picture p, int[] seam) {
for (int i = 0; i < seam.length; i++) {
p.set(seam[i], i, new Color(255, 0, 0));
}
}
public void show(Picture img) {
if (frame == null) {
frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem menuItem1 = new JMenuItem(" Save... ");
menuItem1.addActionListener(img);
menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
menu.add(menuItem1);
frame.setJMenuBar(menuBar);
frame.setContentPane(img.getJLabel());
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setTitle("Output");
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
// draw
frame.setContentPane(img.getJLabel());
frame.revalidate();
frame.repaint();
}
public static void main(String[] args) {
if (args.length <= 1) {
System.out.println("Usage: SeamCarver [filename] [numPixels] [horizontal | yN]");
return;
}
Picture samplePicture = new Picture(args[0]);
SeamCarver sc = new SeamCarver(samplePicture);
int N = Integer.parseInt(args[1]);
SeamCarverVisualizer scv = new SeamCarverVisualizer();
if (args[2].equals("y")) {
scv.visualizeHorizontalCarve(sc, N);
} else {
scv.visualizeVerticalCarve(sc, N);
}
}
}