jscicalc.CalculatorPanel Class Reference

This class creates the main CalculatorPanel, which contains 44 buttons and a display panel (DisplayPanel object) to emulate an LCD display. More...

Inheritance diagram for jscicalc.CalculatorPanel:

jscicalc.HexPanel jscicalc.PlainPanel jscicalc.ShiftHexPanel jscicalc.ShiftPanel jscicalc.StatPanel jscicalc.ShiftStatPanel
Collaboration diagram for jscicalc.CalculatorPanel:

Collaboration graph
[legend]

List of all members.

Public Member Functions

synchronized void run ()
 Required to implement Runnable interface.
synchronized void paintComponent (java.awt.Graphics graphics)
 Paint the component: sets the colour for the background.
synchronized void layoutSprings ()
 Long function that lays out buttons and DisplayPanel.
synchronized void setDisplayPanel ()
synchronized java.util.Vector
< CalculatorButton
buttons ()
 CalculatorApplet might need this.
synchronized java.util.HashMap
< Character, CalculatorButton
keyMap ()
 CalculatorApplet uses this to handle keyboard shortcuts.
Base base ()
 I can’t rememeber why we have this.
void base (Base base)
 I can’t rememeber why we have this.

Static Public Member Functions

static CalculatorPanel createPanel (CalculatorApplet applet, SpecialButtonType sbt, java.awt.Color colour)
 We use this rather than a constructor to create a CalculatorPanel because in practice we always want a subclass.

Protected Member Functions

 CalculatorPanel (CalculatorApplet applet, SpecialButtonType sbt, java.awt.Color colour)
 Constructor is protected so that this class never actually gets instantiated.
abstract void setButtons ()
 Subclasses implement this so that they can define all the standard buttons like Numeral, Add, etc.
void createKeyMap ()
 The method that creates the keyMap.

Protected Attributes

java.awt.Color colour
 Background colour.
final CalculatorApplet applet
 We need to know the CalculatorApplet so we can assign it to buttons as we add them.
final SpecialButtonType sbt
 used to identify panels, which are accessed through a CalculatorPanel pointer.
javax.swing.SpringLayout layout
 The Layout.
java.util.Vector
< CalculatorButton
buttons
 The list of buttons.
java.util.HashMap< Character,
CalculatorButton
keyMap
 A map used by CalculatorApplet to fire Action events to buttons based on keyboard presses.

Private Member Functions

void setUp ()
 Creates a key map for the CalculatorApplet, puts in nonstandard buttons, asks the subclass method to set the standard buttons, and arranges all buttons in a SpringLayout.

Private Attributes

Base base
 A Base.
javax.swing.Spring xBorderSpring
 A Spring.
javax.swing.Spring yBorderSpring
 A Spring.
javax.swing.Spring strutX
 A Spring.
javax.swing.Spring strutY
 A Spring.
javax.swing.Spring minX
 A Spring.
javax.swing.Spring minY
 A Spring.
javax.swing.Spring buttonWidthSpring
 A Spring.
javax.swing.Spring narrowButtonWidthSpring
 A Spring.
javax.swing.Spring thinButtonWidthSpring
 A Spring.
javax.swing.Spring buttonHeightSpring
 A Spring.
javax.swing.Spring shortButtonHeightSpring
 A Spring.
javax.swing.Spring shortButtonWidthSpring
 A Spring.
javax.swing.Spring displayWidthSpring
 A Spring.
javax.swing.Spring displayHeightSpring
 A Spring.


Detailed Description

This class creates the main CalculatorPanel, which contains 44 buttons and a display panel (DisplayPanel object) to emulate an LCD display.

The panel needs to know about the controller CalculatorApplet because the controller creates the DisplayPanel and because each button must can ask the CalculatorApplet object to change state or add something to the expression stored in Parser.

The CalculatorPanel is the pane that sits inside the main frame or is the visible part of the applet.

In fact the calculator has more than 44 buttons. This creates an obvious problem: how to make all the buttons available without cluttering up the GUI in much the way that a real calculator does and without making the buttons small and unreadable.

The buttons are derived from CalculatorButton and use HTML to draw their text representation. This allows us to have very readable buttons that we can rescale. But it means we cannot redraw buttons instantly. So, the solution of redrawing buttons as needed in response to a press of the SHIFT button produces a noticeable slowdown in the calculator operation.

My solution is to have several CalculatorPanel objects, each derived from this class and to replace them as needed. This creates its own problem. We need to setup and draw six panels with nearly 350 buttons. Morevover the calculator should be extensible and allow the possibility of even more. The consequence of all this drawing is that the calculator would be quite slow to start up - nothing like as slow as starting M$ office - but enough to be noticeable and irritating.

There are several ways to get round this, not mutually incompatible. For example, you could use separate button panels so that not all buttons get replaced. But it suited me to use multithreading instead. The first panel available should be the PlainPanel object and it is created and added first. The remaining panels are created on a low priority thread. So, at least for simple calculations and switching on, the calculator starts reasonably quickly and, in particular, shows a visible GUI almost immediately after the frame appears.

To make the multithreading work, this calculator implements the Runnable interface.

See also:
CalculatorButton

CalculatorApplet

DisplayPanel

Author:
J. D. Lamb
Version:
Revision
1.6

Constructor & Destructor Documentation

jscicalc.CalculatorPanel.CalculatorPanel ( CalculatorApplet  applet,
SpecialButtonType  sbt,
java.awt.Color  colour 
) [protected]

Constructor is protected so that this class never actually gets instantiated.

Parameters:
applet Needed so that buttons can call the controller
sbt Stored so that we can identify the subclass
colour The background colour.

References jscicalc.CalculatorPanel.colour.


Member Function Documentation

static CalculatorPanel jscicalc.CalculatorPanel.createPanel ( CalculatorApplet  applet,
SpecialButtonType  sbt,
java.awt.Color  colour 
) [static]

We use this rather than a constructor to create a CalculatorPanel because in practice we always want a subclass.

We supply the CalculatorApplet object and a SpecialButtonType used to identify which subclass we want. We also pass a colour. Conceivably different panels could have different colours, though it might be better to get each to ask CalculatorApplet for its colour.

If sbt is SpecialButtonType.NONE then the CalculatorPanel is a PlainPanel and is created now. Otherwise it is created in a background thread.

Parameters:
applet The CalculatorApplet object needed for the buttons and DisplayPanel
sbt Used to identify what to construct
colour What colour should the panel background be?
See also:
CalculatorApplet

SpecialButtonType

References jscicalc.CalculatorPanel.colour, jscicalc.HEX, jscicalc.NONE, jscicalc.CalculatorPanel.run(), jscicalc.SHIFT, jscicalc.SHIFT_HEX, jscicalc.SHIFT_STAT, and jscicalc.STAT.

Referenced by jscicalc.CalculatorApplet.setPanels().

synchronized void jscicalc.CalculatorPanel.run (  ) 

Required to implement Runnable interface.

Calls setUp().

References jscicalc.CalculatorPanel.setUp().

Referenced by jscicalc.CalculatorPanel.createPanel().

synchronized void jscicalc.CalculatorPanel.paintComponent ( java.awt.Graphics  graphics  ) 

Paint the component: sets the colour for the background.

Parameters:
graphics Needed for all paintComponent methods

References jscicalc.CalculatorPanel.colour.

void jscicalc.CalculatorPanel.setUp (  )  [private]

abstract void jscicalc.CalculatorPanel.setButtons (  )  [protected, pure virtual]

Subclasses implement this so that they can define all the standard buttons like Numeral, Add, etc.

Nonstandard buttons (ModeButton, ShiftButton, ...) are handles separately by setUp().

Implemented in jscicalc.HexPanel, jscicalc.PlainPanel, jscicalc.ShiftHexPanel, jscicalc.ShiftPanel, jscicalc.ShiftStatPanel, and jscicalc.StatPanel.

Referenced by jscicalc.CalculatorPanel.setUp().

synchronized void jscicalc.CalculatorPanel.layoutSprings (  ) 

synchronized void jscicalc.CalculatorPanel.setDisplayPanel (  ) 

