/Users/lyon/j4p/src/ip/gui/frames/HistogramFrame.java

1    /** 
2     * Victor Silva - University of Bridgeport 27 April 1997 
3     * 
4     * Computes and displays the histogram for each primary 
5     * color of an image that was previously read. 
6     * 
7     * modified by DL 3/12/98...made 2D, added CMF, etc. 
8     */ 
9    package ip.gui.frames; 
10    
11   import graphics.dclap.SavePICT; 
12   import math.MatFloat; 
13   import math.Stats; 
14    
15   import java.awt.*; 
16   import java.awt.event.ActionEvent; 
17   import java.awt.event.ActionListener; 
18   import java.io.FileOutputStream; 
19   import java.io.IOException; 
20    
21    
22   public class HistogramFrame 
23           extends ShortCutFrame implements ActionListener { 
24       private int FRAME_WIDTH = 275; 
25       private int FRAME_HEIGHT = 150; 
26       private int Ysize = 
27               getToolkit().getScreenSize().height; 
28       private static int startx = 256; 
29       private static int starty = 0; 
30       private Menu fileMenu = getMenu("File"); 
31       private MenuItem print_mi = addMenuItem(fileMenu, "[p]rint"); 
32       private MenuItem save_mi = addMenuItem(fileMenu, "[s]ave as pict"); 
33       private Font f = new Font("Times", Font.PLAIN, 12); 
34    
35       Stats stats = new Stats(); 
36    
37       public HistogramFrame(String title) { 
38           super(title); 
39       } 
40    
41       private void initMenu() { 
42           MenuBar mb = new MenuBar(); 
43           mb.add(fileMenu); 
44           setMenuBar(mb); 
45       } 
46    
47       private void moveFrame(int x, int y) { 
48           setBounds(x, y, 
49                   FRAME_WIDTH, FRAME_HEIGHT); 
50       } 
51    
52       public void myShow() { 
53           moveFrame(startx, starty); 
54           starty += FRAME_HEIGHT; 
55           if (starty * 1.5 > Ysize) starty = 0; 
56           super.setVisible(true); 
57       } 
58    
59       public double[] getCMF() { 
60           return stats.getCMF(); 
61       } 
62    
63       public void printPMF() { 
64           stats.printPMF(); 
65       } 
66    
67       public void printCMF() { 
68           stats.printCMF(); 
69       } 
70    
71       public HistogramFrame(short plane[][], String title) { 
72           super(title); 
73           setSize(FRAME_WIDTH, FRAME_HEIGHT); 
74           initMenu(); 
75           setResizable(true); 
76           stats.initStats(plane); 
77       } 
78    
79       //private void save() { 
80       //  ip.gui.SaveFrame.print(this); 
81   // } 
82       private void save() { 
83           FileDialog fd = new FileDialog(this, 
84                   "Enter a pict file name", FileDialog.SAVE); 
85           fd.setVisible(true); 
86           fd.setVisible(false); 
87           String fn = fd.getDirectory() + fd.getFile(); 
88           //if usr canceled, return 
89           if (fd.getFile() == null) return; 
90           try { 
91               FileOutputStream fos = 
92                       new FileOutputStream(fn); 
93               Component c = this; 
94               SavePICT p = new SavePICT(); 
95               p.saveAsPict(c, fos); 
96               fos.close(); 
97           } catch (IOException e) { 
98               System.out.println(e); 
99           } 
100      } 
101   
102   
103      public void actionPerformed(ActionEvent e) { 
104          if (match(e, save_mi)) { 
105              save(); 
106              return; 
107          } 
108          if (match(e, print_mi)) { 
109              print(this); 
110              return; 
111          } 
112          super.actionPerformed(e); 
113      } 
114   
115      public void update(Graphics g) { 
116          Rectangle r = getBounds(); 
117          FRAME_WIDTH = r.width; 
118          FRAME_HEIGHT = r.height; 
119          g.clearRect(0, 0, r.width, r.height); 
120          paint(g); 
121      } 
122   
123      public void paint(Graphics g) { 
124   
125          int leftMargin = 15; 
126          int bottomMargin = FRAME_HEIGHT - 45; 
127          g.setFont(f); 
128          float max = (float) MatFloat.getMax(stats.getPMF()); 
129          float yscale = (float) (0.9 * bottomMargin / max); 
130          g.drawString("PMF max=" + max, 
131                  leftMargin, bottomMargin + 12); 
132          for (int i = 0,x1 = leftMargin; i < 256; i++, x1 = i + leftMargin) 
133              g.drawLine(x1, bottomMargin, x1, 
134                      bottomMargin - (int) (yscale * stats.getPMF()[i])); 
135      } 
136   
137      public static void print(Frame comp) { 
138          Toolkit tk = Toolkit.getDefaultToolkit(); 
139          PrintJob printJob = 
140                  tk.getPrintJob( 
141                          comp, 
142                          "print me!", 
143                          null); 
144          Graphics g = printJob.getGraphics(); 
145          comp.print(g); 
146          printJob.end(); 
147      } 
148  }