Author: scooter
Date: 2009-08-08 22:43:43 -0700 (Sat, 08 Aug 2009)
New Revision: 17753
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/HistoChangeListener.java
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/Histogram.java
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/HistogramDialog.java
Log:
Updates -- X scaling is still not right
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/HistoChangeListener.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/HistoChangeListener.java
2009-08-09 04:00:43 UTC (rev 17752)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/HistoChangeListener.java
2009-08-09 05:43:43 UTC (rev 17753)
@@ -5,6 +5,7 @@
* @author
* @version 1.00 2009/7/14
*/
+
package clusterMaker.ui;
public interface HistoChangeListener {
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/Histogram.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/Histogram.java
2009-08-09 04:00:43 UTC (rev 17752)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/Histogram.java
2009-08-09 05:43:43 UTC (rev 17753)
@@ -1,270 +1,380 @@
-/**
- * @(#)Histogram.java
- *
- * Java class that implements a Histogram
- *
- * @author
- * @version 1.00 2009/6/30
- */
-
-package clusterMaker.ui;
-
-import java.awt.Color;
-import java.awt.Dimension;
-import java.awt.Graphics;
-import java.awt.event.*;
-
-import java.lang.Math;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import java.text.DecimalFormat;
-
-import javax.swing.JComponent;
-
-class Histogram extends JComponent implements MouseMotionListener,
MouseListener{
- private int[] histoArray;
- private int histoMax = Integer.MIN_VALUE;
- private double[] graphData;
- private double minValue = Double.MAX_VALUE;
- private double maxValue = Double.MIN_VALUE;
- private int low;
- private int high;
- private final int XSTART = 100;
- private final int YEND = 50;
- private final int NBINS = 100;
- private final int NDIVS = 100;
- private int mouseX;
- private boolean boolShowLine = false;
- private double binSize;
- private List<HistoChangeListener> listeners = null;
-
-
- DecimalFormat form = new DecimalFormat("0.0##E0"); //rounds values for
drawString
-
- Histogram(double[] inputData, int nBins) {
- super();
- setPreferredSize(new Dimension(800,500));
- histoArray = new int[NBINS];
- this.graphData = inputData;
- for (int i=0; i < graphData.length; i++) {
- minValue = Math.min(minValue, graphData[i]);
- maxValue = Math.max(maxValue, graphData[i]);
- }
- // Adjust min and max for esthetic purposes
-
-
-
- listeners = new ArrayList();
- createHistogram(graphData);
- addMouseMotionListener(this);
- addMouseListener(this);
- }
-
- public void updateData(double[] graphData) {
- // Trigger redraw
- }
-
- public void paint(Graphics g) {
- super.paint(g);
- drawGraph(g);
- if(boolShowLine)
- mouseLine(mouseX, g);
- }
-
- public void mouseMoved(MouseEvent e) {}
- public void mouseDragged(MouseEvent e) {
- Dimension dim = getPreferredSize();
- int width = dim.width;
- int xIncrement = (width-125)/NBINS;
- int histoMousePos = (e.getX()-XSTART)/xIncrement;
-
- if(e.getX()>XSTART && boolShowLine){
- mouseX = e.getX();
- repaint();
- }
- }
- public void mouseClicked(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- public void mousePressed(MouseEvent e){
- Dimension dim = getPreferredSize();
- int width = dim.width;
- int xIncrement = (width-125)/NBINS;
- int histoMousePos = (e.getX()-XSTART)/xIncrement;
-
- if(e.getX()>XSTART && boolShowLine){
- mouseX = e.getX();
- repaint();
- }
- }
-
- public void mouseReleased(MouseEvent e){
- Dimension dim = getPreferredSize();
- int width = dim.width;
- int xIncrement = (width-125)/NBINS;
- int histoMousePos = (e.getX()-XSTART)/xIncrement;
- if(e.getX()>XSTART &&
e.getX()<(XSTART+xIncrement*histoArray.length) && boolShowLine){
- double binValue = minValue+(binSize*histoMousePos);
- // System.out.println("histoArray["+histoMousePos+"] =
"+ histoArray[histoMousePos]+", "+form.format((binValue)));
- if (listeners.size() == 0) return;
- for (HistoChangeListener listener: listeners)
- listener.histoValueChanged(binValue);
- }
-
- }
-
- //handle the rest of the wizard buttons in a similar fashion
-
-
-
- public void setBoolShowLine(boolean inShowLine){boolShowLine =
inShowLine;}
-
- /**
- * Add a new change listener to this histogram
- *
- * @param listener the HistoChangeListener to call when the cutoff
value changes
- */
- public void addHistoChangeListener(HistoChangeListener listener) {
- if (listeners.contains(listener)) return;
- listeners.add(listener);
- }
-
- /**
- * Remove a change listener from this histogram
- *
- * @param listener the HistoChangeListener to remove
- */
- public void removeHistoChangeListener(HistoChangeListener listener) {
- listeners.remove(listener);
- }
-
- private void mouseLine(int mX, Graphics g){
- Dimension dim = getPreferredSize();
- int height = dim.height-100;
- int width = dim.width;
- int xIncrement = (width-125)/NBINS;
- int histoMousePos = (mX-XSTART)/xIncrement;
- if(histoMousePos >= histoArray.length)
- histoMousePos = histoArray.length-1;
-
- g.setColor(Color.red);
- g.drawLine(mX, YEND, mX, height);
- g.setColor(Color.black);
- g.drawString(form.format((minValue+(binSize*histoMousePos)))+"
("+histoArray[histoMousePos]+" values)",mX-50,YEND-5); //REMOVE THIS LATER
- }
-
- private void createHistogram(double[] inputData){
- histoMax = Integer.MIN_VALUE;
- binSize = (maxValue - minValue)/NBINS;
- // System.out.println("binSize = "+binSize);
- for(double dataItr : inputData){
- for(int nI=0; nI < NBINS; nI++){
- if(dataItr==minValue){
- histoArray[0]+=1;
- break;
- }
- if(dataItr>minValue+binSize*nI &&
dataItr<=minValue+binSize*(nI+1) ){
- histoArray[nI]+=1;
- break;
- }
- }
- }
- int test = 0; //REMOVE THIS LATER
- for(int nI=0; nI<histoArray.length; nI++){
- histoMax = Math.max(histoMax, histoArray[nI]);
- // System.out.println("hitoArray["+nI+"] =
"+histoArray[nI]); //REMOVE THIS LATER
- test+=histoArray[nI]; //REMOVE THIS LATER
- }
-
- // Adjust histomax for esthetics
- int histoMax2 = (int)Math.pow(10,
Math.rint(Math.log10(histoMax)+.5));
- if (histoMax < histoMax2/2)
- histoMax = histoMax2/2;
- else
- histoMax = histoMax2;
- // System.out.println("test = "+test); //REMOVE THIS LATER
- // System.out.println("histoMax = "+histoMax); //REMOVE THIS
LATER
- }
-
- private void drawGraph(Graphics g){
- Dimension dim = getPreferredSize();
- int height = dim.height-100;
- int width = dim.width;
-
- drawAxes(g, height, width);
- drawLabels(g, height, width);
- drawData(g, height, width);
- }
-
- private void drawAxes(Graphics g, int height, int width) {
- double yIncrement = height/NDIVS;
- int xIncrement = (width-125)/NBINS;
-
- int chartWidth = histoArray.length*xIncrement+100;
-
- g.setColor(Color.black);
- g.drawLine(XSTART,YEND,XSTART,height);
- g.setColor(Color.green);
- g.drawLine(XSTART,height,chartWidth,height);
-
-
- for(int nI=1;nI<=NDIVS;nI++){
- if(nI%10==0)
- g.setColor(Color.red);
- else
- g.setColor(Color.gray);
-
g.drawLine(XSTART,height-(int)(yIncrement*nI),chartWidth,
height-(int)(yIncrement*nI));
- }
-
- for(int nI=0; nI<=histoArray.length; nI++){
- if(nI%10==0){
- g.setColor(Color.black);
-
g.drawLine(XSTART+xIncrement*nI,height,100+xIncrement*nI,height+10);
- }
-
- }
- }
-
- private void drawLabels(Graphics g, int height, int width) {
- double yIncrement = height/NDIVS;
- int xIncrement = (width-125)/NBINS;
- double binSize = (maxValue - minValue)/NBINS;
-
- g.setColor(Color.gray);
- // Handle esthetics. This doesn't work for 1,000's of values
- for(int nI=1;nI<=100;nI++){
- if(nI%10==0)
-
g.drawString(""+nI*histoMax/100,20,height-(int)(yIncrement*nI)+5);
- }
-
- g.drawString(""+form.format(minValue),XSTART-25,height+20);
-
g.drawString(""+form.format(maxValue),XSTART-25+xIncrement*histoArray.length,height+20);
- for(int nI=1; nI<histoArray.length; nI++){
- if(nI%10==0)
-
g.drawString(""+form.format((minValue+(binSize*nI))),XSTART-20+xIncrement*nI,height+20);
- }
- }
-
-
- private void drawData(Graphics g, int height, int width){
- int nBlueChange = 100;
- double yIncrement = (height-50)/(double)histoMax;
- //System.out.println("yIncrement = "+yIncrement);
- int xIncrement = (width-125)/NBINS;
-
- for(int nI=0; nI<histoArray.length; nI++){
- g.setColor(new Color(0,0,nBlueChange));
- g.fillRect(XSTART+xIncrement*nI,
height-(int)(histoArray[nI]*yIncrement), xIncrement,
(int)(histoArray[nI]*yIncrement));
- g.setColor(Color.black);
- g.drawRect(XSTART+xIncrement*nI,
height-(int)(histoArray[nI]*yIncrement), xIncrement,
(int)(histoArray[nI]*yIncrement));
-
- nBlueChange+=15;
- if(nBlueChange >= 250)
- nBlueChange = 100;
- }
-
- }
-
-}
+ /**
+ * @(#)Histogram.java
+ *
+ * Java class that implements a Histogram
+ *
+ * @author
+ * @version 1.00 2009/6/30
+ */
+
+package clusterMaker.ui;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.event.*;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.font.TextAttribute;
+import java.text.AttributedString;
+import java.text.AttributedCharacterIterator;
+
+import java.lang.Math;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import java.text.DecimalFormat;
+
+import javax.swing.JComponent;
+
+class Histogram extends JComponent implements MouseMotionListener,
MouseListener{
+
+ // The histogram
+ private int[] histoArray;
+
+ // Original data
+ private double[] graphData;
+
+ // Y scale values
+ private int histoMax = Integer.MIN_VALUE;
+ private int histoMin = 0;
+ private int histoMaxUp;
+
+ // X scale values
+ private double minValue = Double.MAX_VALUE;
+ private double maxValue = Double.MIN_VALUE;
+ private int low;
+ private int high;
+
+ private final int XSTART = 100;
+ private final int YEND = 50;
+ private final int NBINS;
+ private int mouseX;
+ private boolean boolShowLine = false;
+ private List<HistoChangeListener> listeners = null;
+ private Font adjSizeFont;
+ private int fontSize;
+ private double xInterval;
+
+ DecimalFormat form = new DecimalFormat("0.0E0"); //rounds values for
drawString
+
+ Histogram(double[] inputData, int nBins) {
+ super();
+ NBINS = nBins;
+ setPreferredSize(new Dimension(1000,400));
+ histoArray = new int[NBINS];
+ this.graphData = inputData;
+ listeners = new ArrayList();
+
+ createHistogram(graphData);
+
+ addMouseMotionListener(this);
+ addMouseListener(this);
+ fontSize = 5+ (int)(this.getPreferredSize().getWidth()/(NBINS));
+ if(fontSize>18)
+ fontSize=18;
+ adjSizeFont = new Font("Helvetica", Font.PLAIN, fontSize);
+ // System.out.println("fontSize = "+fontSize);
+ }
+
+ public void updateData(double[] graphData) {
+ // Trigger redraw
+ histoArray = new int[NBINS];
+ this.graphData = graphData;
+
+ createHistogram(graphData);
+ }
+
+ public void paint(Graphics g) {
+ super.paint(g);
+ fontSize = 3 +(int)(this.getPreferredSize().getWidth()/(NBINS));
+ if(fontSize>18)
+ fontSize=18;
+
+ adjSizeFont = new Font("Helvetica", Font.PLAIN, fontSize);
+ System.out.println("fontSize = "+fontSize);
+ drawGraph(g);
+ if(boolShowLine)
+ mouseLine(mouseX, g);
+ }
+
+ public void mouseMoved(MouseEvent e) {}
+ public void mouseDragged(MouseEvent e) {
+ Dimension dim = getPreferredSize();
+ int width = dim.width;
+ int xIncrement = (width-125)/NBINS;
+ int histoMousePos = (e.getX()-XSTART)/xIncrement;
+
+ if(e.getX()>XSTART && boolShowLine){
+ mouseX = e.getX();
+ repaint();
+ }
+ }
+ public void mouseClicked(MouseEvent e){}
+ public void mouseEntered(MouseEvent e){}
+ public void mouseExited(MouseEvent e){}
+ public void mousePressed(MouseEvent e){
+ Dimension dim = getPreferredSize();
+ int width = dim.width;
+ int xIncrement = (width-125)/NBINS;
+ int histoMousePos = (e.getX()-XSTART)/xIncrement;
+
+ if(e.getX()>XSTART && boolShowLine){
+ mouseX = e.getX();
+ repaint();
+ }
+ }
+
+ public void mouseReleased(MouseEvent e){
+ Dimension dim = getPreferredSize();
+ int width = dim.width;
+ int xIncrement = (width-125)/NBINS;
+ int histoMousePos = (e.getX()-XSTART)/xIncrement;
+ if(e.getX()>XSTART &&
e.getX()<(XSTART+xIncrement*histoArray.length) && boolShowLine){
+ double binValue = minValue+(xInterval*histoMousePos);
+ // System.out.println("histoArray["+histoMousePos+"] =
"+ histoArray[histoMousePos]+", "+form.format((binValue)));
+ if (listeners.size() == 0) return;
+ for (HistoChangeListener listener: listeners)
+ listener.histoValueChanged(binValue);
+ }
+
+ }
+
+ public void setBoolShowLine(boolean inShowLine){boolShowLine =
inShowLine;}
+
+ /**
+ * Add a new change listener to this histogram
+ *
+ * @param listener the HistoChangeListener to call when the cutoff
value changes
+ */
+ public void addHistoChangeListener(HistoChangeListener listener) {
+ if (listeners.contains(listener)) return;
+ listeners.add(listener);
+ }
+
+ /**
+ * Remove a change listener from this histogram
+ *
+ * @param listener the HistoChangeListener to remove
+ */
+ public void removeHistoChangeListener(HistoChangeListener listener) {
+ listeners.remove(listener);
+ }
+
+
+ private void mouseLine(int mX, Graphics g){
+ Dimension dim = getPreferredSize();
+ int height = dim.height-100;
+ int width = dim.width;
+ int xIncrement = (width-125)/NBINS;
+ int histoMousePos = (mX-XSTART)/xIncrement;
+ if(histoMousePos >= histoArray.length)
+ histoMousePos = histoArray.length-1;
+
+ g.setColor(Color.red);
+ g.drawLine(mX, YEND, mX, height);
+ g.setColor(Color.black);
+ g.setFont(adjSizeFont);
+
g.drawString(toSciNotation(form.format((minValue+(xInterval*histoMousePos))).toString(),"
("+histoArray[histoMousePos]+" values)"),mX-50,YEND-5);
+ }
+
+ private void createHistogram(double[] inputData){
+ calculateXScale();
+
+ // Bin the data
+ for(double dataItr : inputData){
+ for(int nI=0; nI < NBINS; nI++){
+ if(dataItr==minValue){
+ histoArray[0]+=1;
+ break;
+ }
+ if(dataItr>minValue+xInterval*nI &&
dataItr<=minValue+xInterval*(nI+1) ){
+ histoArray[nI]+=1;
+ break;
+ }
+ }
+ }
+ calculateYScale();
+ }
+
+ private void calculateXScale() {
+
+ // Calculate our minimum and maximum X values
+ for (int i=0; i < graphData.length; i++) {
+ minValue = Math.min(minValue, graphData[i]);
+ maxValue = Math.max(maxValue, graphData[i]);
+ }
+
+ System.out.println("range = "+minValue+" - "+maxValue);
+
+ // Calculate our X scale
+ double range = maxValue - minValue;
+ double oomRange = Math.log10(range); //order of magnitude
+ // System.out.println("oomRange = "+oomRange);
+ oomRange = oomRange + (.5*oomRange/Math.abs(oomRange)); //
Increase our oom by .5
+ // System.out.println("oomRange = "+oomRange);
+ oomRange = (int)(oomRange); //make it an integer
+
+ double high = (Math.rint((maxValue/Math.pow(10, oomRange))+.5))
* (Math.pow(10, oomRange)); // This is our initial high value
+
+ // System.out.println("high = "+high);
+ if (maxValue <= high/2)
+ high = high/2; // A little fine-tuning
+
+ double low = (Math.rint((minValue/Math.pow(10, oomRange))-.5))
* Math.pow(10,oomRange);
+
+ if (minValue >= low/2)
+ low = low/2;
+
+ xInterval = (high - low) / NBINS;
+
+ }
+
+ private void calculateYScale() {
+ histoMin = 0;
+
+ // First, determine the max value
+ for(int nI=0; nI<histoArray.length; nI++){
+ histoMax = Math.max(histoMax, histoArray[nI]);
+ }
+
+ while(histoMax > histoMaxUp)
+ histoMaxUp +=
(int)(Math.pow(10,(int)(Math.log10(histoMax))));
+
+ if(histoMaxUp<10)
+ histoMaxUp = 10;
+ }
+
+ private void drawGraph(Graphics g){
+ Dimension dim = getPreferredSize();
+ int height = dim.height-100;
+ int width = dim.width;
+
+ drawAxes(g, height, width);
+ drawLabels(g, height, width);
+ drawData(g, height, width);
+ }
+
+ private void drawAxes(Graphics g, int height, int width) {
+
+ int xIncrement = (width-125)/NBINS;
+ int maxX = xIncrement*NBINS+XSTART;
+
+ // Draw the Y axis
+ g.setColor(Color.black);
+ g.drawLine(XSTART,YEND,XSTART,height);
+
+ // Draw the X axis
+ g.drawLine(XSTART,height,maxX,height);
+
+ // Draw the Y incremental lines
+ double yIncrement = (height-YEND)/(double)histoMaxUp;
+ for(int nI=1;nI<=histoMaxUp;nI++){
+ if(((double)nI%((double)histoMaxUp/10.0)) == 0.0){
+ g.setColor(Color.red);
+
g.drawLine(XSTART-5,(int)(height-(yIncrement*nI)),maxX,(int)(height-(yIncrement*nI)));
+ }
+ else if(((double)nI%((double)histoMaxUp/20.0)) == 0.0){
+ g.setColor(Color.gray);
+
g.drawLine(XSTART,(int)(height-(yIncrement*nI)),maxX,(int)(height-(yIncrement*nI)));
+ }
+ }
+
+ g.setColor(Color.black);
+ for(int nI=0; nI<=NBINS; nI++){
+ if(nI%10==0){
+
g.drawLine(XSTART+xIncrement*nI,height,XSTART+xIncrement*nI,height+10);
+ }
+ }
+ }
+
+ private void drawLabels(Graphics g, int height, int width) {
+ g.setColor(Color.black);
+ g.setFont(adjSizeFont);
+ FontMetrics metrics = g.getFontMetrics();
+
+ // Draw the Y labels
+ double yIncrement = (height-YEND)/(double)histoMaxUp;
+ for(int nI=1;nI<=histoMaxUp;nI++){
+ String str = ""+nI;
+ int offset = 90-metrics.stringWidth(str);
+
+ if(nI%(histoMaxUp/10)==0)
+ g.drawString(str, offset,
height-(int)(yIncrement*nI)+5);
+ }
+
+ // Now draw the X labels
+ int xIncrement = (width-125)/NBINS;
+ for(int nI=0; nI<=NBINS; nI++){
+ double value = low+(xInterval*nI);
+ String str = form.format(value);
+ int offset = XSTART+metrics.stringWidth(str)/2 - 50;
+ if (value == 0 || (value > 1 && value < 10))
+ offset += 20;
+
+ if(nI%20==0)
+ g.drawString(toSciNotation(str,
""),offset+xIncrement*nI,height+25);
+ if(nI%20==10)
+ g.drawString(toSciNotation(str,
""),offset+xIncrement*nI,height+30);
+ }
+ }
+
+
+ // TODO: Change this method to use height and width. You may need to
scale the
+ // the font also.
+ private void drawData(Graphics g, int height, int width){
+ int nBlueChange = 100;
+ double yIncrement = (height-50)/(double)(histoMaxUp);
+ //System.out.println("yIncrement = "+yIncrement);
+ int xIncrement = (width-125)/NBINS;
+ double xValue = low;
+ int histoIndex = 0;
+
+ for(int nI=0; nI<=NBINS; nI++){
+ if (xValue >= minValue) {
+ g.setColor(new Color(0,0,nBlueChange));
+ g.fillRect(XSTART+xIncrement*nI,
(int)(height-(histoArray[histoIndex]*yIncrement)), xIncrement,
(int)(histoArray[histoIndex]*yIncrement));
+ g.setColor(Color.black);
+ g.drawRect(XSTART+xIncrement*nI,
(int)(height-(histoArray[histoIndex]*yIncrement)), xIncrement,
(int)(histoArray[histoIndex]*yIncrement));
+ histoIndex++;
+
+ nBlueChange+=15;
+ if(nBlueChange >= 250)
+ nBlueChange = 100;
+ }
+ xValue += xInterval;
+ }
+ }
+
+ private AttributedCharacterIterator toSciNotation(String d, String
suffix){
+ String returnString = "";
+ for(int i=0; i<d.length(); i++){
+ if(d.charAt(i)== 'E')
+ break;
+ returnString+=d.charAt(i);
+ }
+
+ String exponent = "";
+ for(int i=d.length()-1; i>0; i--){
+ if(d.charAt(i)== 'E')
+ exponent+=d.substring(i+1,d.length());
+ }
+
+ AttributedString str;
+ if (Integer.parseInt(exponent) == 0) {
+ str = new AttributedString(returnString+suffix);
+ } else {
+ returnString += "x10";
+ int superOffset = returnString.length();
+ returnString += exponent;
+ int superEnd = returnString.length();
+
+ str = new AttributedString(returnString+suffix);
+ str.addAttribute(TextAttribute.SUPERSCRIPT,
TextAttribute.SUPERSCRIPT_SUPER, superOffset, superEnd);
+ }
+
+ str.addAttribute(TextAttribute.FONT, adjSizeFont, 0,
returnString.length());
+ return str.getIterator();
+ }
+}
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/HistogramDialog.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/HistogramDialog.java
2009-08-09 04:00:43 UTC (rev 17752)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/ui/HistogramDialog.java
2009-08-09 05:43:43 UTC (rev 17753)
@@ -1,107 +1,209 @@
-/**
- * @(#)HistogramDialog.java
- *
- * A JDialog component that displays a histogram. This
- *
- * @author
- * @version
- */
-package clusterMaker.ui;
-
-import java.awt.Dimension;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.ComponentEvent;
-import java.awt.event.ComponentListener;
-
-import javax.swing.BorderFactory;
-import javax.swing.BoxLayout;
-import javax.swing.JButton;
-import javax.swing.JDialog;
-import javax.swing.JPanel;
-
-import javax.swing.border.Border;
-import javax.swing.border.EtchedBorder;
-
-
-public class HistogramDialog extends JDialog implements ActionListener,
ComponentListener{
- double[] inputArray;
- int nBins;
- Histogram histo;
-
- public HistogramDialog(String title, double[] inputArray, int nBins) {
- super();
- this.inputArray = inputArray;
- this.nBins = nBins;
- setTitle(title);
-
- initializeOnce();
- }
-
- //public double getCutoff() {}
-
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand().equals("set"))
- histo.setBoolShowLine(true);
- if(e.getActionCommand().equals("close"))
- this.dispose();
- }
-
- public void componentHidden(ComponentEvent e) {}
- public void componentShown(ComponentEvent e) {}
- public void componentMoved(ComponentEvent e) {}
-
- public void componentResized(ComponentEvent e) {
- // Get our new size & update histogram
- Dimension dim = e.getComponent().getSize();
- histo.setPreferredSize(new Dimension(dim.width, dim.height));
- }
-
- private void initializeOnce() {
-
- JPanel mainPanel = new JPanel();
- mainPanel.setLayout(new BoxLayout(mainPanel,
BoxLayout.PAGE_AXIS));
- mainPanel.addComponentListener(this);
-
-
- // Create and add the histogram component
- histo = new Histogram(inputArray, nBins);
- mainPanel.add(histo);
-
- // TODO: Add box to set lower and upper bounds. Look at JText
and JLabel
-
- // Create our button box
- JPanel buttonBox = new JPanel();
-
- // Close button
- JButton closeButton = new JButton("close");
- closeButton.addActionListener(this);
- closeButton.setActionCommand("close");
-
- // OK button
- JButton okButton = new JButton("Set Cutoff");
- okButton.addActionListener(this);
- okButton.setActionCommand("set");
-
- buttonBox.add(okButton);
- buttonBox.add(closeButton);
-
buttonBox.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
- Dimension buttonDim = buttonBox.getPreferredSize();
- buttonBox.setMinimumSize(buttonDim);
- buttonBox.setMaximumSize(new Dimension(Integer.MAX_VALUE,
buttonDim.height));
- mainPanel.add(buttonBox);
-
- setContentPane(mainPanel);
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
-
- }
-
- public void addHistoChangeListener(HistoChangeListener h){
- histo.addHistoChangeListener(h);
- }
-
- public void removeHistoChangeListener(HistoChangeListener h){
- histo.removeHistoChangeListener(h);
- }
-
-}
+/**
+ * @(#)HistogramDialog.java
+ *
+ * A JDialog component that displays a histogram. This
+ *
+ * @author
+ * @version
+ */
+
+package clusterMaker.ui;
+
+import java.awt.Dimension;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.ComponentEvent;
+import java.awt.event.ComponentListener;
+import java.awt.ScrollPane;
+
+
+import javax.swing.BorderFactory;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+
+import javax.swing.border.Border;
+import javax.swing.border.EtchedBorder;
+
+
+public class HistogramDialog extends JDialog implements ActionListener,
ComponentListener, HistoChangeListener{
+ double[] inputArray;
+ int nBins;
+ int currentBins;
+ Histogram histo;
+ JPanel mainPanel;
+ JPanel buttonBox;
+ ScrollPane scrollPanel;
+ JButton zoomOutButton;
+ boolean isZoomed = false;
+
+ public HistogramDialog(String title, double[] inputArray, int nBins) {
+ super();
+ this.inputArray = inputArray;
+ this.nBins = nBins;
+ this.currentBins = nBins;
+ setTitle(title);
+
+ initializeOnce();
+ }
+
+ //public double getCutoff() {}
+
+ public void actionPerformed(ActionEvent e) {
+ if(e.getActionCommand().equals("set"))
+ histo.setBoolShowLine(true);
+ if(e.getActionCommand().equals("close"))
+ this.dispose();
+ if(e.getActionCommand().equals("zoom")){
+ currentBins = currentBins * 2;
+ isZoomed = true;
+ zoom(inputArray, false);
+ zoomOutButton.setEnabled(true);
+ }
+ if(e.getActionCommand().equals("zoomOut")){
+ currentBins = currentBins / 2;
+ if (currentBins == nBins) {
+ isZoomed = false;
+ zoomOutButton.setEnabled(false);
+ }
+ zoom(inputArray, true);
+ }
+
+ }
+
+ public void componentHidden(ComponentEvent e) {}
+ public void componentShown(ComponentEvent e) {}
+ public void componentMoved(ComponentEvent e) {}
+
+ public void componentResized(ComponentEvent e) {
+ // Get our new size & update histogram
+ Dimension dim = e.getComponent().getSize();
+ if(!isZoomed){
+ histo.setPreferredSize(new Dimension(dim.width,
dim.height));
+ }
+ else{
+ histo.setPreferredSize(new Dimension(2000, dim.height));
+ histo.repaint();
+ scrollPanel.setPreferredSize(new Dimension(dim.width,
dim.height));
+ }
+ }
+
+ private void initializeOnce() {
+
+ mainPanel = new JPanel();
+ mainPanel.setLayout(new BoxLayout(mainPanel,
BoxLayout.PAGE_AXIS));
+ mainPanel.addComponentListener(this);
+
+
+ // Create and add the histogram component
+ histo = new Histogram(inputArray, nBins);
+ mainPanel.add(histo);
+
+ addHistoChangeListener(this);
+
+ // TODO: Add box to set lower and upper bounds. Look at JText
and JLabel
+
+ // Create our button box
+ buttonBox = new JPanel();
+
+ // Close button
+ JButton closeButton = new JButton("Close");
+ closeButton.addActionListener(this);
+ closeButton.setActionCommand("close");
+
+ // OK button
+ JButton okButton = new JButton("Set Cutoff");
+ okButton.addActionListener(this);
+ okButton.setActionCommand("set");
+
+ JButton zoomButton = new JButton("Zoom In");
+ zoomButton.addActionListener(this);
+ zoomButton.setActionCommand("zoom");
+
+ zoomOutButton = new JButton("Zoom Out");
+ zoomOutButton.addActionListener(this);
+ zoomOutButton.setActionCommand("zoomOut");
+ zoomOutButton.setEnabled(false);
+
+ buttonBox.add(okButton);
+ buttonBox.add(closeButton);
+ buttonBox.add(zoomButton);
+ buttonBox.add(zoomOutButton);
+
buttonBox.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
+ Dimension buttonDim = buttonBox.getPreferredSize();
+ buttonBox.setMinimumSize(buttonDim);
+ buttonBox.setMaximumSize(new Dimension(Integer.MAX_VALUE,
buttonDim.height));
+ mainPanel.add(buttonBox);
+
+ setContentPane(mainPanel);
+ setDefaultCloseOperation(DISPOSE_ON_CLOSE);
+
+ }
+
+ public void addHistoChangeListener(HistoChangeListener h){
+ histo.addHistoChangeListener(h);
+ }
+
+ public void removeHistoChangeListener(HistoChangeListener h){
+ histo.removeHistoChangeListener(h);
+ }
+
+ private void zoom(double[] inputArray, boolean zoomOut){
+
+ mainPanel.removeAll();
+ // Get the width of the current histogram
+ Dimension histoDim = histo.getSize();
+ int histoWidth = histoDim.width*2;
+
+ if (zoomOut)
+ histoWidth = histoDim.width / 2;
+
+ // Create a new histogram
+ histo = new Histogram(inputArray, currentBins);
+ addHistoChangeListener(this);
+
+ // Get the size of the dialog
+ Dimension dim = this.getSize();
+ int height = dim.height-50; // Account for the button box
+
+ if (isZoomed) {
+ scrollPanel = new
ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
+ scrollPanel.addComponentListener(this);
+ scrollPanel.add(histo);
+ scrollPanel.setPreferredSize(new Dimension(dim.width,
height));
+ histo.setPreferredSize(new Dimension(histoWidth,
height));
+ mainPanel.add(scrollPanel);
+ } else {
+ histo.setPreferredSize(new Dimension(dim.width,
dim.height));
+ mainPanel.add(histo);
+ }
+
+ mainPanel.add(buttonBox);
+
+ // Trigger a relayout
+ pack();
+ }
+
+ public void histoValueChanged(double bounds){
+ System.out.println("histoValueChanged to "+bounds);
+ }
+
+ /**
+ * Main method for testing purposes.
+ */
+ public static void main(String [] args) {
+
+ double[] randArray = new double[100000]; //used for test
+ for(int nI=0; nI < randArray.length; nI++){
+ randArray[nI] = Math.random()*0.0000007;//assigning
random doubles to randArray (used for test)
+ }
+
+ JDialog dialog = new HistogramDialog("Test Dialog", randArray,
100);
+ dialog.pack();
+ dialog.setVisible(true);
+ dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
+ }
+
+}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en
-~----------~----~----~----~------~----~------~--~---