View Javadoc
1 package edu.psu.geovista.app.scatterplot; 2 3 /*** 4 * Title: Histogram 5 * Description: Create histogram for a variable 6 * Copyright: Copyright (c) 2002 7 * Company: GeoVISTA Center 8 * @author Xiping Dai 9 * @version 1.0 10 */ 11 12 import java.lang.*; 13 import java.awt.*; 14 import javax.swing.*; 15 import java.awt.event.*; 16 import java.util.Vector; 17 import javax.swing.event.EventListenerList; 18 19 public class Histogram extends JPanel implements MouseListener, ComponentListener{ 20 private static final String COMMAND_POINT_SELECTED = "cmdSel"; 21 private static double AXISSPACEPORTION = 1.0/6.0; 22 private static int DEFAULT_HIST_NUM = 20; 23 transient private double[] data; 24 transient private String variableName; 25 transient private DataArray dataArray; 26 transient private DataArray histArray; 27 transient private double[] histogramArray; 28 transient private double[] accumulativeFrequency; 29 transient private double[] dataX; 30 transient private int histNumber = DEFAULT_HIST_NUM; 31 transient private double barWidth; 32 transient private boolean axisOn = true; 33 transient private boolean accFrequency = false; 34 transient private int plotOriginX; 35 transient private int plotOriginY; 36 transient private int plotEndX; 37 transient private int plotEndY; 38 transient private double[] xAxisExtents; 39 transient private double[] yAxisExtents; 40 transient private int[] exsInt; 41 transient private int[] whyInt; 42 transient private int[] accumulativeInt; 43 transient private int[] selectionInt; 44 transient private int exsStep; 45 //private Vector selRecords = new Vector(); 46 transient private int[] selectedRecords; 47 transient private Vector[] histRecords; 48 transient private Rectangle[] histRecs; 49 transient private double[] selectionArray; //the count of seleced observation in each histogram bin. 50 transient private Color background; 51 transient private Color foreground; 52 transient private int mouseX1, mouseX2, mouseY1, mouseY2; 53 transient private int selectX = 0; 54 transient private int selectY = 0; 55 transient private int selectWidth = 0; 56 transient private int selectHeight = 0; 57 transient private JPopupMenu popup; 58 transient private JDialog dialog1; 59 transient private JDialog dialog2; 60 transient private JTextField histNumberField = new JTextField(16); 61 transient private JTextField yAxisMinField = new JTextField(16); 62 transient private JTextField yAxisMaxField = new JTextField(16); 63 transient private JTextField xAxisMinField = new JTextField(16); 64 transient private JTextField xAxisMaxField = new JTextField(16); 65 transient private EventListenerList listenerListAction = new EventListenerList(); 66 67 public Histogram() { 68 this.setPreferredSize(new Dimension(300,300)); 69 } 70 71 public void setData (double[] data){ 72 this.data = data; 73 this.dataArray = new DataArray(data); 74 this.xAxisExtents = (double[])dataArray.getExtent().clone(); 75 this.selectedRecords = new int[data.length]; 76 histogramCalculation (); 77 this.addComponentListener(this); 78 this.setupDataforDisplay(); 79 this.setAccumulativeFrequency(); 80 //Create the popup menu. 81 popup = new JPopupMenu(); 82 JMenuItem menuItem = new JMenuItem("Set Histogram Range"); 83 menuItem.addActionListener(new ActionListener() { 84 85 /*** 86 * put your documentation comment here 87 * @param e 88 */ 89 public void actionPerformed (ActionEvent e) { 90 showDialog1(400, 400); 91 } 92 }); 93 popup.add(menuItem); 94 menuItem = new JMenuItem("Set Frequency Range"); 95 menuItem.addActionListener(new ActionListener() { 96 97 /*** 98 * put your documentation comment here 99 * @param e 100 */ 101 public void actionPerformed (ActionEvent e) { 102 showDialog2(400, 400); 103 } 104 }); 105 popup.add(menuItem); 106 menuItem = new JMenuItem("Accumulative Frequency"); 107 menuItem.addActionListener(new ActionListener() { 108 109 /*** 110 * put your documentation comment here 111 * @param e 112 */ 113 public void actionPerformed (ActionEvent e) { 114 setAccFrequency (!accFrequency); 115 repaint(); 116 } 117 }); 118 popup.add(menuItem); 119 addMouseListener(this); 120 } 121 122 public double[] getData (){ 123 return this.data; 124 } 125 126 public void setVariableName (String name){ 127 this.variableName = name; 128 } 129 130 public String getVariableName (){ 131 return this.variableName; 132 } 133 134 public void setHistNumber (int num){ 135 this.histNumber = num; 136 } 137 138 public int getHistNumber (){ 139 return this.histNumber; 140 } 141 142 public void setAxisOn (boolean axisOn){ 143 this.axisOn = axisOn; 144 } 145 146 public boolean getAxisOn (){ 147 return this.axisOn; 148 } 149 150 /* public void setSelection(Vector selectedObs){ 151 this.selRecords = selectedObs; 152 //System.out.println(selRecords.size()); 153 if (this.selectionArray == null || this.selectionArray.length != this.histNumber){ 154 this.selectionArray = new double[this.histNumber]; 155 } else { 156 for (int i = 0; i < this.selectionArray.length; i ++){ 157 this.selectionArray[i] = 0; 158 } 159 } 160 if (this.selectedRecords == null){ 161 this.selectedRecords = new int[this.data.length]; 162 } 163 for(int i = 0; i < selRecords.size(); i ++){ 164 int j=(int)Math.floor((data[((Integer)selRecords.get(i)).intValue()]-xAxisExtents[0])/barWidth); 165 j=((this.histNumber<=j) ? this.histNumber-1 :j); 166 this.selectionArray[j] ++; 167 this.selectedRecords[((Integer)selRecords.get(i)).intValue()] = 1; 168 } 169 this.setSelectionScreen(); 170 }*/ 171 172 public void setSelections(int[] selectedObs){ 173 this.selectedRecords = selectedObs; 174 if (this.selectionArray == null || this.selectionArray.length != this.histNumber){ 175 this.selectionArray = new double[this.histNumber]; 176 } else { 177 for (int i = 0; i < this.selectionArray.length; i ++){ 178 this.selectionArray[i] = 0; 179 } 180 } 181 for(int i = 0; i < this.selectedRecords.length; i ++){ 182 int j; 183 if (this.selectedRecords[i] ==1){ 184 j=(int)Math.floor((data[i]-xAxisExtents[0])/barWidth); 185 j=((this.histNumber<=j) ? this.histNumber-1 :j); 186 this.selectionArray[j] ++; 187 } 188 } 189 this.setSelectionScreen(); 190 } 191 192 public int[] getSelections (){ 193 return this.selectedRecords; 194 } 195 196 /* public Vector getSelection (){ 197 return this.selRecords; 198 }*/ 199 200 /*** 201 * Minimum and maximum values for xAxis. xAxisExtents[0] = min, xAxisExtents[1] = max. 202 * @param double[] xAxisExtents 203 */ 204 public void setXAxisExtents (double[] xAxisExtents) { 205 //System.out.println("set up axis ..." + xAxisExtents[0]); 206 this.xAxisExtents = (double[])xAxisExtents.clone(); 207 //System.out.println("set up axis ..." + xAxisExtents[0]); 208 this.histogramCalculation(); 209 this.setupDataforDisplay(); 210 repaint(); 211 } 212 213 /*** 214 * put your documentation comment here 215 * @return 216 */ 217 public double[] getXAxisExtents () { 218 return this.xAxisExtents; 219 } 220 221 public void setAccFrequency(boolean accFrequency){ 222 this.accFrequency = accFrequency; 223 } 224 225 /*** 226 * put your documentation comment here 227 * @param c 228 */ 229 public void setBackground (Color c) { 230 if (c == null) 231 return; 232 this.background = c; 233 int colorTotal = c.getRed() + c.getGreen() + c.getBlue(); 234 int greyColor = 128 * 3; 235 if (colorTotal < greyColor) 236 this.foreground = Color.white; 237 else 238 this.foreground = Color.black; 239 this.repaint(); 240 } 241 242 private void histogramCalculation (){ 243 if (data == null){ 244 return; 245 } 246 if (data.length < this.histNumber){ 247 this.histNumber = data.length; 248 } 249 250 this.histogramArray = new double[this.histNumber]; 251 this.accumulativeFrequency = new double[this.histNumber]; 252 this.dataX = new double[this.histNumber]; 253 this.histRecords = new Vector[this.histNumber]; 254 this.histRecs = new Rectangle[this.histNumber]; 255 256 for (int i = 0; i < this.histNumber; i ++){ 257 histRecords[i] = new Vector(); 258 } 259 260 barWidth = (xAxisExtents[1] - xAxisExtents[0])/ (double)histNumber; 261 262 for (int i = 0; i < data.length; i ++){ 263 if (data[i]>=xAxisExtents[0]&&data[i]<=xAxisExtents[1]) { 264 int j=(int)Math.floor((data[i]-xAxisExtents[0])/barWidth); 265 j=((this.histNumber<=j) ? this.histNumber-1 :j); 266 this.histogramArray[j] ++; 267 this.histRecords[j].add(new Integer(i)); 268 } 269 } 270 271 for (int i = 0; i < this.histNumber; i++){ 272 dataX[i] = i * barWidth + xAxisExtents[0]; 273 if (i == 0){ 274 this.accumulativeFrequency[i] = this.histogramArray[i]; 275 }else{ 276 this.accumulativeFrequency[i] = this.accumulativeFrequency[i-1]+ 277 this.histogramArray[i]; 278 } 279 } 280 281 this.histArray = new DataArray(this.histogramArray); 282 this.yAxisExtents = histArray.getMaxMinCoorValue(); 283 } 284 285 286 public void paintComponent (Graphics g) { 287 g.setColor(background); 288 g.fillRect(0, 0, getSize().width, getSize().height); 289 g.setColor(foreground); 290 if (this.axisOn == true){ 291 drawAxis(g); 292 } 293 drawPlot(g); 294 if (this.selectionArray != null){ 295 drawSelection(g); 296 } 297 if (this.accFrequency == true){ 298 drawAccumulativeFrequency(g); 299 } 300 } 301 302 303 private void drawPlot (Graphics g) { 304 int len = this.histNumber; 305 for (int i = 0; i < len-1; i ++){ 306 g.drawRect(this.exsInt[i], this.whyInt[i], this.exsInt[i+1]-this.exsInt[i], this.plotOriginY - this.whyInt[i]); 307 g.setColor(Color.gray); 308 g.fillRect(this.exsInt[i]+1, this.whyInt[i]+1, this.exsInt[i+1]-this.exsInt[i]-1, this.plotOriginY - this.whyInt[i]-1); 309 g.setColor(foreground); 310 } 311 g.drawRect(this.exsInt[len-1], this.whyInt[len-1], this.plotEndX-this.exsInt[len-1], this.plotOriginY - this.whyInt[len-1]); 312 g.setColor(Color.gray); 313 g.fillRect(this.exsInt[len-1]+1, this.whyInt[len-1]+1, this.plotEndX-this.exsInt[len-1]-1, this.plotOriginY - this.whyInt[len-1]-1); 314 g.setColor(this.foreground); 315 } 316 317 private void setSelectionScreen(){ 318 this.selectionInt = new int[this.selectionArray.length]; 319 double scale; 320 scale = getScale(plotOriginY, plotEndY, yAxisExtents[0], yAxisExtents[1]); 321 this.selectionInt = getValueScreen(this.selectionArray, scale, plotOriginY, 0); 322 } 323 324 private void drawSelection (Graphics g){ 325 for (int i = 0; i < this.histNumber-1; i ++){ 326 if (this.selectionArray[i] > 0){ 327 g.drawRect(this.exsInt[i], this.selectionInt[i], this.exsInt[i+1]-this.exsInt[i], this.plotOriginY - this.selectionInt[i]); 328 g.setColor(Color.blue); 329 g.fillRect(this.exsInt[i]+1, this.selectionInt[i]+1, this.exsInt[i+1]-this.exsInt[i]-1, this.plotOriginY - this.selectionInt[i]-1); 330 g.setColor(foreground); 331 } 332 } 333 if (selectionArray[this.histNumber-1] > 0){ 334 g.drawRect(this.exsInt[this.histNumber-1], this.selectionInt[this.histNumber-1], this.plotEndX-this.exsInt[this.histNumber-1], this.plotOriginY - this.selectionInt[this.histNumber-1]); 335 g.setColor(Color.blue); 336 g.fillRect(this.exsInt[this.histNumber-1]+1, this.selectionInt[this.histNumber-1]+1, this.plotEndX-this.exsInt[this.histNumber-1]-1, this.plotOriginY - this.selectionInt[this.histNumber-1]-1); 337 g.setColor(this.foreground); 338 } 339 } 340 341 private void drawAxis (Graphics g) { 342 int plotWidth, plotHeight; 343 plotWidth = (int)this.getSize().getWidth(); 344 plotHeight = (int)this.getSize().getHeight(); 345 g.setColor(foreground); 346 g.drawLine(plotOriginX, plotEndY, plotOriginX, plotOriginY); 347 g.drawLine(plotOriginX, plotOriginY, plotEndX, plotOriginY); 348 // draw tick bars for scales on Y coordinate 349 int fontSize; 350 if (plotWidth < plotHeight){ 351 if (plotWidth < 300){ 352 fontSize = 9; 353 } else { 354 fontSize = (int)(plotWidth/32); 355 } 356 }else { 357 if (plotHeight < 300){ 358 fontSize = 9; 359 } else { 360 fontSize = (int)(plotHeight/32); 361 } 362 } 363 Font font = new Font("", Font.PLAIN, fontSize); 364 g.setFont(font); 365 //draw the labels on y axis (frequency). 366 String scaleStringY; 367 double barNumber = this.histArray.getTickNumber(); 368 double yBarDistance = ((plotOriginY - plotEndY)/barNumber); 369 //System.out.println("drawaxis: "+plotOriginY+" "+plotEndY+" "+yBarDistance+" "+barNumber); 370 for (int i = 0; i <= barNumber; i++) { 371 g.drawLine(plotOriginX - 3, plotEndY + (int)(i*yBarDistance), plotOriginX, 372 plotEndY + (int)(i*yBarDistance)); 373 if (Math.abs(this.histArray.getMajorTick()) <= 1) { 374 scaleStringY = Float.toString((float)(yAxisExtents[1] - i*this.histArray.getMajorTick())); 375 } 376 else { 377 scaleStringY = Integer.toString((int)(yAxisExtents[1] - i*this.histArray.getMajorTick())); 378 } 379 g.drawString(scaleStringY, plotOriginX - (int)(plotWidth*AXISSPACEPORTION/2), 380 plotEndY + (int)(i*yBarDistance + yBarDistance*1/6)); 381 } 382 //draw the labels on x axis. 383 //First tick. 384 String scaleStringX; 385 g.drawLine(plotOriginX, plotOriginY, plotOriginX, plotOriginY + 3); 386 if (Math.abs(xAxisExtents[0]) <= 1) { 387 scaleStringX = Float.toString((float)xAxisExtents[0]); 388 } else { 389 scaleStringX = Integer.toString((int)xAxisExtents[0]); 390 } 391 g.drawString(scaleStringX, plotOriginX - 3, plotOriginY + (int)(plotHeight*AXISSPACEPORTION/4)); 392 //Last tick. 393 g.drawLine(plotEndX, plotOriginY, plotEndX, plotOriginY + 3); 394 if (Math.abs(xAxisExtents[1]) <= 1) { 395 scaleStringX = Float.toString((float)xAxisExtents[1]); 396 } else { 397 scaleStringX = Integer.toString((int)xAxisExtents[1]); 398 } 399 g.drawString(scaleStringX, plotEndX - 8, plotOriginY + (int)(plotHeight*AXISSPACEPORTION/4)); 400 font = new Font("", Font.PLAIN, fontSize + 3); 401 g.setFont(font); 402 //draw X axis attribute string 403 g.drawString(this.variableName, plotOriginX + (plotEndX - plotOriginX)/2 - plotWidth/12, 404 plotOriginY + plotHeight/6 - 5); 405 //draw Y axis attribute string. Need rotation for drawing the string vertically. 406 Graphics2D g2d = (Graphics2D)g; 407 g2d.rotate(-Math.PI/2, plotOriginX - plotWidth/9, plotOriginY - (plotOriginY 408 - plotEndY)/3); 409 g2d.drawString("Frequency", plotOriginX - plotWidth/9, plotOriginY - (plotOriginY 410 - plotEndY)/3); 411 g2d.rotate(+Math.PI/2, plotOriginX - plotWidth/9, plotOriginY - (plotOriginY 412 - plotEndY)/3); 413 } 414 415 private void setAccumulativeFrequency(){ 416 this.accumulativeInt = new int[this.accumulativeFrequency.length]; 417 double scale; 418 scale = getScale(plotOriginY, plotEndY, 0, this.data.length); 419 accumulativeInt = getValueScreen(this.accumulativeFrequency, scale, plotOriginY, 0); 420 } 421 422 private void drawAccumulativeFrequency (Graphics g){ 423 int len = this.histNumber; 424 g.setColor(Color.blue); 425 Graphics2D g2d = (Graphics2D)g; 426 g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); 427 for (int i = 0; i < len-1; i ++){ 428 g.drawLine(this.exsInt[i], this.accumulativeInt[i], this.exsInt[i+1], this.accumulativeInt[i]); 429 } 430 g.drawLine(this.exsInt[len-1], this.accumulativeInt[len-1], this.plotEndX, this.accumulativeInt[len-1]); 431 g.drawLine(this.plotEndX, this.plotOriginY, this.plotEndX, this.plotEndY); 432 g2d.setStroke(new BasicStroke(1)); 433 //g.drawString((new Integer((int)(this.accumulativeFrequency[len-1]))).toString(), this.plotEndX + 1, this.plotEndY + 5); 434 g.drawString((new Integer((int)(this.accumulativeFrequency[len-1]))).toString(), this.plotEndX + 1, this.accumulativeInt[len-1]+5); 435 g.setColor(this.foreground); 436 } 437 438 public void componentHidden(ComponentEvent e) { 439 440 } 441 442 public void componentMoved(ComponentEvent e) { 443 444 } 445 446 public void componentResized(ComponentEvent e) { 447 //System.out.println("in component resized"); 448 this.setupDataforDisplay(); 449 this.setAccumulativeFrequency(); 450 this.repaint(); 451 } 452 453 public void componentShown(ComponentEvent e) { 454 } 455 456 /*** 457 * Calculate scale between real data and integer data for showing up on screen. 458 * @param min 459 * @param max 460 * @param dataMin 461 * @param dataMax 462 * @return scale 463 */ 464 private double getScale (int min, int max, double dataMin, double dataMax) { 465 double scale; 466 scale = (max - min)/(dataMax - dataMin); 467 return scale; 468 } 469 /*** 470 * Convert the single value to integer value worked on screen. 471 * @param data 472 * @param scale 473 * @param min 474 * @param dataMin 475 * @return valueScreen 476 */ 477 private int getValueScreen (double data, double scale, int min, double dataMin) { 478 int valueScreen; 479 if (Double.isNaN(data)) { 480 valueScreen = Integer.MIN_VALUE; 481 } 482 else { 483 valueScreen = (int)((data - dataMin)*scale + min); 484 } 485 return valueScreen; 486 } 487 /*** 488 * Convert the numeric values of observations to integer value worked on screen. 489 * @param dataArray 490 * @param scale 491 * @param min 492 * @param dataMin 493 * @return valueScreen 494 */ 495 private int[] getValueScreen (double[] dataArray, double scale, int min, double dataMin) { 496 int[] valueScreen = new int[dataArray.length]; 497 for (int i = 0; i < dataArray.length; i++) { 498 if (Double.isNaN(dataArray[i])) { 499 valueScreen[i] = Integer.MIN_VALUE; 500 } 501 else { 502 valueScreen[i] = (int)((dataArray[i] - dataMin)*scale + min); 503 } 504 } 505 return valueScreen; 506 } 507 508 private void setupDataforDisplay(){ 509 //System.out.println("In setup data for display ..." + xAxisExtents[0]); 510 if (axisOn){ 511 plotOriginX = (int)(this.getWidth()*AXISSPACEPORTION); 512 plotOriginY = (int)(this.getHeight()*(1 - AXISSPACEPORTION)); 513 plotEndX = (int)(this.getWidth()) - (int)(this.getWidth()*AXISSPACEPORTION/2); 514 plotEndY = (int)(this.getHeight()*AXISSPACEPORTION/2); 515 }else { 516 plotOriginX = 0; 517 plotOriginY = (int)(this.getSize().getHeight() - 2); 518 plotEndX = (int)(this.getSize().getWidth()) - 3; 519 plotEndY = 3; 520 } 521 int len = this.histNumber; 522 exsInt = new int[len]; 523 whyInt = new int[len]; 524 //get positions on screen 525 double scale; 526 scale = getScale(plotOriginX, plotEndX, xAxisExtents[0], xAxisExtents[1]); 527 exsInt = getValueScreen(dataX, scale, plotOriginX, xAxisExtents[0]); 528 exsStep = exsInt[1]-exsInt[0]; 529 scale = getScale(plotOriginY, plotEndY, yAxisExtents[0], yAxisExtents[1]); 530 whyInt = getValueScreen(this.histogramArray, scale, plotOriginY, yAxisExtents[0]); 531 //System.out.println("setupdisplay: "+plotOriginY+" "+plotEndY+" "+scale); 532 for (int i = 0; i < this.histNumber -1; i ++){ 533 this.histRecs[i] = new Rectangle (this.exsInt[i], this.whyInt[i], 534 this.exsInt[i+1]-this.exsInt[i], this.plotOriginY - this.whyInt[i]); 535 } 536 this.histRecs[this.histNumber-1] = new Rectangle (this.exsInt[len-1], 537 this.whyInt[len-1], this.plotEndX-this.exsInt[len-1], this.plotOriginY - this.whyInt[len-1]); 538 } 539 /*** 540 * New data ranges setup dialog. 541 * @param x 542 * @param y 543 */ 544 private void showDialog1 (int x, int y) { 545 546 if (this.dialog1 == null){ 547 JFrame dummyFrame = new JFrame(); 548 dialog1 = new JDialog(dummyFrame, "Data Range Configuer", true); 549 JButton actionButton; 550 JButton resetButton; 551 dialog1.setLocation(x, y); 552 dialog1.setSize(300, 100); 553 dialog1.getContentPane().setLayout(new GridLayout(4, 2)); 554 //create buttons for action 555 actionButton = new JButton("Apply"); 556 actionButton.addActionListener(new java.awt.event.ActionListener() { 557 558 /*** 559 * Button to set up new data ranges shown up in scatter plot. 560 * @param e 561 */ 562 public void actionPerformed (ActionEvent e) { 563 try { 564 actionButton_actionPerformed(e); 565 } catch (Exception exception) {} 566 } 567 }); 568 resetButton = new JButton("Reset"); 569 resetButton.addActionListener(new java.awt.event.ActionListener() { 570 571 /*** 572 * put your documentation comment here 573 * @param e 574 */ 575 public void actionPerformed (ActionEvent e) { 576 resetButton_actionPerformed(e); 577 } 578 }); 579 histNumberField.setText(Integer.toString(this.histNumber)); 580 this.xAxisMinField.setText(Double.toString(this.xAxisExtents[0])); 581 this.xAxisMaxField.setText(Double.toString(this.xAxisExtents[1])); 582 dialog1.getContentPane().add(new JLabel(("Histogram Number"))); 583 dialog1.getContentPane().add(this.histNumberField); 584 dialog1.getContentPane().add(new JLabel(("DataRange Min"))); 585 dialog1.getContentPane().add(this.xAxisMinField); 586 dialog1.getContentPane().add(new JLabel(("DataRange Max"))); 587 dialog1.getContentPane().add(this.xAxisMaxField); 588 dialog1.getContentPane().add(actionButton); 589 dialog1.getContentPane().add(resetButton); 590 } 591 dialog1.show(); 592 } 593 594 /*** 595 * Set up new data ranges to show. 596 * @param e 597 */ 598 private void actionButton_actionPerformed (ActionEvent e) { 599 //get the input data from text field 600 this.histNumber = Integer.parseInt(histNumberField.getText()); 601 xAxisExtents[0] = Double.parseDouble(xAxisMinField.getText()); 602 xAxisExtents[1] = Double.parseDouble(xAxisMaxField.getText()); 603 this.histogramCalculation (); 604 this.setupDataforDisplay(); 605 this.setAccumulativeFrequency(); 606 //fireActionPerformed(COMMAND_DATARANGE_SET); 607 //System.out.println("ok, fire event."); 608 repaint(); 609 dialog1.hide(); 610 } 611 612 /*** 613 * put your documentation comment here 614 * @param e 615 */ 616 private void resetButton_actionPerformed (ActionEvent e) { 617 this.histNumber = this.DEFAULT_HIST_NUM; 618 this.xAxisExtents = (double[])dataArray.getExtent().clone(); 619 //yAxisExtents = (double[])this.histArray.getMaxMinCoorValue().clone(); 620 621 histNumberField.setText(Integer.toString(this.histNumber)); 622 xAxisMinField.setText(Double.toString(xAxisExtents[0])); 623 xAxisMaxField.setText(Double.toString(xAxisExtents[1])); 624 this.histogramCalculation (); 625 this.setupDataforDisplay(); 626 this.setAccumulativeFrequency(); 627 //fireActionPerformed(COMMAND_DATARANGE_SET); 628 repaint(); 629 dialog1.hide(); 630 } 631 632 /*** 633 * New data ranges setup dialog. 634 * @param x 635 * @param y 636 */ 637 private void showDialog2 (int x, int y) { 638 JFrame dummyFrame = new JFrame(); 639 dialog2 = new JDialog(dummyFrame, "Frequency Range Configuer", true); 640 JButton actionButton; 641 JButton resetButton; 642 dialog2.setLocation(x, y); 643 dialog2.setSize(300, 100); 644 dialog2.getContentPane().setLayout(new GridLayout(3, 2)); 645 yAxisMinField.setText(Double.toString(yAxisExtents[0])); 646 yAxisMaxField.setText(Double.toString(yAxisExtents[1])); 647 //create buttons for action 648 actionButton = new JButton("Apply"); 649 actionButton.addActionListener(new java.awt.event.ActionListener() { 650 651 /*** 652 * Button to set up new data ranges shown up in scatter plot. 653 * @param e 654 */ 655 public void actionPerformed (ActionEvent e) { 656 try { 657 actionButton2_actionPerformed(e); 658 } catch (Exception exception) {} 659 } 660 }); 661 resetButton = new JButton("Reset"); 662 resetButton.addActionListener(new java.awt.event.ActionListener() { 663 664 /*** 665 * put your documentation comment here 666 * @param e 667 */ 668 public void actionPerformed (ActionEvent e) { 669 resetButton2_actionPerformed(e); 670 } 671 }); 672 dialog2.getContentPane().add(new JLabel(("Frequency" + " Min"))); 673 dialog2.getContentPane().add(yAxisMinField); 674 dialog2.getContentPane().add(new JLabel(("Frequency" + " Max"))); 675 dialog2.getContentPane().add(yAxisMaxField); 676 dialog2.getContentPane().add(actionButton); 677 dialog2.getContentPane().add(resetButton); 678 dialog2.show(); 679 } 680 681 /*** 682 * Set up new data ranges to show. 683 * @param e 684 */ 685 private void actionButton2_actionPerformed (ActionEvent e) { 686 //get the input data from text field 687 yAxisExtents[0] = Double.parseDouble(yAxisMinField.getText()); 688 yAxisExtents[1] = Double.parseDouble(yAxisMaxField.getText()); 689 this.histArray.setExtent(this.yAxisExtents); 690 this.setupDataforDisplay(); 691 //fireActionPerformed(COMMAND_DATARANGE_SET); 692 //System.out.println("ok, fire event."); 693 repaint(); 694 dialog2.hide(); 695 } 696 697 /*** 698 * put your documentation comment here 699 * @param e 700 */ 701 private void resetButton2_actionPerformed (ActionEvent e) { 702 this.histArray.setDataExtent(); 703 yAxisExtents = (double[])this.histArray.getMaxMinCoorValue().clone(); 704 this.histogramCalculation (); 705 yAxisMinField.setText(Double.toString(yAxisExtents[0])); 706 yAxisMaxField.setText(Double.toString(yAxisExtents[1])); 707 this.setupDataforDisplay(); 708 //fireActionPerformed(COMMAND_DATARANGE_SET); 709 repaint(); 710 dialog2.hide(); 711 } 712 /*** 713 * put your documentation comment here 714 * @param e 715 */ 716 private void maybeShowPopup (MouseEvent e) { 717 if (e.isPopupTrigger()) { 718 popup.show(e.getComponent(), e.getX(), e.getY()); 719 } 720 } 721 722 public void mouseClicked(MouseEvent e){ 723 int count = e.getClickCount(); 724 //With shift pressed, it will continue to select. 725 if (!(e.isShiftDown())){ 726 // this.selRecords.clear(); 727 for (int i = 0; i < this.selectedRecords.length; i ++){ 728 this.selectedRecords[i] = 0; 729 } 730 731 if (this.selectionArray == null || this.selectionArray.length != this.histNumber){ 732 this.selectionArray = new double[this.histNumber]; 733 } else { 734 for (int i = 0; i < this.selectionArray.length; i ++){ 735 this.selectionArray[i] = 0; 736 } 737 } 738 } 739 int[] mousePos = new int[2]; 740 mousePos[0] = e.getX(); 741 mousePos[1] = e.getY(); 742 //single click, select performed. 743 if (count == 1) { 744 for (int i = 0; i < this.histNumber; i ++){ 745 if (this.histRecs[i].contains(mousePos[0],mousePos[1])){ 746 for (int j = 0; j < this.histRecords[i].size(); j ++){ 747 // this.selRecords.add(this.histRecords[i].get(j)); 748 this.selectedRecords[((Integer)this.histRecords[i].get(j)).intValue()] = 1; 749 //this.selectionArray[i] ++; 750 } 751 this.setSelections(this.selectedRecords); 752 this.setSelectionScreen(); 753 // this.selRecords.trimToSize(); 754 repaint(); 755 fireActionPerformed (); 756 continue; 757 } 758 } 759 } 760 } 761 762 public void mousePressed(MouseEvent e){ 763 if (e.isPopupTrigger()) 764 maybeShowPopup(e); 765 } 766 767 public void mouseReleased(MouseEvent e){ 768 if (e.isPopupTrigger()){ 769 maybeShowPopup(e); 770 } 771 } 772 773 public void mouseEntered(MouseEvent e){ 774 } 775 776 public void mouseExited(MouseEvent e){ 777 } 778 779 /*** 780 * adds an ActionListener to the button 781 */ 782 public void addActionListener (ActionListener l) { 783 listenerListAction.add(ActionListener.class, l); 784 } 785 786 /*** 787 * removes an ActionListener from the button 788 */ 789 public void removeActionListener (ActionListener l) { 790 listenerListAction.remove(ActionListener.class, l); 791 } 792 793 /*** 794 * Notify all listeners that have registered interest for 795 * notification on this event type. The event instance 796 * is lazily created using the parameters passed into 797 * the fire method. 798 * @see EventListenerList 799 */ 800 public void fireActionPerformed () { 801 // Guaranteed to return a non-null array 802 Object[] listeners = listenerListAction.getListenerList(); 803 // Process the listeners last to first, notifying 804 // those that are interested in this event 805 ActionEvent e2 = new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"OK"); 806 for (int i = listeners.length - 2; i >= 0; i -= 2) { 807 if (listeners[i] == ActionListener.class) { 808 // Lazily create the event: 809 ((ActionListener)listeners[i + 1]).actionPerformed(e2); 810 } 811 } 812 } 813 }

This page was automatically generated by Maven