⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dataset.java

📁 java 作图的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   */      public void draw_data(Graphics g, Rectangle bounds) {           Color c;           if ( xaxis != null ) {                xmax = xaxis.maximum;                xmin = xaxis.minimum;           }           if ( yaxis != null ) {                ymax = yaxis.maximum;                ymin = yaxis.minimum;           }                      xrange = xmax - xmin;           yrange = ymax - ymin;	   /*	   ** draw the legend before we clip the data window	   */           draw_legend(g,bounds);	   /*	   ** Clip the data window	   */           if(clipping) g.clipRect(bounds.x, bounds.y,                                    bounds.width, bounds.height);           c = g.getColor();           if( linestyle != DataSet.NOLINE ) {               if ( linecolor != null) g.setColor(linecolor);               else                    g.setColor(c);               draw_lines(g,bounds);           }               if( marker > 0 ) {               if(markercolor != null) g.setColor(markercolor);               else                    g.setColor(c);               draw_markers(g,bounds);           }               g.setColor(c);      }  /**   * return the data X maximum.   */      public double getXmax() {  return dxmax; }   /**   * return the data X minimum.   */      public double getXmin() {  return dxmin; }   /**   * return the data Y maximum.   */      public double getYmax() {  return dymax; }   /**   * return the data Y minimum.   */      public double getYmin() {  return dymin; }        /**   * Define a data legend in the graph window   * @param x    pixel position of the legend.   * @param y    pixel position of the legend.   * @param text text to display in the legend   */       public void legend(int x, int y, String text) {           if(text == null) { legend_text = null;  return; }           if(legend_text == null) legend_text = new TextLine(text);           else                    legend_text.setText(text);           legend_text.setJustification(TextLine.LEFT);           legend_ix    = x;           legend_iy    = y;           legend_dx    = 0.0;           legend_dy    = 0.0;      }  /**   * Define a data legend in the graph window   * @param x    data position of the legend.   * @param y    data position of the legend.   * @param text text to display in the legend   */       public void legend(double x, double y, String text) {           if(text == null) { legend_text = null;  return; }           if(legend_text == null) legend_text = new TextLine(text);           else                    legend_text.setText(text);           legend_text.setJustification(TextLine.LEFT);           legend_dx    = x;           legend_dy    = y;           legend_ix    = 0;           legend_iy    = 0;      }  /**   * Set the font to be used in the legend   * @param f font   */      public void legendFont(Font f) {           if(f == null) return;           if(legend_text == null) legend_text = new TextLine();           legend_text.setFont(f);      }  /**   * Set the color for the legend text   * @param c color   */      public void legendColor(Color c) {           if(c == null) return;           if(legend_text == null) legend_text = new TextLine();           legend_text.setColor(c);      }  /**   * Return the number of data points in the DataSet   * @return number of (x,y0 points.   */      public int dataPoints() {  return length/stride; }  /**   * get the data point at the parsed index. The first (x,y) pair   * is at index 0.   * @param index Data point index   * @return array containing the (x,y) pair.   */      public double[] getPoint(int index) {            double point[] = new double[stride];            int i = index*stride;            if( index < 0 || i > length-stride ) return null;            for(int j=0; j<stride; j++) point[j] = data[i+j];                        return point;	  }  /**   * Return the data point that is closest to the parsed (x,y) position   * @param x    * @param y (x,y) position in data space.    * @return array containing the closest data point.   */      public double[] getClosestPoint(double x, double y) {            double point[] = {0.0, 0.0, 0.0};            int i;            double xdiff, ydiff, dist2;            xdiff = data[0] - x;            ydiff = data[1] - y;                          point[0] = data[0];            point[1] = data[1];            point[2] = xdiff*xdiff + ydiff*ydiff;                         for(i=stride; i<length-1; i+=stride) {                xdiff = data[i  ] - x;                ydiff = data[i+1] - y;                dist2 = xdiff*xdiff + ydiff*ydiff;                if(dist2 < point[2]) {                    point[0] = data[i  ];                    point[1] = data[i+1];                    point[2] = dist2;		  }           }           //System.out.println("DataSet: closestpoint "+point[0]+", "+point[1]+", "+point[2]);           return point;            	  }/************************ Protected Methods*********************/  /**   * Draw into the data window the straight line segments joining the   * data points.   * @param g Graphics context   * @param w Data window   */      protected void draw_lines(Graphics g, Rectangle w) {          int i;          int j;          boolean inside0 = false;          boolean inside1 = false;          double x,y;          int x0 = 0 , y0 = 0;          int x1 = 0 , y1 = 0;//     Calculate the clipping rectangle          Rectangle clip = g.getClipRect();          int xcmin = clip.x;          int xcmax = clip.x + clip.width;          int ycmin = clip.y;          int ycmax = clip.y + clip.height;//    Is there any data to draw? Sometimes the draw command will//    will be called before any data has been placed in the class.          if( data == null || data.length < stride ) return;          //          System.out.println("Drawing Data Lines!");//    Is the first point inside the drawing region ?          if( (inside0 = inside(data[0], data[1])) ) {              x0 = (int)(w.x + ((data[0]-xmin)/xrange)*w.width);              y0 = (int)(w.y + (1.0 - (data[1]-ymin)/yrange)*w.height);              if( x0 < xcmin || x0 > xcmax ||                   y0 < ycmin || y0 > ycmax)  inside0 = false;          }          for(i=stride; i<length; i+=stride) {//        Is this point inside the drawing region?              inside1 = inside( data[i], data[i+1]);             //        If one point is inside the drawing region calculate the second point              if ( inside1 || inside0 ) {               x1 = (int)(w.x + ((data[i]-xmin)/xrange)*w.width);               y1 = (int)(w.y + (1.0 - (data[i+1]-ymin)/yrange)*w.height);               if( x1 < xcmin || x1 > xcmax ||                    y1 < ycmin || y1 > ycmax)  inside1 = false;              }//        If the second point is inside calculate the first point if it//        was outside              if ( !inside0 && inside1 ) {                x0 = (int)(w.x + ((data[i-stride]-xmin)/xrange)*w.width);                y0 = (int)(w.y + (1.0 - (data[i-stride+1]-ymin)/yrange)*w.height);              }//        If either point is inside draw the segment              if ( inside0 || inside1 )  {                      g.drawLine(x0,y0,x1,y1);              }/***        The reason for the convolution above is to avoid calculating**        the points over and over. Now just copy the second point to the**        first and grab the next point*/              inside0 = inside1;              x0 = x1;              y0 = y1;          }      }  /**   *  Return true if the point (x,y) is inside the allowed data range.   */      protected boolean inside(double x, double y) {          if( x >= xmin && x <= xmax &&               y >= ymin && y <= ymax )  return true;                    return false;      }  /**   *  Draw the markers.   *  Only markers inside the specified range will be drawn. Also markers   *  close the edge of the clipping region will be clipped.   * @param g Graphics context   * @param w data window   * @see graph.Markers   */      protected void draw_markers(Graphics g, Rectangle w) {          int x1,y1;          int i;//     Calculate the clipping rectangle          Rectangle clip = g.getClipRect();          int xcmin = clip.x;          int xcmax = clip.x + clip.width;          int ycmin = clip.y;          int ycmax = clip.y + clip.height;/***        Load the marker specified for this data*/          Markers m = g2d.getMarkers();          if( m == null) return;//          System.out.println("Drawing Data Markers!");          for(i=0; i<length; i+=stride) {              if( inside( data[i], data[i+1]) ) {                x1 = (int)(w.x + ((data[i]-xmin)/xrange)*w.width);                y1 = (int)(w.y + (1.0 - (data[i+1]-ymin)/yrange)*w.height);                if( x1 >= xcmin && x1 <= xcmax &&                    y1 >= ycmin && y1 <= ycmax )                         m.draw(g, marker, markerscale, x1, y1);                }          }      }  /**   * Draw a legend for this data set   * @param g Graphics context   * @param w Data Window   */      protected void draw_legend(Graphics g, Rectangle w) {          Color c = g.getColor();          Markers m = null;          if( legend_text == null) return;          if( legend_text.isNull() ) return;          if( legend_ix == 0 && legend_iy == 0 ) {                legend_ix = (int)(w.x + ((legend_dx-xmin)/xrange)*w.width);                legend_iy = (int)(w.y + (1.0 - (legend_dy-ymin)/yrange)*w.height);	  }          if( linestyle != DataSet.NOLINE ) {              if ( linecolor != null) g.setColor(linecolor);              g.drawLine(legend_ix,legend_iy,legend_ix+legend_length,legend_iy);          }          if( marker > 0 ) {               m = g2d.getMarkers();               if( m != null) {                  if(markercolor != null) g.setColor(markercolor);                  else                    g.setColor(c);                  m.draw(g,marker,1.0, legend_ix+legend_length/2, legend_iy);               }          }          legend_text.draw( g,                        legend_ix+legend_length+legend_text.charWidth(g,' '),                        legend_iy+legend_text.getAscent(g)/3);          g.setColor(c);      }  /**   * Calculate the range of the data. This modifies dxmin,dxmax,dymin,dymax   * and xmin,xmax,ymin,ymax   */      protected void range(int stride) {           int i;           if( length >= stride ) {              dxmax = data[0];              dymax = data[1];              dxmin = dxmax;              dymin = dymax;           } else {               dxmin = 0.0;               dxmax = 0.0;               dymin = 0.0;               dymax = 0.0;           }           for(i=stride; i<length; i+=stride ) {             if( dxmax < data[i] )   { dxmax = data[i]; }             else             if( dxmin > data[i] )   { dxmin = data[i]; }             if( dymax < data[i+1] ) { dymax = data[i+1]; }             else             if( dymin > data[i+1] ) { dymin = data[i+1]; }           }           if( xaxis == null) {              xmin = dxmin;              xmax = dxmax;           }           if( yaxis == null) {              ymin = dymin;              ymax = dymax;           }     }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -