Class GraphicsUtilities

java.lang.Object
cnuphys.splot.plot.GraphicsUtilities

public class GraphicsUtilities extends Object
  • Field Details

    • highlightColor1

      public static Color highlightColor1
      One default color for highlighted drawing.
    • highlightColor2

      public static Color highlightColor2
      Second default color for highlighted drawing.
    • dash1

      public static final BasicStroke dash1
      A stroke used for highlight drawing.
    • dash2

      public static final BasicStroke dash2
      Another stroke used for highlight drawing.
    • dash1_2

      public static final BasicStroke dash1_2
      A stroke used for highlight drawing.
    • dash2_2

      public static final BasicStroke dash2_2
      Another stroke used for highlight drawing.
    • simpleDash

      public static final BasicStroke simpleDash
      A stroke used for common dashed lines.
    • simpleDash2

      public static final BasicStroke simpleDash2
    • strokes

      protected static Hashtable<String,Stroke> strokes
      Hashtable of strokes. As strokes are created, they are cached so that they aren't created over and over again. There should not be many--each linewidth and solid or dashed combination--probably no more than six or eight.
  • Constructor Details

    • GraphicsUtilities

      public GraphicsUtilities()
  • Method Details

    • setSizeSmall

      public static void setSizeSmall(JComponent component)
      Set component to small size variant. Probably only works on Mac.
      Parameters:
      component - the component to change size
    • setSizeMini

      public static void setSizeMini(JComponent component)
      Set component to mini size variant. Probably only works on Mac.
      Parameters:
      component - the component to change size
    • setSquareButton

      public static void setSquareButton(JButton button)
      Set button for square style. For mac so you don't have to use those wasteful oval buttons when space is tight
      Parameters:
      button - the button to change style
    • setTexturedButton

      public static void setTexturedButton(JButton button)
      Set button for textured style. For mac so you don't have to use those wasteful oval buttons when space is tight
      Parameters:
      button - the button to change style
    • minClip

      public static Rectangle minClip(Shape currentClip, Rectangle rect)
      Compute a minimal clip from the intersection of the bounds of the current clip and a rectangle
      Parameters:
      currentClip - the current clip
      rect - the rectangle of interest
      Returns:
      the intersection bounding rect, or null
    • getStroke

      public static Stroke getStroke(float lineWidth, LineStyle lineStyle)
      Gets a stroke appropriate for the line width and line type. Try the hash table first, if not found create the Stroke and place it in the hashtable for future use.
      Parameters:
      lineWidth - the desired line width in pixels.
      lineStyle - the desired line style.
      Returns:
      the appropriate stroke.
    • rectangleToPoints

      public static Point[] rectangleToPoints(Rectangle rect)
      Create four points corresponding to the corners of a rectangle. Useful for item selection points.
      Parameters:
      rect - the rectangle in question.
      Returns:
      points corresponding to the corners.
    • getComponentTranslucentImageBuffer

      public static BufferedImage getComponentTranslucentImageBuffer(Component c)
      Obtain a translucent buffer big enough for offscreen drawing.
      Parameters:
      c - the component being rendered offscreen.
      Returns:
      a translucent buffer big enough for offscreen drawing.
    • getComponentImageBuffer

      public static BufferedImage getComponentImageBuffer(Component c)
      Obtain a buffer big enough for offscreen drawing. This does not do the drawing.
      Parameters:
      c - the component to be rendered offscreen.
      Returns:
      an image big enough for the job.
    • paintComponentOnImage

      public static void paintComponentOnImage(Component c, BufferedImage image)
      Paints the component on an existing image.
      Parameters:
      c - the component to paint.
      image - the image to paint on, already created.
    • getComponentImage

      public static BufferedImage getComponentImage(Component c)
      return an image resulting from offscreen drawing.
      Parameters:
      c - the component being rendered.
      Returns:
      an image upon which the component was drawn.
    • centerComponent

      public static void centerComponent(Component component)
      Center a component.
      Parameters:
      component - The Component to center
    • centerComponent

      public static void centerComponent(Component component, int dh, int dv)
      Center a component.
      Parameters:
      component - The Component to center.
      dh - offset from horizontal center.
      dv - offset from vertical center.
    • screenFraction

      public static Dimension screenFraction(double fraction)
      Returns a dimension that is a specified fraction of the screen size. This is useful, for example, to make a farme 85% of the screen.
      Parameters:
      fraction - the fraction desired, e.g., 0.85. No check for reasonableness is made.
      Returns:
      the requested dimension.
    • rectangleARFixedAdjust

      public static void rectangleARFixedAdjust(Rectangle r, Point p0, Point p)
      Adjust a second point so that the rectangle it forms with the first point matches an aspect ratio of a given rectangle. Useful for rubber banding when you want to preserve the aspect ratio.
      Parameters:
      r - the rectangle whose aspect ration will be mayched.
      p0 - anchor point
      p - will be adjusted so that p0, p form a rectangle with the same aspected ratio as r.
    • rectangleARFixed

      public static Rectangle rectangleARFixed(Rectangle r, Point p0, Point p)
      Get a rectangle with the same apsect ratio. Useful for rubber banding when you want to preserve the sapect ratio.
    • rectangleFromPoints

      public static Rectangle rectangleFromPoints(Point p1, Point p2)
      Given two points, return the rectangle
      Parameters:
      p1 - one point
      p2 - the other point
      Returns:
      the rectangle created from two points
    • sizeToScreen

      public void sizeToScreen(JFrame frame, double fractionalSize)
      Size and center a JFrame relative to the screen.
      Parameters:
      frame - the frame to size.
      fractionalSize - the fraction desired of the screen--e.g., 0.85 for 85%.
    • drawSimple3DOval

      public static void drawSimple3DOval(Graphics g, Rectangle r, Color fc, Color ic, boolean outsie)
      Draw an oval with a 3D etching.
      Parameters:
      g - the Graphics context.
      r - the bounding rectangle.
      fc - an optional fill color.
      ic - an optional inner fill color.
      outsie - if true, the 3D effect is "out", otherwise "in".
    • drawSimple3DRect

      public static void drawSimple3DRect(Graphics g, int x, int y, int w, int h, boolean outsie)
      Draw a 3d "etching" around a rectangular area..
      Parameters:
      g - the Graphics context.
      x - the left of the rectangle.
      y - the top of the rectangle.
      w - the width of the rectangle.
      h - the height of the rectangle.
      outsie - if true, the 3D effect is "out", otherwise "in".
    • drawSimple3DRect

      public static void drawSimple3DRect(Graphics g, int x, int y, int w, int h, Color fc, boolean outsie)
      Draw a 3d rectangle.
      Parameters:
      g - the Graphics context.
      x - the left of the rectangle.
      y - the top of the rectangle.
      w - the width of the rectangle.
      h - the height of the rectangle.
      fc - the fill color (if null, no fill).
      outsie - if true, the 3D effect is "out", otherwise "in".
    • drawSimple3DRect

      public static void drawSimple3DRect(Graphics g, Rectangle r, Color fc, boolean outsie)
      Draw a 3d rectangle.
      Parameters:
      g - the Graphics context.
      r - the rectangle.
      fc - the fill color.
      outsie - if true, the 3D effect is "out", otherwise "in".
    • fillAndFrameRect

      public static void fillAndFrameRect(Graphics g, Rectangle r, Color fill, Color frame)
      Fill and frame a rectangle
      Parameters:
      g - the Graphics context.
      r - the bounding rectangle.
      fill - the fill color.
      frame - the frame (line) color.
    • drawSimple3DDiamond

      public static void drawSimple3DDiamond(Graphics g, Rectangle r, Color fc, boolean outsie)
      Draw a 3d diamond.
      Parameters:
      g - the Graphics context.
      r - the bounding rectangle.
      fc - the fill color.
      outsie - if true, the 3D effect is "out", otherwise "in".
    • drawRectangle

      public static void drawRectangle(Graphics g, int x, int y, int w2, int h2, Color lc, Color fc)
      Draw a simple rectangle symbol.
      Parameters:
      g - the Graphics context.
      x - the horizontal center.
      y - the vertical center.
      w2 - the half width.
      h2 - the half height.
      lc - the line color.
      fc - the fill color.
    • drawOval

      public static void drawOval(Graphics g, int x, int y, int w2, int h2, Color lc, Color fc)
      Draw a simple oval symbol.
      Parameters:
      g - the Graphics context.
      x - the horizontal center.
      y - the vertical center.
      w2 - the half width.
      h2 - the half height.
      lc - the line color.
      fc - the fill color.
    • drawUpTriangle

      public static void drawUpTriangle(Graphics g, int x, int y, int s2, Color lc, Color fc)
      Draw a simple up triangle symbol.
      Parameters:
      g - the Graphics context.
      x - the horizontal center.
      y - the vertical center.
      s2 - the half width.
      lc - the line color.
      fc - the fill color.
    • drawDownTriangle

      public static void drawDownTriangle(Graphics g, int x, int y, int s2, Color lc, Color fc)
      Draw a simple down triangle symbol.
      Parameters:
      g - the Graphics context.
      x - the horizontal center.
      y - the vertical center.
      s2 - the half width.
      lc - the line color.
      fc - the fill color.
    • drawCross

      public static void drawCross(Graphics g, int x, int y, int s2, Color lc)
      Draw a simple cross symbol.
      Parameters:
      g - the Graphics context.
      x - the horizontal center.
      y - the vertical center.
      s2 - the half width.
      lc - the line color.
    • drawX

      public static void drawX(Graphics g, int x, int y, int s2, Color lc)
      Draw a simple "X" symbol.
      Parameters:
      g - the Graphics context.
      x - the horizontal center.
      y - the vertical center.
      s2 - the half width.
      lc - the line color.
    • drawHighlightedRectangle

      public static void drawHighlightedRectangle(Graphics g, Rectangle r)
      Draws a highlighted rectangle using default colors.
      Parameters:
      g - the graphics context.
      r - the rectangle being highlighted.
    • drawHighlightedRectangle

      public static void drawHighlightedRectangle(Graphics g, Rectangle r, Color color1, Color color2)
      Draws a highlighted rectangle.
      Parameters:
      g - the graphics context.
      r - the rectangle being highlighted.
      color1 - one color for the alternating dash.
      color2 - the other color for the alternating dash.
    • drawHighlightedLine

      public static void drawHighlightedLine(Graphics g, int x1, int y1, int x2, int y2, Color color1, Color color2)
      Parameters:
      g -
      x1 -
      y1 -
      x2 -
      y2 -
      color1 -
      color2 -
    • drawHighlightedArc

      public static void drawHighlightedArc(Graphics g, int x, int y, int width, int height, int startAngle, int arcAngle, Color color1, Color color2)
    • drawHighlightedOval

      public static void drawHighlightedOval(Graphics g, Rectangle r)
      Draws a highlighted rectangle using default colors.
      Parameters:
      g - the graphics context.
      r - the rectangle bounding the oval being highlighted.
    • drawHighlightedOval

      public static void drawHighlightedOval(Graphics g, Rectangle r, Color color1, Color color2)
      Draws a highlighted oval.
      Parameters:
      g - the graphics context.
      r - the rectangle bounding the oval being highlighted.
      color1 - one color for the alternating dash.
      color2 - the other color for the alternating dash.
    • drawHighlightedPolyline

      public static void drawHighlightedPolyline(Graphics g, int[] x, int[] y, int n)
      Draws a highlighted polyline (set of unclosed points) using default colors.
      Parameters:
      g - the graphics context.
      x - the x coordinate array.
      y - the y coordinate array.
      n - the number of points to draw.
    • drawHighlightedPolyline

      public static void drawHighlightedPolyline(Graphics g, int[] x, int[] y, int n, Color color1, Color color2)
      Draws a highlighted polyline (set of unclosed points).
      Parameters:
      g - the graphics context.
      x - the x coordinate array.
      y - the y coordinate array.
      n - the number of points to draw.
      color1 - one color for the alternating dash.
      color2 - the other color for the alternating dash.
    • drawHighlightedShape

      public static void drawHighlightedShape(Graphics g, Shape shape)
      Draws a highlighted shape using default colors.
      Parameters:
      g - the graphics context.
      shape - the shape being highlighted.
    • drawHighlightedShape

      public static void drawHighlightedShape(Graphics g, Shape shape, Color color1, Color color2)
      Draws a highlighted shape.
      Parameters:
      g - the graphics context.
      shape - the shape being highlighted.
      color1 - one color for the alternating dash.
      color2 - the other color for the alternating dash.
    • pointOnLine

      public static boolean pointOnLine(int px, int py, int startx, int starty, int endx, int endy)
      Returns true if the point is on the line, with an amount of slop controlled by the class constant SELECTRES.
      Parameters:
      px - the x coordinate of the point to test.
      py - the y coordinate of the point to test.
      startx - the x coordinate of the start of the line.
      starty - the y coordinate of the start of the line.
      endx - the x coordinate of the end of the line.
      endy - the y coordinate of the end of the line.
      Returns:
      true if the point is on the line.
    • pointOnLine

      public static boolean pointOnLine(Point p, Point start, Point end)
      Returns true if the point is on the line, with an amount of slop controlled by the class constant SELECTRES.
      Parameters:
      p - the point to test.
      start - the start of the line.
      end - the end of the line.
      Returns:
      true if the point is on the line.
    • drawRotatedText

      public static void drawRotatedText(Graphics2D g, String s, Font font, int x, int y, double angleDegrees)
      Draw rotated text.
      Parameters:
      g - the Graphics context.
      s - the text to draw.
      x - the x pixel coordinate of baseline.
      y - the y pixel coordinate of baseline.
      angleDegrees - the angle of rotation in decimal degrees.
    • drawRotatedText

      public static void drawRotatedText(Graphics2D g, String s, Font font, int xo, int yo, int delX, int delY, double angleDegrees)
      Draw rotated text.
      Parameters:
      g - the Graphics context.
      s - the text to draw.
      xo - the x pixel coordinate of rotation point.
      yo - the y pixel coordinate of rotation point.
      delX - unrotated offset from rotation anchor
      delY - unrotated offset from rotation anchor
      angleDegrees - the angle of rotation in decimal degrees.
    • drawArrow

      public static void drawArrow(Graphics g1, int x, int y, int xx, int yy)
      Draws an arrow on the given Graphics2D context
      Parameters:
      g1 - The Graphics context to draw on
      x - The x location of the "tail" of the arrow
      y - The y location of the "tail" of the arrow
      xx - The x location of the "head" of the arrow
      yy - The y location of the "head" of the arrow
    • drawArrow

      public static void drawArrow(Graphics g1, int x, int y, int xx, int yy, boolean highlight, Color c1, Color c2)
      Draws an arrow on the given Graphics2D context
      Parameters:
      g1 - The Graphics context to draw on
      x - The x location of the "tail" of the arrow
      y - The y location of the "tail" of the arrow
      xx - The x location of the "head" of the arrow
      yy - The y location of the "head" of the arrow
    • drawStyleLine

      public static void drawStyleLine(Graphics g, Color lineColor, float lineWidth, LineStyle lineStyle, int x1, int y1, int x2, int y2)
      Draw a styled line
      Parameters:
      g - the graphics context
      style - the style
      x1 - starting x
      y1 - starting y
      x2 - ending x
      y2 - ending y
    • colorToHex

      public static String colorToHex(Color color)
      Get the hex format of the color in the form #rrggbbaa
      Parameters:
      color - the color to convert
      Returns:
      the hex string for the color
    • colorFromHex

      public static Color colorFromHex(String hex)
      Obtain a color from a hex string in common internet format #rrggbbaa. The "#" and the aa are optional
      Parameters:
      hex - the hex string
      Returns:
      the corresponding color or black on error.
    • getParentContainer

      public static Container getParentContainer(Component c)
    • initializeLookAndFeel

      public static void initializeLookAndFeel()
      Initialize the look and feel.
      Parameters:
      desiredLnf - the desired look and feel.
    • main

      public static void main(String[] arg)