/Users/lyon/j4p/src/j2d/edge/CannyPanel.java
|
1 package j2d.edge;
2
3 import gui.run.RunButton;
4 import gui.run.RunSlider;
5 import j2d.ComparisonUtils;
6 import j2d.ImageBeanInterface;
7 import j2d.ImageProcessListener;
8 import j2d.ImageProcessorFactory;
9 import j2d.animation.AnimationDialog;
10
11 import javax.swing.*;
12 import java.awt.*;
13
14
15 public class CannyPanel extends JPanel {
16
17
18 private int widGaussianKernel = 15;
19 private int thresh1 = 50;
20 private int thresh2 = 230;
21 private int thresh = 128;
22 private float sigma = 1.0f;
23
24
25 ImageProcessListener ipl = null;
26
27 /**Construct the frame*/
28 public CannyPanel(ImageProcessListener _ipl) {
29 ipl = _ipl;
30 initGuiElements();
31
32 }
33
34 private JPanel getButtonPanel() {
35 JPanel jp = new JPanel();
36 jp.setLayout(new FlowLayout());
37 jp.add(new RunButton("apply") {
38 public void run() {
39 updateImage();
40 }
41 });
42 jp.add(new RunButton("Reset") {
43 public void run() {
44 ipl.update(null);
45 }
46 });
47 jp.add(new RunButton("comparison") {
48 public void run() {
49 ImageProcessorFactory ipf = new CannyProcessor(
50 thresh1,
51 thresh2,
52 thresh,
53 widGaussianKernel,
54 sigma);
55 ComparisonUtils.showComparisonFrame(
56 getComparisonPanel(ipf));
57 }
58 });
59 jp.add(new RunButton("save gifs") {
60 public void run() {
61 saveGifs();
62 }
63 });
64 return jp;
65 }
66
67 private void saveGifs() {
68 ImageProcessorFactory ipf = new CannyProcessor(
69 thresh1,
70 thresh2,
71 thresh,
72 widGaussianKernel,
73 sigma);
74
75 ImageBeanInterface origImagePanel =
76 (ImageBeanInterface) ipl;
77 new AnimationDialog(origImagePanel.getImage(),
78 ipf);
79
80
81 }
82
83 /**Component initialization*/
84 private void initGuiElements() {
85 RunSlider rs[] = {
86 RunSlider.getLabeledSlider("kernel",
87 new RunSlider(3, 40, 15) {
88 public void run() {
89 widGaussianKernel = getValue();
90 }
91 }),
92
93 RunSlider.getLabeledSlider("sigma",
94 new RunSlider(1, 10, 1) {
95 public void run() {
96 sigma = getValue();
97 }
98 }),
99
100
101 RunSlider.getLabeledSlider("Thresh1",
102 new RunSlider(1, 255, thresh1) {
103 public void run() {
104 thresh1 = getValue();
105 }
106 }),
107
108 RunSlider.getLabeledSlider("Thresh2",
109 new RunSlider(1, 255, thresh2) {
110 public void run() {
111 thresh2 = getValue();
112 }
113 }),
114
115 RunSlider.getLabeledSlider("Threshold",
116 new RunSlider(1, 255, 128) {
117 public void run() {
118 thresh = getValue();
119 }
120 }),
121
122 };
123 JPanel labelPanel = new JPanel();
124 JPanel sliderPanel = new JPanel();
125 labelPanel.setLayout(new GridLayout(0, 1));
126 sliderPanel.setLayout(new GridLayout(0, 1));
127 for (int i = 0; i < rs.length; i++) {
128 labelPanel.add(rs[i].getValueLabel());
129 sliderPanel.add(rs[i]);
130 }
131 setLayout(new BorderLayout());
132 add(labelPanel, BorderLayout.EAST);
133 add(sliderPanel, BorderLayout.CENTER);
134 add(getButtonPanel(), BorderLayout.SOUTH);
135
136 }
137
138 public JPanel getComparisonPanel(ImageProcessorFactory ipf) {
139
140 ImageBeanInterface origImagePanel =
141 (ImageBeanInterface) ipl;
142 Image sourceImage = origImagePanel.getImage();
143 return ComparisonUtils.getComparisonPanel(
144 sourceImage, ipf);
145
146 }
147
148
149 public void updateImage() {
150 ipl.update(new CannyProcessor(
151 thresh1,
152 thresh2,
153 thresh,
154 widGaussianKernel,
155 sigma
156 ));
157 }
158
159
160 }
161