View Javadoc

1   /**
2    * 
3    */
4   package de.desy.acop.video;
5   
6   import java.awt.BorderLayout;
7   import java.awt.Color;
8   import java.awt.Component;
9   import java.awt.Font;
10  import java.awt.GridBagConstraints;
11  import java.awt.GridBagLayout;
12  import java.awt.Insets;
13  import java.text.DecimalFormat;
14  
15  import javax.swing.JFrame;
16  import javax.swing.JLabel;
17  import javax.swing.JPanel;
18  import javax.swing.JTable;
19  import javax.swing.SwingConstants;
20  import javax.swing.border.LineBorder;
21  import javax.swing.table.DefaultTableCellRenderer;
22  import javax.swing.table.DefaultTableModel;
23  
24  /**
25   * </code>NumericDataDisplayer</code> displays numerical image analysis data in form of
26   * a table.
27   * 
28   * @author Tilen Kusterle, Cosylab
29   *
30   */
31  public class NumericDataDisplayer extends JPanel {
32  
33  	private static final long serialVersionUID = 1L;
34  	private static final DecimalFormat decimalFormat = new DecimalFormat("#0.0");
35  	
36  	private JTable table3C;
37  	private JTable table2C;
38  	private DefaultTableModel tm3C;
39  	private DefaultTableModel tm2C;
40  	
41  	private class CellRenderer extends DefaultTableCellRenderer {
42  
43  		private static final long serialVersionUID = 1L;
44  		
45  		private int[] headerRows;
46  		
47  		public CellRenderer(int[] headerRows) {
48  			this.headerRows = headerRows;
49  		}
50  
51  		/* (non-Javadoc)
52  		 * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
53  		 */
54  		@Override
55  		public Component getTableCellRendererComponent(JTable table,
56  				Object value, boolean isSelected, boolean hasFocus, int row,
57  				int column) {
58  			JLabel label =  (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
59  					row, column);
60  			for (int i = 0; i < headerRows.length; i++) {
61  				if (headerRows[i] == row) {
62  					label.setHorizontalAlignment(SwingConstants.CENTER);
63  					label.setFont(label.getFont().deriveFont(Font.BOLD));
64  					return label;
65  				}
66  			}
67  			if (column == 0) label.setFont(label.getFont().deriveFont(Font.ITALIC));
68  			label.setHorizontalAlignment(SwingConstants.LEADING);
69  			return label;
70  		}
71  		
72  	}
73  	
74  	/**
75  	 * Constructs this </code>NumericDataDisplayer</code>..
76  	 */
77  	public NumericDataDisplayer() {
78  		super(new BorderLayout());
79  		
80  		JPanel tablePanel = new JPanel(new GridBagLayout());
81  		
82  		tm3C = new DefaultTableModel(6, 3) {
83  
84  			private static final long serialVersionUID = 1L;
85  
86  			@Override
87  			public boolean isCellEditable(int row, int column) {
88  				return false;
89  			}
90  		};
91  		tm3C.setValueAt("XY PROJECTION", 0, 0);
92  		tm3C.setValueAt("X VALUE (stat./fit)", 0, 1);
93  		tm3C.setValueAt("Y VALUE (stat./fit)", 0, 2);
94  		tm3C.setValueAt("mean [pixels]", 1, 0);
95  		tm3C.setValueAt("STD [pixels]", 2, 0);
96  		tm3C.setValueAt("amplitude", 3, 0);
97  		tm3C.setValueAt("constant", 4, 0);
98  		tm3C.setValueAt("slope", 5, 0);
99  		table3C = new JTable(tm3C);
100 //		table3C.setBorder(new LineBorder(Color.BLACK));
101 		table3C.setDefaultRenderer(Object.class, new CellRenderer(new int[]{0}));
102 		
103 		GridBagConstraints gbc = new GridBagConstraints(0,0,1,1,1,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
104 		tablePanel.add(table3C, gbc);
105 		
106 		tm2C = new DefaultTableModel(14, 2) {
107 
108 			private static final long serialVersionUID = 1L;
109 
110 			@Override
111 			public boolean isCellEditable(int row, int column) {
112 				return false;
113 			}
114 		};
115 		tm2C.setValueAt("2D DATA", 0, 0);
116 		tm2C.setValueAt("VALUE", 0, 1);
117 		tm2C.setValueAt("STD A [pixels]", 1, 0);
118 		tm2C.setValueAt("STD B [pixels]", 2, 0);
119 		tm2C.setValueAt("rotation [degrees]", 3, 0);
120 		tm2C.setValueAt("amplitude", 4, 0);
121 		tm2C.setValueAt("minimum", 5, 0);
122 		tm2C.setValueAt("IMAGE DATA", 6, 0);
123 		tm2C.setValueAt("VALUE", 6, 1);
124 		tm2C.setValueAt("width [pixels]", 7, 0);
125 		tm2C.setValueAt("height [pixels]", 8, 0);
126 		tm2C.setValueAt("ROI X [pixels]", 9, 0);
127 		tm2C.setValueAt("ROI Y [pixels]", 10, 0);
128 		tm2C.setValueAt("ROI width [pixels]", 11, 0);
129 		tm2C.setValueAt("ROI height [pixels]", 12, 0);
130 		tm2C.setValueAt("threshold", 13, 0);
131 		table2C = new JTable(tm2C);
132 //		table2C.setBorder(new LineBorder(Color.BLACK));
133 		table2C.setDefaultRenderer(Object.class, new CellRenderer(new int[]{0, 6}));
134 		
135 		gbc.gridy++;
136 		tablePanel.add(table2C, gbc);
137 		
138 		tablePanel.setBorder(new LineBorder(Color.BLACK));
139 		
140 		add(tablePanel, BorderLayout.NORTH);
141 	}
142 	
143 	/**
144 	 * Sets table data.
145 	 * @param imageW the width of image
146 	 * @param imageH the width of image
147 	 * @param roiX the start X coordinate of region of interest
148 	 * @param roiY the start Y coordinate of region of interest
149 	 * @param roiW the width of region of interest
150 	 * @param roiH the height of region of interest
151 	 * @param threshold the threshold used in calculation
152 	 * @param meanX the X mean
153 	 * @param stdX the X STD
154 	 * @param sideAmplitudeX the X amplitude
155 	 * @param sideConstX the X constant
156 	 * @param meanXFit the X mean
157 	 * @param stdXFit the X STD
158 	 * @param sideAmplitudeXFit the X amplitude
159 	 * @param sideConstXFit the X constant
160 	 * @param meanY the Y mean
161 	 * @param stdY the Y STD
162 	 * @param sideAmplitudeY the Y amplitude
163 	 * @param sideConstY the Y constant
164 	 * @param meanYFit the Y mean
165 	 * @param stdYFit the Y STD
166 	 * @param sideAmplitudeYFit the Y amplitude
167 	 * @param sideConstYFit the Y constant
168 	 * @param stdA the A STD
169 	 * @param stdB the B STD
170 	 * @param angle2D the rotation of 2D profile 
171 	 * @param amplitude2D the amplitude of 2D profile
172 	 * @param const2D the constant of 2D profile
173 	 */
174 	public void setData(int imageW, int imageH, int roiX, int roiY, int roiW, int roiH, double threshold,
175 			double meanX, double stdX, double sideAmplitudeX, double sideConstX,
176 			double meanXFit, double stdXFit, double sideAmplitudeXFit, double sideConstXFit, double sideSlopeXFit,
177 			double meanY, double stdY, double sideAmplitudeY, double sideConstY,
178 			double meanYFit, double stdYFit, double sideAmplitudeYFit, double sideConstYFit, double sideSlopeYFit,
179 			double stdA, double stdB, double angle2D, double amplitude2D, double const2D) {
180 		tm3C.setValueAt(decimalFormat.format(meanX) + " / " + decimalFormat.format(meanXFit), 1, 1);
181 		tm3C.setValueAt(decimalFormat.format(meanY) + " / " + decimalFormat.format(meanYFit), 1, 2);
182 		tm3C.setValueAt(decimalFormat.format(stdX) + " / " + decimalFormat.format(stdXFit), 2, 1);
183 		tm3C.setValueAt(decimalFormat.format(stdY) + " / " + decimalFormat.format(stdYFit), 2, 2);
184 		tm3C.setValueAt(decimalFormat.format(sideAmplitudeX) + " / " + decimalFormat.format(sideAmplitudeXFit), 3, 1);
185 		tm3C.setValueAt(decimalFormat.format(sideAmplitudeY) + " / " + decimalFormat.format(sideAmplitudeYFit), 3, 2);
186 		tm3C.setValueAt(decimalFormat.format(sideConstX) + " / " + decimalFormat.format(sideConstXFit), 4, 1);
187 		tm3C.setValueAt(decimalFormat.format(sideConstY) + " / " + decimalFormat.format(sideConstYFit), 4, 2);
188 		tm3C.setValueAt(decimalFormat.format(0) + " / " + decimalFormat.format(sideSlopeXFit), 5, 1);
189 		tm3C.setValueAt(decimalFormat.format(0) + " / " + decimalFormat.format(sideSlopeYFit), 5, 2);
190 
191 		
192 		tm2C.setValueAt(decimalFormat.format(stdA), 1, 1);
193 		tm2C.setValueAt(decimalFormat.format(stdB), 2, 1);
194 		tm2C.setValueAt(decimalFormat.format(angle2D), 3, 1);
195 		tm2C.setValueAt(decimalFormat.format(amplitude2D), 4, 1);
196 		tm2C.setValueAt(decimalFormat.format(const2D), 5, 1);
197 		tm2C.setValueAt(imageW, 7, 1);
198 		tm2C.setValueAt(imageH, 8, 1);
199 		tm2C.setValueAt(roiX, 9, 1);
200 		tm2C.setValueAt(roiY, 10, 1);
201 		tm2C.setValueAt(roiW, 11, 1);
202 		tm2C.setValueAt(roiH, 12, 1);
203 		tm2C.setValueAt(decimalFormat.format(threshold), 13, 1);
204 	}
205 	
206 	/**
207 	 * Clears table contents.
208 	 */
209 	public void clearData() {
210 		tm3C.setValueAt(null, 1, 1);
211 		tm3C.setValueAt(null, 1, 2);
212 		tm3C.setValueAt(null, 2, 1);
213 		tm3C.setValueAt(null, 2, 2);
214 		tm3C.setValueAt(null, 3, 1);
215 		tm3C.setValueAt(null, 3, 2);
216 		tm3C.setValueAt(null, 4, 1);
217 		tm3C.setValueAt(null, 4, 2);
218 		tm3C.setValueAt(null, 5, 1);
219 		tm3C.setValueAt(null, 5, 2);
220 		
221 		tm2C.setValueAt(null, 1, 1);
222 		tm2C.setValueAt(null, 2, 1);
223 		tm2C.setValueAt(null, 3, 1);
224 		tm2C.setValueAt(null, 4, 1);
225 		tm2C.setValueAt(null, 5, 1);
226 		tm2C.setValueAt(null, 7, 1);
227 		tm2C.setValueAt(null, 8, 1);
228 		tm2C.setValueAt(null, 9, 1);
229 		tm2C.setValueAt(null, 10, 1);
230 		tm2C.setValueAt(null, 11, 1);
231 		tm2C.setValueAt(null, 12, 1);
232 		tm2C.setValueAt(null, 13, 1);
233 	}
234 	
235 	/**
236 	 * Test method.
237 	 * @param args
238 	 */
239 	public static void main(String[] args) {
240 		JFrame frame = new JFrame();
241 		frame.setTitle("Test");
242 		frame.setSize(700, 700);
243 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
244 		NumericDataDisplayer ndd = new NumericDataDisplayer();
245 		frame.setContentPane(ndd);
246 		frame.setVisible(true);
247 		
248 		while(true) {
249 			if (Math.random() > 0.1) {
250 				int imageW = (int) Math.round(1000*Math.random());
251 				int imageH = (int) Math.round(1000*Math.random());
252 				int roiX = (int) Math.round(1000*Math.random());
253 				int roiY = (int) Math.round(1000*Math.random());
254 				int roiW = (int) Math.round(1000*Math.random());
255 				int roiH = (int) Math.round(1000*Math.random());
256 				double threshold = Math.random();
257 				double meanX = Math.random();
258 				double stdX = Math.random();
259 				double sideAmplitudeX = Math.random();
260 				double sideConstX = Math.random();
261 				double meanY = Math.random();
262 				double stdY = Math.random();
263 				double sideAmplitudeY = Math.random();
264 				double sideConstY = Math.random();			
265 				double stdA = Math.random();
266 				double stdB = Math.random();
267 				double angle2D = Math.random();
268 				double amplitude2D = Math.random();
269 				double const2D = Math.random();
270 				ndd.setData(imageW, imageH, roiX, roiY, roiW, roiH, threshold, meanX, stdX, sideAmplitudeX, sideConstX,
271 						Math.random(),Math.random(),Math.random(),Math.random(),Math.random(),
272 						meanY, stdY, sideAmplitudeY, sideConstY, 
273 						Math.random(),Math.random(),Math.random(),Math.random(), Math.random(),
274 						stdA, stdB, angle2D, amplitude2D, const2D);
275 			} else {
276 				ndd.clearData();
277 			}
278 			
279 			try {
280 				Thread.sleep(2000);
281 			} catch (InterruptedException e) {
282 				e.printStackTrace();
283 			}
284 		}
285 	}
286 
287 }