1
2
3
4
5 package spellcast.ui;
6
7 import java.awt.*;
8 import javax.swing.*;
9 import spellcast.beings.*;
10
11 /***
12 *
13 * @author Barrie Treloar
14 */
15 public class MonsterDetails
16 {
17 private Monster monster;
18 private JLabel monsterName;
19 private JLabel monsterTitle;
20 private JLabel hitPoints;
21
22 public MonsterDetails( Monster m )
23 {
24 monster = m;
25
26 monsterName = new JLabel( m.getName(), SwingConstants.LEFT );
27 monsterTitle = new JLabel( " the " + getMonsterClass( m ), SwingConstants.LEFT );
28 hitPoints = new JLabel();
29 hitPoints.setHorizontalAlignment( SwingConstants.LEFT );
30 setHitPoints( m.getHitPoints() );
31 }
32
33 /***
34 * Return the type of monster.
35 */
36 private String getMonsterClass( Monster m )
37 {
38 String result = null;
39
40 String fqn = m.getClass().getName();
41 int indexOfLastDot = fqn.lastIndexOf( '.' );
42 if ( indexOfLastDot == -1 )
43 {
44 throw new RuntimeException( "getMonsterClass() only handles monsters within packages." );
45 }
46 else
47 {
48 result = fqn.substring( indexOfLastDot + 1 );
49 }
50 return result;
51 }
52
53 /***
54 * Set the value of the hit points display to the value specified.
55 */
56 private void setHitPoints( int currentHitPoints )
57 {
58 hitPoints.setText( " " + String.valueOf( currentHitPoints ) );
59 }
60
61 /***
62 * Return the monster for this monster detail.
63 */
64 public Monster getMonster()
65 {
66 return monster;
67 }
68
69 /***
70 * Return the monster name component for this monster detail.
71 */
72 public Component getMonsterName()
73 {
74 return monsterName;
75 }
76
77 /***
78 * Return the monster title component for this monster detail.
79 */
80 public Component getMonsterTitle()
81 {
82 return monsterTitle;
83 }
84
85 /***
86 * Return the monster hit point component for this monster detail.
87 */
88 public Component getHitPoints()
89 {
90 return hitPoints;
91 }
92
93
94 }
95
96