edu.uah.math.devices
Class GaltonBoard

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by edu.uah.math.devices.Graph
                      extended by edu.uah.math.devices.GaltonBoard
All Implemented Interfaces:
java.awt.event.MouseMotionListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible

public class GaltonBoard
extends Graph
implements java.awt.event.MouseMotionListener, java.io.Serializable

This class is a simple model of the classical Galton board. A ball falls through a triangular array of pegs. At each peg, the ball may bounce to the right or to the left. This class can be used to illustrate Bernoulli trials and various combinatorial formulas.

Version:
August, 2003
Author:
Kyle Siegrist, Dawn Duehring
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class edu.uah.math.devices.Graph
ABOVE, BELOW, HORIZONTAL, LEFT, RIGHT, showModelDistribution, VERTICAL
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
GaltonBoard()
          This default constructor creates a new Galton board with 10 rows and with the dafult probabilities and colors.
GaltonBoard(int n)
          This special constructor creates a new Galton board with a specified number of rows, and with default probability 0.5, and default peg color blue and ball color red.
GaltonBoard(int n, double p)
          This special constructor creates a new Galton board with a specified number of rows, a specified probability, and with default peg color blue and ball color red.
GaltonBoard(int n, double p, java.awt.Color pc, java.awt.Color bc)
          This general constructor creates a new Galton board with a specified number for the last row, a specified probability, and specified ball and peg colors.
 
Method Summary
 void drawBall(int x, int y)
          This method draws the ball at a specified peg.
 java.awt.Color getBallColor()
          This method returns the ball color.
 int getBallColumn()
          This method returns the column number of the ball (an integer between 0 and the row number of the ball).
 int getBallRow()
          This method returns the row number of the ball (an integer between 0 and the number of rows);
 java.lang.String getBitString()
          This method returns the bit string that corresponds to the path of the ball through the Galton board, up to the current ball location.
 java.awt.Color getPegColor()
          This method returns the peg color.
 double getProbability()
          This method returns the probability of a step to the right.
 double getRadius()
          This method returns the radius of the pegs and the ball.
 int getRows()
          This method returns the number of rows.
 java.lang.String getSubset()
          This method returns the subset of {1, 2, ..., n} (where n is the number of rows) that corresponds to the path of the ball through the Galton board, up to the current ball location.
 boolean isPathDrawn()
          This method returns the path drawn boolean variable.
 void mouseDragged(java.awt.event.MouseEvent event)
           
 void mouseEntered(java.awt.event.MouseEvent event)
           
 void mouseExited(java.awt.event.MouseEvent event)
           
 void mouseMoved(java.awt.event.MouseEvent e)
          This method handles the mouse move event.
 void mousePressed(java.awt.event.MouseEvent event)
           
 void mouseReleased(java.awt.event.MouseEvent event)
           
 int moveBall()
          This method moves the ball randomly to the next row, according to the probability.
 void moveBall(int i)
          This method moves the ball to a specified position on the next row.
 void paintComponent(java.awt.Graphics g)
          This method draws the Galton board.
 void reset()
          This method resets the Galton board, including the path, bit string, and subset.
 void setBallColor(java.awt.Color c)
          This method sets the ball color.
 void setColors(java.awt.Color pc, java.awt.Color bc)
          This method sets the colors.
 void setPath()
          This method sets the path of the ball through the Galton board randomly, according to the probability.
 void setPath(int[] p)
          This method sets the path of the ball through the Galton board.
 void setPathDrawn(boolean b)
          This method sets the state of the path drawn boolean variable.
 void setPegColor(java.awt.Color c)
          This method sets the peg color.
 void setProbability(double p)
          This method sets the probability of a step to the right.
 void setRadius(double r)
          This method sets the radius of the pegs and the ball.
 void setRows(int n)
          This method sets the number of rows in the Galton board.
 
Methods inherited from class edu.uah.math.devices.Graph
drawAxis, drawAxis, drawAxis, drawBox, drawBox, drawBoxPlot, drawBoxPlot, drawCircle, drawLabel, drawLine, drawPoint, drawTick, drawTick, fillBox, fillBox, fillBoxPlot, fillBoxPlot, fillCircle, format, getBottomMargin, getLeftMargin, getPointSize, getRightMargin, getTopMargin, getXGraph, getXMax, getXMin, getXPixels, getXScale, getYGraph, getYMax, getYMin, getYPixels, getYScale, setMargins, setPointSize, setScale, setShowModelDistribution
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GaltonBoard