synchronized java.util.Vector<CalculatorButton> jscicalc.CalculatorPanel.buttons (  ) 

synchronized java.util.HashMap<Character,CalculatorButton> jscicalc.CalculatorPanel.keyMap (  ) 

CalculatorApplet uses this to handle keyboard shortcuts.

It needs to know the char associated with each button so that it can find the button and fire CalculatorButton.actionPerformed() as required.

Returns:
a map from characters to buttons

Referenced by jscicalc.CalculatorPanel.createKeyMap(), and jscicalc.CalculatorPanel.setUp().

void jscicalc.CalculatorPanel.createKeyMap (  )  [protected]

The method that creates the keyMap.

It has a debug feature that tells you if you’ve used any character twice. This is useful for detecting subtle problems.

References jscicalc.CalculatorPanel.buttons(), and jscicalc.CalculatorPanel.keyMap().

Referenced by jscicalc.ShiftPanel.setButtons(), jscicalc.ShiftHexPanel.setButtons(), jscicalc.PlainPanel.setButtons(), and jscicalc.HexPanel.setButtons().

I can’t rememeber why we have this.

Returns:
the base, which ought just to be the CalculatorApplet value.

Referenced by jscicalc.HexPanel.HexPanel(), and jscicalc.ShiftHexPanel.ShiftHexPanel().

void jscicalc.CalculatorPanel.base ( Base  base  ) 

I can’t rememeber why we have this.

Sets tha Base.

Parameters:
base the base, which ought just to be the CalculatorApplet value.


Member Data Documentation

A Base.

I don’t know if we use this.

java.awt.Color jscicalc.CalculatorPanel.colour [protected]

javax.swing.Spring jscicalc.CalculatorPanel.xBorderSpring [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings(), and jscicalc.CalculatorPanel.setDisplayPanel().

javax.swing.Spring jscicalc.CalculatorPanel.yBorderSpring [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings(), and jscicalc.CalculatorPanel.setDisplayPanel().

javax.swing.Spring jscicalc.CalculatorPanel.strutX [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

javax.swing.Spring jscicalc.CalculatorPanel.strutY [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

javax.swing.Spring jscicalc.CalculatorPanel.minX [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

javax.swing.Spring jscicalc.CalculatorPanel.minY [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

javax.swing.Spring jscicalc.CalculatorPanel.buttonWidthSpring [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

javax.swing.Spring jscicalc.CalculatorPanel.thinButtonWidthSpring [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

javax.swing.Spring jscicalc.CalculatorPanel.buttonHeightSpring [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings().

javax.swing.Spring jscicalc.CalculatorPanel.displayWidthSpring [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings(), and jscicalc.CalculatorPanel.setDisplayPanel().

javax.swing.Spring jscicalc.CalculatorPanel.displayHeightSpring [private]

A Spring.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings(), and jscicalc.CalculatorPanel.setDisplayPanel().

We need to know the CalculatorApplet so we can assign it to buttons as we add them.

Referenced by jscicalc.CalculatorPanel.layoutSprings(), jscicalc.CalculatorPanel.setDisplayPanel(), and jscicalc.CalculatorPanel.setUp().

used to identify panels, which are accessed through a CalculatorPanel pointer.

Referenced by jscicalc.CalculatorPanel.setUp().

javax.swing.SpringLayout jscicalc.CalculatorPanel.layout [protected]

The Layout.

Probably doesn’t need to be a class member.

Referenced by jscicalc.CalculatorPanel.layoutSprings(), jscicalc.CalculatorPanel.setDisplayPanel(), and jscicalc.CalculatorPanel.setUp().

java.util.Vector<CalculatorButton> jscicalc.CalculatorPanel.buttons [protected]

java.util.HashMap<Character,CalculatorButton> jscicalc.CalculatorPanel.keyMap [protected]

A map used by CalculatorApplet to fire Action events to buttons based on keyboard presses.

See also:
keyMap()

Referenced by jscicalc.CalculatorApplet.keyTyped().


The documentation for this class was generated from the following file:

Generated on Sun Jun 1 08:57:40 2008 for Java Scientific Calculator by  doxygen 1.5.5