

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. | |
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.
| jscicalc.CalculatorPanel.CalculatorPanel | ( | CalculatorApplet | applet, | |
| SpecialButtonType | sbt, | |||
| java.awt.Color | colour | |||
| ) | [protected] |
Constructor is protected so that this class never actually gets instantiated.
| 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.
| 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.
| 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? |
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.
| graphics | Needed for all paintComponent methods |
References jscicalc.CalculatorPanel.colour.
| void jscicalc.CalculatorPanel.setUp | ( | ) | [private] |
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.
References jscicalc.CalculatorPanel.applet, jscicalc.CalculatorPanel.buttons(), jscicalc.HEX, jscicalc.CalculatorPanel.keyMap(), jscicalc.CalculatorPanel.layout, jscicalc.CalculatorPanel.layoutSprings(), jscicalc.NONE, jscicalc.CalculatorPanel.sbt, jscicalc.CalculatorPanel.setButtons(), jscicalc.SHIFT, jscicalc.SHIFT_HEX, jscicalc.SHIFT_STAT, and jscicalc.STAT.
Referenced by jscicalc.CalculatorPanel.run().
| 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 | ( | ) |
Long function that lays out buttons and DisplayPanel.
It uses a SpringLayout, which is very powerful but not well documented by Sun, to place the buttons exactly where I want them while allowing the whole calculator to be rescaled as required.
References jscicalc.CalculatorPanel.applet, jscicalc.CalculatorApplet.buttonHeight(), jscicalc.CalculatorPanel.buttonHeightSpring, jscicalc.CalculatorPanel.buttons(), jscicalc.CalculatorApplet.buttonWidth(), jscicalc.CalculatorPanel.buttonWidthSpring, jscicalc.CalculatorApplet.displayHeight(), jscicalc.CalculatorPanel.displayHeightSpring, jscicalc.CalculatorPanel.displayWidthSpring, jscicalc.CalculatorPanel.layout, jscicalc.CalculatorApplet.minSize(), jscicalc.CalculatorPanel.minX, jscicalc.CalculatorPanel.minY, jscicalc.CalculatorPanel.narrowButtonWidthSpring, jscicalc.CalculatorPanel.shortButtonHeightSpring, jscicalc.CalculatorPanel.shortButtonWidthSpring, jscicalc.CalculatorApplet.strutSize(), jscicalc.CalculatorPanel.strutX, jscicalc.CalculatorPanel.strutY, jscicalc.CalculatorPanel.thinButtonWidthSpring, jscicalc.CalculatorPanel.xBorderSpring, and jscicalc.CalculatorPanel.yBorderSpring.
Referenced by jscicalc.CalculatorPanel.setUp().
| synchronized void jscicalc.CalculatorPanel.setDisplayPanel | ( | ) |
References jscicalc.CalculatorPanel.applet, jscicalc.CalculatorPanel.displayHeightSpring, jscicalc.CalculatorApplet.displayPanel, jscicalc.CalculatorPanel.displayWidthSpring, jscicalc.CalculatorPanel.layout, jscicalc.CalculatorPanel.xBorderSpring, and jscicalc.CalculatorPanel.yBorderSpring.
Referenced by jscicalc.CalculatorApplet.setCalculatorPanel().
| synchronized java.util.Vector<CalculatorButton> jscicalc.CalculatorPanel.buttons | ( | ) |
CalculatorApplet might need this.
Referenced by jscicalc.CalculatorPanel.createKeyMap(), jscicalc.CalculatorPanel.layoutSprings(), jscicalc.ShiftStatPanel.setButtons(), jscicalc.ShiftPanel.setButtons(), jscicalc.ShiftHexPanel.setButtons(), jscicalc.PlainPanel.setButtons(), jscicalc.HexPanel.setButtons(), and jscicalc.CalculatorPanel.setUp().
| 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.
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.
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.
| base | the base, which ought just to be the CalculatorApplet value. |
Base jscicalc.CalculatorPanel.base [private] |
A Base.
I don’t know if we use this.
java.awt.Color jscicalc.CalculatorPanel.colour [protected] |
Background colour.
Referenced by jscicalc.CalculatorPanel.CalculatorPanel(), jscicalc.CalculatorPanel.createPanel(), jscicalc.HexPanel.HexPanel(), jscicalc.CalculatorPanel.paintComponent(), jscicalc.PlainPanel.PlainPanel(), jscicalc.ShiftHexPanel.ShiftHexPanel(), jscicalc.ShiftPanel.ShiftPanel(), jscicalc.ShiftStatPanel.ShiftStatPanel(), and jscicalc.StatPanel.StatPanel().
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().
javax.swing.Spring jscicalc.CalculatorPanel.narrowButtonWidthSpring [private] |
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().
javax.swing.Spring jscicalc.CalculatorPanel.shortButtonHeightSpring [private] |
A Spring.
Probably doesn’t need to be a class member.
Referenced by jscicalc.CalculatorPanel.layoutSprings().
javax.swing.Spring jscicalc.CalculatorPanel.shortButtonWidthSpring [private] |
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().
final CalculatorApplet jscicalc.CalculatorPanel.applet [protected] |
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().
final SpecialButtonType jscicalc.CalculatorPanel.sbt [protected] |
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] |
The list of buttons.
Referenced by jscicalc.CalculatorApplet.keyPressed(), and jscicalc.CalculatorApplet.keyTyped().
java.util.HashMap<Character,CalculatorButton> jscicalc.CalculatorPanel.keyMap [protected] |
A map used by CalculatorApplet to fire Action events to buttons based on keyboard presses.
Referenced by jscicalc.CalculatorApplet.keyTyped().
1.5.5