1 package jp.sourceforge.stigmata.ui.swing.graph;
7 import java.awt.BorderLayout;
9 import java.awt.Dimension;
10 import java.awt.FlowLayout;
11 import java.awt.Graphics2D;
12 import java.awt.RenderingHints;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.geom.Line2D;
16 import java.awt.image.BufferedImage;
18 import java.io.IOException;
19 import java.util.HashSet;
23 import javax.imageio.ImageIO;
24 import javax.swing.Box;
25 import javax.swing.ImageIcon;
26 import javax.swing.JButton;
27 import javax.swing.JLabel;
28 import javax.swing.JOptionPane;
29 import javax.swing.JPanel;
31 import jp.sourceforge.stigmata.ui.swing.GUIUtility;
32 import jp.sourceforge.stigmata.ui.swing.StigmataFrame;
33 import jp.sourceforge.stigmata.ui.swing.actions.ChangeColorAction;
37 * @author Haruaki TAMADA
40 public class SimilarityDistributionGraphPane extends JPanel{
41 private static final long serialVersionUID = 2314463453465L;
43 private StigmataFrame stigmata;
44 private Map<Integer, Integer> distributions;
45 private int totalCount = 0;
46 private int maxFrequency = 0;
47 private JLabel iconLabel;
48 private BufferedImage image;
50 public SimilarityDistributionGraphPane(StigmataFrame stigmata, Map<Integer, Integer> distributions){
51 this.stigmata = stigmata;
53 this.distributions = distributions;
60 public String[] getSupportedFormats(){
61 String[] formats = ImageIO.getWriterFormatNames();
62 Set<String> set = new HashSet<String>();
63 for(String f: formats){
65 set.add(f.toLowerCase());
68 return set.toArray(new String[set.size()]);
71 private void drawGraph(Color color){
72 Graphics2D g = image.createGraphics();
73 g.setColor(getBackground());
74 g.fillRect(0, 0, image.getWidth(), image.getHeight());
76 int width = image.getWidth();
77 int height = image.getHeight();
78 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
80 drawBorder(g, width, height);
83 Dimension d = new Dimension(width - 20, height - 20);
84 double w = (d.width / 100d);
86 Integer v1 = distributions.get(0);
87 if(v1 == null) v1 = new Integer(0);
89 for(int i = 0; i <= 100; i++){
90 Integer v2 = distributions.get(i);
91 if(v2 == null) new Integer(0);
93 double hh1 = v1 * ((double)height / totalCount);
94 double hh2 = v2 * ((double)height / totalCount);
96 g.draw(new Line2D.Double(x, d.height - hh1, x + w, d.height - hh2));
100 iconLabel.setIcon(new ImageIcon(image));
103 private void drawBorder(Graphics2D g, int width, int height){
104 g.setColor(Color.BLACK);
105 g.draw(new Line2D.Double(0, height - 20, width, height - 20));
106 g.draw(new Line2D.Double(20, 0, 20, height));
108 g.setColor(Color.GRAY);
110 double h = (height - 20) / 2d;
111 g.draw(new Line2D.Double(20, h, width, h));
113 g.draw(new Line2D.Double(20, h, width, h));
114 g.draw(new Line2D.Double(20, h * 3, width, h * 3));
117 double w = (width - 20d) / 2d;
118 g.draw(new Line2D.Double(w + 20, 0, w + 20, height - 20));
120 g.draw(new Line2D.Double(w + 20, 0, w + 20, height - 20));
121 g.draw(new Line2D.Double(w * 3 + 20, 0, w * 3 + 20, height - 20));
123 g.drawString("0", 10, height - 5);
124 g.drawString("50%", (width - 20) / 2 + 10, height - 5);
125 g.drawString(stigmata.getMessages().get("similarity.label"), width - 60, height - 5);
126 g.drawString("50%", 0, (height - 20) / 2);
129 private void initializeData(){
131 for(int i = 0; i <= 100; i++){
132 Integer frequency = distributions.get(new Integer(i));
133 if(frequency == null){
134 frequency = new Integer(0);
135 distributions.put(new Integer(i), frequency);
137 if(maxFrequency < frequency.intValue()){
138 maxFrequency = frequency.intValue();
140 totalCount += frequency.intValue();
144 private void initializeLayouts(){
145 image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
147 JPanel center = new JPanel(new FlowLayout(FlowLayout.CENTER));
148 iconLabel = new JLabel();
149 Box south = Box.createHorizontalBox();
150 JButton storeImageButton = GUIUtility.createButton(stigmata.getMessages(), "savegraph");
151 JButton switchColorButton = new JButton(new ChangeColorAction(stigmata, new ActionListener(){
152 public void actionPerformed(ActionEvent e){
153 ChangeColorAction action = (ChangeColorAction)e.getSource();
154 if(action.isColorSelected()){
155 drawGraph(action.getColor());
159 storeImageButton.addActionListener(new ActionListener(){
160 public void actionPerformed(ActionEvent e){
165 setLayout(new BorderLayout());
166 center.add(iconLabel = new JLabel());
167 south.add(Box.createHorizontalGlue());
168 south.add(switchColorButton);
169 south.add(Box.createHorizontalGlue());
170 south.add(storeImageButton);
171 south.add(Box.createHorizontalGlue());
173 add(center, BorderLayout.CENTER);
174 add(south, BorderLayout.SOUTH);
177 private void storeGraphImage(){
178 String[] exts = getSupportedFormats();
179 File file = stigmata.getSaveFile(
180 exts, stigmata.getMessages().get("savegraph.description")
184 String format = file.getName();
185 format = format.substring(format.lastIndexOf('.') + 1);
187 ImageIO.write(image, format, file);
189 } catch(IOException e){
190 JOptionPane.showMessageDialog(
192 stigmata.getMessages().get("error.io", e.getMessage()),
193 stigmata.getMessages().get("error.dialog.title"),
194 JOptionPane.ERROR_MESSAGE