From: Lukas P. <pe...@us...> - 2002-07-26 09:33:16
|
Update of /cvsroot/javaprofiler/module/net/sourceforge/javaprofiler/module/views In directory usw-pr-cvs1:/tmp/cvs-serv13413 Modified Files: TimeSeriesGraph.java Log Message: removed JLabels Index: TimeSeriesGraph.java =================================================================== RCS file: /cvsroot/javaprofiler/module/net/sourceforge/javaprofiler/module/views/TimeSeriesGraph.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** TimeSeriesGraph.java 15 Jul 2002 11:16:29 -0000 1.1 --- TimeSeriesGraph.java 26 Jul 2002 09:33:14 -0000 1.2 *************** *** 1,175 **** ! package net.sourceforge.javaprofiler.module.views; ! ! import javax.swing.*; ! import java.awt.*; ! import javax.swing.event.ChangeListener; ! import javax.swing.event.ChangeEvent; ! ! /** Draws values of a time series in a bar graph. ! * @author Lukas Petru ! */ ! public class TimeSeriesGraph extends JComponent implements ! ChangeListener ! { ! /** The model that holds time series data. */ ! private TimeSeriesModel model; ! /** This JLabel shows the maximum value of the graph. */ ! private JLabel maximum; ! /** This is the maximum value of the graph. */ ! private int maxValue; ! /** This JLabel shows the last value of the graph. */ ! private JLabel current; ! /** This is the last value shown in the graph. */ ! private int curValue; ! ! /** Constructs graph with default model. */ ! public TimeSeriesGraph() { ! setPreferredSize(new Dimension(40,40)); ! setOpaque(true); ! setModel(new TimeSeriesModel()); ! maximum=new JLabel("00"); ! add(maximum); ! maximum.setBackground(null); ! maximum.setOpaque(true); ! current=new JLabel("00"); ! add(current); ! current.setBackground(null); ! current.setOpaque(true); ! } ! ! /** Sets a new model. ! */ ! public void setModel(TimeSeriesModel seriesModel) { ! if (seriesModel==null) ! throw new RuntimeException("TimeSeriesModel must not be null"); ! ! TimeSeriesModel oldModel = getModel(); ! if (oldModel != null) { ! oldModel.removeChangeListener(this); ! } ! ! model=seriesModel; ! model.addChangeListener(this); ! } ! ! /** Returns current model. */ ! public TimeSeriesModel getModel() { ! return model; ! } ! ! /** Used for notification of a model data change. Internal function, ! not to be called by end users. ! */ ! public void stateChanged(ChangeEvent e) { ! repaint(); ! } ! ! /** Revalidates Label position. Called in case of size change, ! border change or label text change. */ ! private void revalidateLabel() { ! Insets insets = getInsets(); ! int mw=getWidth() - insets.left - insets.right; ! int mh=getHeight() - insets.top - insets.bottom; ! // maximum ! Dimension size=maximum.getPreferredSize(); ! int w=size.width; ! int h=size.height; ! if (w>mw) w=mw; ! if (h>mh) h=mh; ! maximum.setBounds(insets.left, insets.top, w, h); ! // current ! size=current.getPreferredSize(); ! w=size.width; ! h=size.height; ! if (w>mw) w=mw; ! if (h>mh) h=mh; ! current.setBounds(getWidth()-insets.right-w, insets.top, w, h); ! } ! ! /* Provides hook for revalidateLabel. */ ! public void revalidate() { ! revalidateLabel(); ! super.revalidate(); ! } ! ! /* Provides hook for revalidateLabel. */ ! public void setBounds(int x, int y, int width, int height) { ! Rectangle old=getBounds(); ! super.setBounds(x, y, width, height); ! if (! getBounds().equals(old)) ! revalidateLabel(); ! } ! ! /** Paints a bar graph. */ ! protected void paintComponent(Graphics g) { ! // draw background (component is always opaque) ! super.paintComponent(g); ! g.setColor(getBackground()); ! g.fillRect(0, 0, getWidth(), getHeight()); ! ! // compute usable area ! Insets insets = getInsets(); ! int currentWidth = getWidth() - insets.left - insets.right; ! int currentHeight = getHeight() - insets.top - insets.bottom; ! currentHeight -= current.getHeight(); ! int top=insets.top; ! int left=insets.left; ! ! // draw horizontal line ! g.setColor(getForeground()); ! int y1=insets.top + current.getHeight(); ! g.drawLine(insets.left, y1, currentWidth, y1); ! ! // retrieve data ! int[] data=model.getData(); ! ! // find largest value ! int max=1; ! for (int i=0; i<data.length; i++) { ! if (data[i] > max) ! max=data[i]; ! if (data[i] < 0) ! throw new RuntimeException("Illegal value in TimeSeriesModel"); ! } ! max=fineScale(max); ! if (maxValue != max) { ! maxValue=max; ! maximum.setText(String.valueOf(max)); ! revalidateLabel(); ! } ! int last=data[data.length-1]; ! if (curValue != last) { ! curValue=last; ! current.setText(String.valueOf(last)); ! revalidateLabel(); ! } ! ! final int sCnt=data.length; ! if (sCnt==0) ! throw new RuntimeException( ! "Array has zero length in TimeSeriesModel."); ! // draw ! final int advance=(currentWidth+1) / sCnt; ! int columnW=advance-1; ! if (columnW<1) columnW=1; ! int columnH; ! int x=getWidth() - insets.right - sCnt*advance + 1; ! final int y=getHeight() - insets.bottom; ! for (int i=0; i<sCnt; i++) { ! columnH=data[i]*currentHeight/max; ! g.fillRect(x, y-columnH, columnW, columnH); ! x+=advance; ! } ! } ! ! /** Returns round number greater or equal to argument. */ ! private int fineScale (int x) { ! int d=1; ! int i=x; ! while (i>=10) { ! i/=10; ! d*=10; ! } ! return (x+d-1)/d*d; ! } ! } --- 1,120 ---- ! package net.sourceforge.javaprofiler.module.views; ! ! import javax.swing.*; ! import java.awt.*; ! import javax.swing.event.ChangeListener; ! import javax.swing.event.ChangeEvent; ! ! /** Draws values of a time series in a bar graph. ! * @author Lukas Petru ! */ ! public class TimeSeriesGraph extends JComponent implements ! ChangeListener ! { ! /** The model that holds time series data. */ ! private TimeSeriesModel model; ! ! /** Constructs graph with default model. */ ! public TimeSeriesGraph() { ! setPreferredSize(new Dimension(40,40)); ! setOpaque(true); ! setModel(new TimeSeriesModel()); ! } ! ! /** Sets a new model. ! */ ! public void setModel(TimeSeriesModel seriesModel) { ! if (seriesModel==null) ! throw new RuntimeException("TimeSeriesModel must not be null"); ! ! TimeSeriesModel oldModel = getModel(); ! if (oldModel != null) { ! oldModel.removeChangeListener(this); ! } ! ! model=seriesModel; ! model.addChangeListener(this); ! } ! ! /** Returns current model. */ ! public TimeSeriesModel getModel() { ! return model; ! } ! ! /** Used for notification of a model data change. Internal function, ! not to be called by end users. ! */ ! public void stateChanged(ChangeEvent e) { ! repaint(); ! } ! ! /** Paints a bar graph. */ ! protected void paintComponent(Graphics g) { ! // draw background (component is always opaque) ! super.paintComponent(g); ! g.setColor(getBackground()); ! g.fillRect(0, 0, getWidth(), getHeight()); ! ! // compute usable area ! Insets insets = getInsets(); ! int currentWidth = getWidth() - insets.left - insets.right; ! int currentHeight = getHeight() - insets.top - insets.bottom; ! int top=insets.top; ! int left=insets.left; ! ! // retrieve data ! int[] data=model.getData(); ! ! // find largest value ! int max=model.getMax(); ! for (int i=0; i<data.length; i++) { ! if (data[i] < 0) ! throw new RuntimeException("Illegal value in TimeSeriesModel"); ! } ! max=fineScale(max); ! ! final int sCnt=data.length; ! if (sCnt==0) ! throw new RuntimeException( ! "Array has zero length in TimeSeriesModel."); ! ! // area width for graph w/o axes ! int areaMaxW=currentWidth-2; ! final int advance=(areaMaxW+1) / sCnt; ! int columnW=advance-1; ! if (columnW<1) columnW=1; ! int areaW=sCnt*advance - 1; ! int areaMaxH=currentHeight-2; ! ! // draw axes ! g.setColor(getForeground()); ! int x0=insets.left + (areaMaxW-areaW)/2; ! int y0=getHeight()-1 - insets.bottom; ! g.drawLine(x0, y0-1, x0+areaW+2-1, y0-1); ! g.drawLine(x0+1, y0, x0+1, y0-(areaMaxH+2-1)); ! ! // draw ! int columnH; ! int x=x0+2; ! final int y=y0 - 1; ! for (int i=0; i<sCnt; i++) { ! columnH=data[i]*areaMaxH/max; ! g.fillRect(x, y-columnH, columnW, columnH); ! x+=advance; ! } ! } ! ! /** Returns round number greater or equal to argument. */ ! private int fineScale (int x) { ! if (x<=0) ! return 1; ! int d=1; ! int i=x; ! while (i>=10) { ! i/=10; ! d*=10; ! } ! // d is power of than <= x ! return (x+d-1)/d*d; ! } ! } |