File: OctreeDialog.java

package info (click to toggle)
java-imaging-utilities 0.14.2%2B3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,280 kB
  • ctags: 3,725
  • sloc: java: 31,190; sh: 238; makefile: 53; xml: 30
file content (221 lines) | stat: -rw-r--r-- 5,424 bytes parent folder | download | duplicates (2)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * OctreeDialog
 * 
 * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.gui.awt.dialogs;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import net.sourceforge.jiu.apps.Strings;
import net.sourceforge.jiu.color.dithering.ErrorDiffusionDithering;

/**
 * A dialog to enter the parameters for an Octree color quantization operation.
 * It also allows to enter the optional algorithms that can be applied in combination with Octree.
 *
 * @author Marco Schmidt
 * @since 0.6.0
 * @see MedianCutDialog
 */
public class OctreeDialog extends Dialog implements 
	ActionListener, KeyListener
{
	public final int[] DITHERING_STRINGS =
	{
		Strings.DITHERING_NONE,
		Strings.FLOYD_STEINBERG_ERROR_DIFFUSION,
		Strings.STUCKI_ERROR_DIFFUSION,
	 	Strings.BURKES_ERROR_DIFFUSION,
		Strings.SIERRA_ERROR_DIFFUSION,
		Strings.JARVIS_JUDICE_NINKE_ERROR_DIFFUSION,
		Strings.STEVENSON_ARCE_ERROR_DIFFUSION
	};
	public final int[] DITHERING_TYPES =
	{
		0,
		ErrorDiffusionDithering.TYPE_FLOYD_STEINBERG,
		ErrorDiffusionDithering.TYPE_STUCKI,
	 	ErrorDiffusionDithering.TYPE_BURKES,
		ErrorDiffusionDithering.TYPE_SIERRA,
		ErrorDiffusionDithering.TYPE_JARVIS_JUDICE_NINKE,
		ErrorDiffusionDithering.TYPE_STEVENSON_ARCE
	};
	private Button ok;
	private Button cancel;
	private TextField numColorsField;
	private Choice outputColorType;
	private Choice dithering;
	private boolean pressedOk;

	/**
	 * Creates a modal dialog to enter the parameter.
	 * @param owner the parent of this modal dialog
	 * @param strings an object to get String constants in the current language
	 * @param numColors the number of colors in the resulting image
	 * @param paletted if true, the output image will be paletted, otherwise truecolor
	 */
	public OctreeDialog(Frame owner, Strings strings, int numColors, boolean paletted)
	{
		super(owner, strings.get(Strings.OCTREE_COLOR_QUANTIZATION), true);
		pressedOk = false;

		Panel panel = new Panel();
		panel.setLayout(new GridLayout(0, 2));

		panel.add(new Label(strings.get(Strings.NUM_COLORS)));
		numColorsField = new TextField(Integer.toString(numColors), 6);
		numColorsField.addKeyListener(this);
		panel.add(numColorsField);

		/*panel.add(new Label(strings.get(Strings.OUTPUT_COLOR_TYPE)));
		outputColorType = new Choice();
		outputColorType.add(strings.get(Strings.OUTPUT_COLOR_TYPE_PALETTED));
		outputColorType.add(strings.get(Strings.OUTPUT_COLOR_TYPE_RGB));
		outputColorType.select(paletted ? 0 : 1);
		panel.add(outputColorType);*/

		panel.add(new Label(strings.get(Strings.DITHERING_METHOD)));
		dithering = new Choice();
		for (int i = 0; i < DITHERING_STRINGS.length; i++)
		{
			dithering.add(strings.get(DITHERING_STRINGS[i]));
		}
		dithering.select(1);
		panel.add(dithering);

		add(panel, BorderLayout.CENTER);

		ok = new Button(strings.get(Strings.OK));
		ok.addActionListener(this);
		cancel = new Button(strings.get(Strings.CANCEL));
		cancel.addActionListener(this);

		panel = new Panel();
		panel.add(ok);
		panel.add(cancel);
		add(panel, BorderLayout.SOUTH);

		pack();
		center();
	}

	/**
	 * Hides (closes) this dialog if the OK button was source of the action event
	 * (e.g. if the button was pressed).
	 */
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == ok)
		{
			pressedOk = true;
			setVisible(false);
		}
		else
		if (e.getSource() == cancel)
		{
			setVisible(false);
		}
	}

	/**
	 * Centers the dialog on screen.
	 */
	public void center()
	{
		Rectangle rect = getBounds();
		int width = rect.width;
		int height = rect.height;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screenSize.width / 2) - (width / 2),
			(screenSize.height / 2) - (height / 2));
	}

	public int getErrorDiffusion()
	{
		int sel = dithering.getSelectedIndex();
		if (sel > 0)
		{
			return DITHERING_TYPES[sel];
		}
		else
		{
			return -1;
		}
	}

	private int getIntValue(TextField textField)
	{
		try
		{
			return Integer.parseInt(textField.getText());
		}
		catch (NumberFormatException nfe)
		{
			return -1;
		}
	}	

	public int getNumColors()
	{
		return getIntValue(numColorsField);
	}

	public boolean hasPressedOk()
	{
		return pressedOk;
	}

	public boolean isOutputTypePaletted()
	{
		return outputColorType.getSelectedIndex() == 0;
	}

	public void keyPressed(KeyEvent e)
	{
		updateOkButton();
	}

	public void keyReleased(KeyEvent e)
	{
		updateOkButton();
	}

	public void keyTyped(KeyEvent e)
	{
		updateOkButton();
	}

	private void updateOkButton()
	{
		int nc = getNumColors();
		boolean enabled = nc >= 1 && nc <= 256;
		ok.setEnabled(enabled);
	}

	public boolean useErrorDiffusion()
	{
		return dithering.getSelectedIndex() > 0;
	}

	public boolean useNoDithering()
	{
		return dithering.getSelectedIndex() == 0;
	}
}