public GaltonBoard(int n,
                   double p,
                   java.awt.Color pc,
                   java.awt.Color bc)
This general constructor creates a new Galton board with a specified number for the last row, a specified probability, and specified ball and peg colors. The rows are numbered from 0 to the last row. The pegs in row j are numbered from 0 to j. The probability governs a step to the right.

Parameters:
n - the number of the last ballRow
p - the probabiltiy of a step to the right
pc - the peg color
bc - the ball color

GaltonBoard

public GaltonBoard(int n,
                   double p)
This special constructor creates a new Galton board with a specified number of rows, a specified probability, and with default peg color blue and ball color red.

Parameters:
n - the number of rows.
p - the probability of a step to the right

GaltonBoard

public GaltonBoard(int n)
This special constructor creates a new Galton board with a specified number of rows, and with default probability 0.5, and default peg color blue and ball color red.

Parameters:
n - the number of rows

GaltonBoard

public GaltonBoard()
This default constructor creates a new Galton board with 10 rows and with the dafult probabilities and colors.

Method Detail

setRows

public void setRows(int n)
This method sets the number of rows in the Galton board. For drawing purposes, the Galton board is given a coordinate system where x varies from -n to n and y from -n to 0.

Parameters:
n - the number of rows

getRows

public int getRows()
This method returns the number of rows.

Returns:
the number of rows

setProbability

public void setProbability(double p)
This method sets the probability of a step to the right.

Parameters:
p - the probability

getProbability

public double getProbability()
This method returns the probability of a step to the right.

Returns:
the probability

setRadius

public void setRadius(double r)
This method sets the radius of the pegs and the ball. The admissiable value are 0 < r <= 0.5

Parameters:
r - the radius

getRadius

public double getRadius()
This method returns the radius of the pegs and the ball.

Returns:
the radius

paintComponent

public void paintComponent(java.awt.Graphics g)
This method draws the Galton board.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - the graphics context

setPath

public void setPath(int[] p)
This method sets the path of the ball through the Galton board.

Parameters:
p - the array of 0's (left moves) and 1's (right moves)

setPath

public void setPath()
This method sets the path of the ball through the Galton board randomly, according to the probability.


moveBall

public void moveBall(int i)
This method moves the ball to a specified position on the next row.

Parameters:
i - 1 for a move right, 0 for a move left

moveBall

public int moveBall()
This method moves the ball randomly to the next row, according to the probability.


getBallColumn

public int getBallColumn()
This method returns the column number of the ball (an integer between 0 and the row number of the ball).

Returns:
the column number

getBallRow

public int getBallRow()
This method returns the row number of the ball (an integer between 0 and the number of rows);

Returns:
the row number

drawBall

public void drawBall(int x,
                     int y)
This method draws the ball at a specified peg.

Parameters:
x - the x-coordinate of the peg
y - the y-coordinate of the peg

getBitString

public java.lang.String getBitString()
This method returns the bit string that corresponds to the path of the ball through the Galton board, up to the current ball location.

Returns:
the string of 0's and 1's

getSubset

public java.lang.String getSubset()
This method returns the subset of {1, 2, ..., n} (where n is the number of rows) that corresponds to the path of the ball through the Galton board, up to the current ball location.

Returns:
the subset

reset

public void reset()
This method resets the Galton board, including the path, bit string, and subset.


mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
This method handles the mouse move event. The peg coordinates are given in the tool tip.

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener
Parameters:
e - the mouse event

setColors

public void setColors(java.awt.Color pc,
                      java.awt.Color bc)
This method sets the colors.

Parameters:
pc - the peg color
bc - the ball color

setPegColor

public void setPegColor(java.awt.Color c)
This method sets the peg color.

Parameters:
c - the peg color

getPegColor

public java.awt.Color getPegColor()
This method returns the peg color.

Returns:
the peg color

setBallColor

public void setBallColor(java.awt.Color c)
This method sets the ball color.

Parameters:
c - the ball color

getBallColor

public java.awt.Color getBallColor()
This method returns the ball color.

Returns:
the ball color

setPathDrawn

public void setPathDrawn(boolean b)
This method sets the state of the path drawn boolean variable.

Parameters:
b - true if the path of the ball is drawn

isPathDrawn

public boolean isPathDrawn()
This method returns the path drawn boolean variable.

Returns:
true if the path of the ball is drawn

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent event)

mouseExited

public void mouseExited(java.awt.event.MouseEvent event)

mousePressed

public void mousePressed(java.awt.event.MouseEvent event)

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent event)

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent event)
Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener