Class ViewJComponentGraph

All Implemented Interfaces:
MouseListener, MouseMotionListener, ImageObserver, MenuContainer, Serializable, EventListener

public class ViewJComponentGraph extends JComponent implements MouseListener, MouseMotionListener
This is a custom made Swing component. It is a graph that takes a panel in the constructor and then draws itself. The graph has the option of labels for the x and y axis, which are set with an accessor. Gridlines can also be turned on and off. The function calling ViewJComponentGraph may specify a number of gridlines to have on the graph. The number of gridlines is the same as the number of tick marks. Points are plotted in the graph by calling an accessor with an array of x coordinates and an array of y coordinates. The number of coordinates in each array must be equal.
Version:
0.1 Aug 1, 1998
Author:
Neva Cherniavsky (primary), Harman Singh
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Use serialVersionUID for interoperability.
      See Also:
    • MAX_NUM_FUNCTS

      public static final int MAX_NUM_FUNCTS
      Maximum number of functions that the graph may display.
      See Also:
    • copiedXs

      private static float[] copiedXs
      DOCUMENT ME!
    • copiedYs

      private static float[] copiedYs
      DOCUMENT ME!
    • copiedName

      private static String copiedName
      DOCUMENT ME!
    • copiedFunct

      private static ViewJComponentFunct copiedFunct
      DOCUMENT ME!
    • backgroundColor

      private Color backgroundColor
      DOCUMENT ME!
    • bounds

      private Rectangle bounds
      DOCUMENT ME!
    • crosshairCursor

      private Cursor crosshairCursor
      DOCUMENT ME!
    • defaultCursor

      private Cursor defaultCursor
      DOCUMENT ME!
    • defaultMaxDomain

      private float defaultMaxDomain
      DOCUMENT ME!
    • defaultMaxRange

      private float defaultMaxRange
      DOCUMENT ME!
    • defaultMinDomain

      private float defaultMinDomain
      DOCUMENT ME!
    • defaultMinRange

      private float defaultMinRange
      DOCUMENT ME!
    • fittedFunctions

      private ViewJComponentFunct[] fittedFunctions
      DOCUMENT ME!
    • font10

      private Font font10
      DOCUMENT ME!
    • font12

      private Font font12
      DOCUMENT ME!
    • font12B

      private Font font12B
      DOCUMENT ME!
    • fun2Icon

      private ImageIcon fun2Icon
      DOCUMENT ME!
    • fun2PrintIcon

      private ImageIcon fun2PrintIcon
      DOCUMENT ME!
    • fun3Icon

      private ImageIcon fun3Icon
      DOCUMENT ME!
    • fun3PrintIcon

      private ImageIcon fun3PrintIcon
      DOCUMENT ME!
    • fun4Icon

      private ImageIcon fun4Icon
      DOCUMENT ME!
    • fun4PrintIcon

      private ImageIcon fun4PrintIcon
      DOCUMENT ME!
    • fun5Icon

      private ImageIcon fun5Icon
      DOCUMENT ME!
    • fun5PrintIcon

      private ImageIcon fun5PrintIcon
      DOCUMENT ME!
    • functions

      private ViewJComponentFunct[] functions
      DOCUMENT ME!
    • graphBounds

      private Rectangle graphBounds
      DOCUMENT ME!
    • maxDomain

      private float maxDomain
      DOCUMENT ME!
    • maxRange

      private float maxRange
      DOCUMENT ME!
    • minDomain

      private float minDomain
      DOCUMENT ME!
    • minRange

      private float minRange
      DOCUMENT ME!
    • newX

      private int[] newX
      DOCUMENT ME!
    • newY

      private int[] newY
      DOCUMENT ME!
    • oldRubberband

      private Rectangle oldRubberband
      DOCUMENT ME!
    • parentFrame

      private JFrame parentFrame
      DOCUMENT ME!
    • rubberbandRect

      private RubberbandRectangle rubberbandRect
      DOCUMENT ME!
    • showFittedFunctions

      private boolean showFittedFunctions
      DOCUMENT ME!
    • showFunctions

      private boolean showFunctions
      DOCUMENT ME!
    • showGridLines

      private boolean showGridLines
      DOCUMENT ME!
    • showLegend

      private boolean showLegend
      DOCUMENT ME!
    • showMinorTickMarks

      private boolean showMinorTickMarks
      DOCUMENT ME!
    • SHOW_LINES_ONLY

      public static final int SHOW_LINES_ONLY
      See Also:
    • SHOW_POINTS_AND_LINES

      public static final int SHOW_POINTS_AND_LINES
      See Also:
    • SHOW_POINTS_ONLY

      public static final int SHOW_POINTS_ONLY
      See Also:
    • showPointsAndLines

      private int showPointsAndLines
      DOCUMENT ME!
    • title

      private String title
      DOCUMENT ME!
    • xGridLines

      private int xGridLines
      DOCUMENT ME!
    • xLabel

      private String xLabel
      DOCUMENT ME!
    • xScale

      private double xScale
      DOCUMENT ME!
    • xTick

      private double xTick
      DOCUMENT ME!
    • yGridLines

      private int yGridLines
      DOCUMENT ME!
    • yLabel

      private String yLabel
      DOCUMENT ME!
    • yScale

      private double yScale
      DOCUMENT ME!
    • yTick

      private double yTick
      DOCUMENT ME!
    • userXGrid

      private boolean userXGrid
    • userYGrid

      private boolean userYGrid
    • xGridUserPositions

      private int[] xGridUserPositions
    • yGridUserPositions

      private int[] yGridUserPositions
    • doLogX

      private boolean doLogX
    • doLogY

      private boolean doLogY
    • zeroXMin

      private boolean zeroXMin
    • zeroYMin

      private boolean zeroYMin
    • x1Vector

      private Vector<Double> x1Vector
    • y1Vector

      private Vector<Double> y1Vector
    • x2Vector

      private Vector<Double> x2Vector
    • y2Vector

      private Vector<Double> y2Vector
    • addSchwarzChristoffelLines

      private boolean addSchwarzChristoffelLines
    • yInvert

      private int yInvert
  • Constructor Details

    • ViewJComponentGraph

      public ViewJComponentGraph(JFrame frame, int width, int height)
      Constructor - creates graph within the JPanel using graphics.
      Parameters:
      frame - frame that this component is in
      width - initial width for the component
      height - initial height for the component
  • Method Details

    • calculateCustomDomain

      public void calculateCustomDomain()
    • calculateCustomRange

      public void calculateCustomRange()
      Calculates the range and domain (according to min and max y values). and expands range if necessary. (does not shrink range)
    • calculateDefaultRangeDomain

      public void calculateDefaultRangeDomain()
      Calculates the default range and domain (according to min and max x and y values). Modifies minRange, maxRange, minDomain, maxDomain accordingly.
    • copyFunct

      public void copyFunct(int index)
      Copies the indicated function's properties, to be pasted later as a new function.
      Parameters:
      index - - index of the function to be copied
    • deleteFunct

      public void deleteFunct(int index)
      Deletes the indicated function.
      Parameters:
      index - - index of the function to be deleted
    • getBackgroundColor

      public Color getBackgroundColor()
      Accessor that gets the background color.
      Returns:
      DOCUMENT ME!
    • getBounds

      public Rectangle getBounds()
      Accessor that returns the bounds of the component.
      Overrides:
      getBounds in class Component
      Returns:
      rectangle bounds of the component
    • getMaxDomain

      public float getMaxDomain()
    • getMinDomain

      public float getMinDomain()
    • getDefaultMaxDomain

      public float getDefaultMaxDomain()
      Returns the default maximum point for the domain (previously calculated).
      Returns:
      DOCUMENT ME!
    • getDefaultMinDomain

      public float getDefaultMinDomain()
      Returns the default minimum point for the domain (previously calculated).
      Returns:
      DOCUMENT ME!
    • getDefaultMaxRange

      public float getDefaultMaxRange()
      Returns the default maximum point for the range (previously calculated).
      Returns:
      DOCUMENT ME!
    • getDefaultMinRange

      public float getDefaultMinRange()
      Returns the default minimum point for the range (previously calculated).
      Returns:
      DOCUMENT ME!
    • getFittedFunctionsVisible

      public boolean getFittedFunctionsVisible()
      Accessor that gets the visible flag.
      Returns:
      the boolean visible flag
    • getFittedFuncts

      public ViewJComponentFunct[] getFittedFuncts()
      Accessor that gets the fitted functions for this graph.
      Returns:
      an array of functions for this graph
    • getFunctionsVisible

      public boolean getFunctionsVisible()
      Accessor that gets the visible flag of the functions.
      Returns:
      the boolean visible flag
    • getFuncts

      public ViewJComponentFunct[] getFuncts()
      Accessor that gets the functions for this graph.
      Returns:
      an array of functions for this graph
    • getGridlinesVisible

      public boolean getGridlinesVisible()
      Accessor that gets the visible flag.
      Returns:
      the boolean visible flag
    • getLegendVisible

      public boolean getLegendVisible()
      Accessor that gets the visible flag.
      Returns:
      the boolean visible flag
    • getMaxRange

      public float getMaxRange()
      Returns the current maximum point for the range.
      Returns:
      DOCUMENT ME!
    • getMinimumSize

      public Dimension getMinimumSize()
      Accessor that returns the minimum size of this component.
      Overrides:
      getMinimumSize in class JComponent
      Returns:
      the minimum size
    • getMinorTickMarksVisible

      public boolean getMinorTickMarksVisible()
      Accessor that gets the visible flag.
      Returns:
      the boolean visible flag
    • getMinRange

      public float getMinRange()
      Returns the current minimum point for the range.
      Returns:
      DOCUMENT ME!
    • getNumberOfXGridLines

      public int getNumberOfXGridLines()
      Accessor that gets the number of gridlines for paint.
      Returns:
      the number of gridlines on the x axis
    • getNumberOfYGridLines

      public int getNumberOfYGridLines()
      Accessor that gets the number of gridlines for paint.
      Returns:
      the number of gridlines on the y axis
    • getPointsAndLinesDisplay

      public int getPointsAndLinesDisplay()
      Accessor that gets the visible flag.
      Returns:
      the integer visible flag
    • getPreferredSize

      public Dimension getPreferredSize()
      Accessor that returns the preferred size of the component.
      Overrides:
      getPreferredSize in class JComponent
      Returns:
      the preferred size
    • getTitle

      public String getTitle()
      Gets the title for the graph.
      Returns:
      title for the graph
    • getXLabel

      public String getXLabel()
      Gets the label for the x axis.
      Returns:
      label for the x axis
    • getYLabel

      public String getYLabel()
      Gets the label for the y axis.
      Returns:
      label for the y axis
    • mouseClicked

      public void mouseClicked(MouseEvent mouseEvent)
      DOCUMENT ME!
      Specified by:
      mouseClicked in interface MouseListener
      Parameters:
      mouseEvent - DOCUMENT ME!
    • mouseDragged

      public void mouseDragged(MouseEvent mouseEvent)
      ******************* Mouse Motion Events ***************************.********************************************* ***********************
      Specified by:
      mouseDragged in interface MouseMotionListener
      Parameters:
      mouseEvent - DOCUMENT ME!
    • mouseEntered

      public void mouseEntered(MouseEvent mouseEvent)
      ************************ Mouse Events *****************************.********************************************* ***********************
      Specified by:
      mouseEntered in interface MouseListener
      Parameters:
      mouseEvent - DOCUMENT ME!
    • mouseExited

      public void mouseExited(MouseEvent mouseEvent)
      DOCUMENT ME!
      Specified by:
      mouseExited in interface MouseListener
      Parameters:
      mouseEvent - DOCUMENT ME!
    • mouseMoved

      public void mouseMoved(MouseEvent mouseEvent)
      Changes the cursor to crosshair and enables the zoom box rubberband if the cursor is in the graph; also sees if cursor is near a valid point and prints it out.
      Specified by:
      mouseMoved in interface MouseMotionListener
      Parameters:
      mouseEvent - event that triggered this function
    • mousePressed

      public void mousePressed(MouseEvent mouseEvent)
      DOCUMENT ME!
      Specified by:
      mousePressed in interface MouseListener
      Parameters:
      mouseEvent - DOCUMENT ME!
    • mouseReleased

      public void mouseReleased(MouseEvent mouseEvent)
      Checks to see that the zoom box is valid, then redraws the graph using the zoom box as bounds.
      Specified by:
      mouseReleased in interface MouseListener
      Parameters:
      mouseEvent - event that triggered this function
    • paintComponent

      public void paintComponent(Graphics g)
      Paints the graph. Checks if the labels are defined and draws them, checks if the gridlines should be shown and draws them, then calls PolyLine with the points.
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - Graphics to paint in
    • paintComponentForPrinter

      public void paintComponentForPrinter(Graphics g)
      Paints the graph for the Printer. Certain things such as background color and graph dimensions and location differ from the regular paint method
      Parameters:
      g - Graphics to paint in
    • pasteFunct

      public void pasteFunct()
      Adds the previously copied copied function to the graph.
    • redrawGraph

      public void redrawGraph(Rectangle newBounds)
      Redraws the graph based on the new bounds. Used for zooming in on portions of the graph.
      Parameters:
      newBounds - the new bounds of the graph
    • resetBounds

      public void resetBounds(Rectangle rect)
      Sets the bounds of the component.
      Parameters:
      rect - the new bounds for the component
    • setBackgroundColor

      public void setBackgroundColor(Color color)
      Accessor that sets the background color to paint.
      Parameters:
      color - the new background color
    • setDefaultRangeDomain

      public void setDefaultRangeDomain()
      Sets range and domain to defaults.
    • setDoLogX

      public void setDoLogX(boolean doLogX)
      Parameters:
      doLogX -
    • setDoLogY

      public void setDoLogY(boolean doLogY)
      Parameters:
      doLogY -
    • setDomain

      public void setDomain(float min, float max)
      Sets the domain of the graph according to the min and max parmaters.
      Parameters:
      min - minimum domain
      max - maximum domain
    • setFittedFunctionsVisible

      public void setFittedFunctionsVisible(boolean visible)
      Accessor that tells whether to show the gridlines.
      Parameters:
      visible - boolean to set it to
    • setFittedFuncts

      public void setFittedFuncts(ViewJComponentFunct[] functs)
      Accessor that sets the fitted functions for this graph.
      Parameters:
      functs - function to set the graph to
    • setFunctionsVisible

      public void setFunctionsVisible(boolean visible)
      Accessor that tells whether to show the functions.
      Parameters:
      visible - boolean to set it to
    • setFuncts

      public void setFuncts(ViewJComponentFunct[] functs)
      Accessor that sets the function for this graph.
      Parameters:
      functs - function to set the graph to
    • setGridlinesVisible

      public void setGridlinesVisible(boolean visible)
      Accessor that tells whether to show the gridlines.
      Parameters:
      visible - boolean to set it to
    • setLabels

      public void setLabels(String xStr, String yStr)
      Sets the labels for the x and y axis to these strings.
      Parameters:
      xStr - label for the x axis
      yStr - label for the y axis
    • setLabelY

      public void setLabelY(String yStr)
      Sets the y axis label to yStr
      Parameters:
      yStr -
    • setLegendVisible

      public void setLegendVisible(boolean visible)
      Accessor that tells whether to show the legend.
      Parameters:
      visible - boolean to set it to
    • setMinorTickMarksVisible

      public void setMinorTickMarksVisible(boolean visible)
      Accessor that tells whether to show the minor tick marks.
      Parameters:
      visible - boolean to set it to
    • setNumberOfXGridLines

      public void setNumberOfXGridLines(int xGrid)
      Accessor that sets the number of gridlines for paint.
      Parameters:
      xGrid - the number of gridlines on the x axis
    • setNumberOfYGridLines

      public void setNumberOfYGridLines(int yGrid)
      Accessor that sets the number of gridlines for paint.
      Parameters:
      yGrid - the number of gridlines on the y axis
    • setPointsAndLinesDisplay

      public void setPointsAndLinesDisplay(int showPointsAndLines)
      Accessor that sets whether to display lines only, points and lines, or points only
      Parameters:
      visible - integer to set it to
    • setRange

      public void setRange(float min, float max)
      Sets the range of the graph according to the min and max parmaters.
      Parameters:
      min - minimum range
      max - maximum range
    • setRangeSymmetric

      public void setRangeSymmetric()
      DOCUMENT ME!
    • setTitle

      public void setTitle(String tStr)
      Sets the title to this string.
      Parameters:
      tStr - title for the graph
    • update

      public void update(Graphics g)
      Calls paint.
      Overrides:
      update in class JComponent
      Parameters:
      g - Graphics to paint in
    • distance

      private double distance(int x1, int y1, int x2, int y2)
      Tests the distance between two points.
      Parameters:
      x1 - x coordinate of the first point
      y1 - y coordinate of the first point
      x2 - x coordinate of second point
      y2 - y coordinate of second point
      Returns:
      returns the distance
    • makeString

      private String makeString(float number, int decPts)
      Makes a string of a float with a specific number of decimal points.
      Parameters:
      number - number to be converted to a string
      decPts - the number of decimal points
      Returns:
      string representation of the number
    • plotGraph

      public void plotGraph(Graphics g)
      Plots the graph by calling PolyLine. Makes new arrays for the x and y, scaling them to the graphics x and y. Also writes the tick mark labels.
      Parameters:
      g - graphics to draw in
    • plotSchwarzChristoffel

      private void plotSchwarzChristoffel(Graphics g)
    • plotGraphForPrinter

      private void plotGraphForPrinter(Graphics g)
      Same as the plotGraph method, except the points used in this method are readable when sent to the printer.
      Parameters:
      g - graphics to draw in
    • getGraphBounds

      public Rectangle getGraphBounds()
    • drawString

      public void drawString(Graphics g, String str, int x, int y)
    • drawLine

      public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
    • setUserXGrid

      public void setUserXGrid(boolean userXGrid)
    • setXGridUserPositions

      public void setXGridUserPositions(int[] xGridUserPositions)
    • setUserYGrid

      public void setUserYGrid(boolean userYGrid)
    • setYGridUserPositions

      public void setYGridUserPositions(int[] yGridUserPositions)
    • setX1Vector

      public void setX1Vector(Vector<Double> x1Vector)
    • getX1Vector

      public Vector<Double> getX1Vector()
    • setX2Vector

      public void setX2Vector(Vector<Double> x2Vector)
    • getX2Vector

      public Vector<Double> getX2Vector()
    • setY1Vector

      public void setY1Vector(Vector<Double> y1Vector)
    • getY1Vector

      public Vector<Double> getY1Vector()
    • setY2Vector

      public void setY2Vector(Vector<Double> y2Vector)
    • getY2Vector

      public Vector<Double> getY2Vector()
    • setAddSchwarzChristoffelLines

      public void setAddSchwarzChristoffelLines(boolean addSchwarzChristoffelLines)
    • setYInvert

      public void setYInvert(int yInvert)