Newer
Older
abgabensammlungSS15 / ea / ub8 / framework / DDrawing / DMeasures.java
@MaxXximus92 MaxXximus92 on 20 Jun 2015 6 KB ea code
package DDrawing;



import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;

/**
 * @author  mkron
 */
public class DMeasures {
  // when in use for a DArea:
  Graphics g;
  // when in use for a ScaledBorder:
  ScaledBorder sb;
  // for both:
  DFunction x_scale;
  // for both:
  DFunction y_scale;
  Component comp;
  Insets insets;

  /**
   * package private constructor for the DMeasures object
   * the object can be obtained by calling the method getDMeasures of an DArea
   * object
   *
   * @param area the DArea object
   */
  DMeasures( DArea area ){
    comp = area;
  }

  DMeasures( ScaledBorder sb ){
    this.sb = sb;
  }

  /**
   * method returns the pixel-point which belongs to the DPoint in the
   * D-coordinates
   * it says where to paint a certain DPoint
   *
   * @param p the DPoint
   * @return the coresponding pixel Point
   */
  public Point getPoint( DPoint p ){
    //System.out.println("DMeasures.getPoint :"+org );
    return getPoint( p.x, p.y );
  }

  /**
   * method returns the pixel-point which belongs to the given D-coordinates
   * it says where to paint a certain DPoint
   *
   * @param x the double x D-coordinate
   * @param y the double y D-coordinate
   * @return the coresponding pixel Point
   */
  public Point getPoint( double x, double y ){
    DRectangle rect = getSourceOf( getDRectangle() );
    if( x_scale != null ) x = x_scale.getSourceOf( x );
    if( y_scale != null ) y = y_scale.getSourceOf( y );
    Point dp = new Point();
    Dimension dim = getInner();
    Insets insets = getInsets();
    dp.x = (int)( dim.width * (x - rect.x)/(double)rect.width ) + insets.left;
    dp.y = (int)( dim.height * (1 - (y - rect.y)/(double)rect.height)) + insets.top;
    return dp;
  }

  /**
   * method returns the point in D-coordinates which corresponds to the
   * given pixel-point
   *
   * @param p Point in pixel coordinates
   * @return the coresponding DPoint
   */
  public DPoint getDPoint( Point p ){
    return getDPoint( p.x, p.y );
  }

  /**
   * method returns the point in D-coordinates which corresponds to the
   * given pixel-coordinates
   *
   * @param x x-pixel coordinate
   * @param y y-pixel coordinate
   * @return the coresponding DPoint
   */
  public DPoint getDPoint( int x, int y ){
    DRectangle rect = getSourceOf( getDRectangle() );
    Dimension dim = getInner();
    Insets insets = getInsets();
    x -= insets.left;
    y -= insets.top;
    double dx, dy;
    dx = rect.x + rect.width * x/(double)dim.width;
    dy = rect.y + rect.height * (1 - y/(double)dim.height );
    if( x_scale != null ) dx = x_scale.getImageOf( dx );
    if( y_scale != null ) dy = y_scale.getImageOf( dy );
    return new DPoint( dx, dy );
  }

  /**
   * returns the visible rectangle in D-coordinates of the shown component
   *
   * return the visible rectangle
   */
  public DRectangle getDRectangle(){
    if( sb != null )
      return getImageOf(
        new DRectangle( sb.src_minx, sb.src_miny,
                        sb.src_maxx - sb.src_minx,
                        sb.src_maxy - sb.src_miny )
      );
    return ((DArea)comp).getDRectangle();
  }

  /**
   * returns the current Graphics object, which might be used by components to
   * paint themselves
   * the method sets the clipping area of the Graphics object to the currently
   * visible rectangle
   *
   * @return the Graphics object ( or null if no object was set )
   */
  public Graphics getGraphics(){
    if( g != null ){
      Dimension d = comp.getSize();
      Insets insets = getInsets();
      g.setClip( insets.left + 1, // dann sieht man noch was von der linken Achse
                 insets.top,
                 d.width - insets.left - insets.right,
                 d.height - insets.top - insets.bottom);
    }
    return g;
  }

  /**
   * used by DArea to set a new Graphics object
   */
  void setGraphics( Graphics g ){ this.g = g; }

  /**
   * used by ScaledBorder to update the DMeasures object
   *
   * @param c the parent component the border
   */
  void update( Component c, Insets insets ){
    this.comp = c;
    this.insets = insets;
    x_scale = sb.x_scale;
    y_scale = sb.y_scale;
  }

  private Dimension getInner(){
    Dimension d = comp.getSize();
    Insets insets = getInsets();
    d.width -= insets.left + insets.right;
    d.height -= insets.top + insets.bottom;
    return d;
  }


  /**
   * method returns the source rectangle of the given rectangle
   * they differ if there are scale functions selected which are not the identity
   *
   * @param rect the image rectangle
   * @return the source of it
   */
  DRectangle getSourceOf( DRectangle rect ){
    if( x_scale == null && y_scale == null ) return rect;
    if( rect.isEmpty() ) return (DRectangle)rect.clone();
    DPoint p1 = new DPoint( rect.x, rect.y ),
           p2 = new DPoint( rect.x + rect.width, rect.y + rect.height );

    if( x_scale != null ){
      p1.x = x_scale.getSourceOf( p1.x );
      p2.x = x_scale.getSourceOf( p2.x );
    }
    if( y_scale != null ){
      p1.y = y_scale.getSourceOf( p1.y );
      p2.y = y_scale.getSourceOf( p2.y );
    }
    return new DRectangle( p1.x, p1.y, p2.x - p1.x, p2.y - p1.y );
  }
  /**
   * method returns the image rectangle of the given rectangle
   * they differ if there are scale functions selected which are not the identity
   *
   * @param rect the source rectangle
   * @return the source of it
   */
  DRectangle getImageOf( DRectangle rect ){
    if( x_scale == null && y_scale == null ) return rect;
    if( rect.isEmpty() ) return (DRectangle)rect.clone();
    DPoint p1 = new DPoint( rect.x, rect.y ),
           p2 = new DPoint( rect.x + rect.width, rect.y + rect.height );

    if( x_scale != null ){
      p1.x = x_scale.getImageOf( p1.x );
      p2.x = x_scale.getImageOf( p2.x );
    }
    if( y_scale != null ){
      p1.y = y_scale.getImageOf( p1.y );
      p2.y = y_scale.getImageOf( p2.y );
    }
    return new DRectangle( p1.x, p1.y, p2.x - p1.x, p2.y - p1.y );
  }

  private Insets getInsets(){
    if( sb != null ) return insets;
    return ((DArea)comp).getInsets();
  }
}