-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserInterface.java
149 lines (129 loc) · 4.4 KB
/
UserInterface.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.awt.image.*;
/**
* This class implements a simple graphical user interface with a text entry
* area, a text output area and an optional image.
*
* @author Michael Kolling (DB edited)
* @version 1.0 (Jan 2003)
*/
public class UserInterface implements ActionListener
{
private GameEngine aEngine;
private JFrame aMyFrame;
private JTextField aEntryField;
private JTextArea aLog;
private JLabel aImage;
private JButton aQuitter;
private JButton aHelp;
/**
* Construct a UserInterface. As a parameter, a Game Engine
* (an object processing and executing the game commands) is
* needed.
*
* @param gameEngine The GameEngine object implementing the game logic.
*/
public UserInterface( final GameEngine pGameEngine )
{
this.aEngine = pGameEngine;
this.createGUI();
} // UserInterface(.)
/**
* Print out some text into the text area.
*/
public void print( final String pText )
{
this.aLog.append( pText );
this.aLog.setCaretPosition( this.aLog.getDocument().getLength() );
} // print(.)
/**
* Print out some text into the text area, followed by a line break.
*/
public void println( final String pText )
{
this.print( pText + "\n" );
} // println(.)
/**
* Show an image file in the interface.
*/
public void showImage( final String pImageName )
{
URL vImageURL = this.getClass().getClassLoader().getResource( pImageName );
if ( vImageURL == null )
System.out.println( "image not found" );
else {
ImageIcon vIcon = new ImageIcon( vImageURL );
this.aImage.setIcon( vIcon );
this.aMyFrame.pack();
}
} // showImage(.)
/**
* Enable or disable input in the input field.
*/
public void enable( final boolean pOnOff )
{
this.aEntryField.setEditable( pOnOff );
if ( ! pOnOff )
this.aEntryField.getCaret().setBlinkRate( 0 );
} // enable(.)
/**
* Set up graphical user interface.
*/
private void createGUI()
{
this.aMyFrame = new JFrame( "Zork" );
this.aEntryField = new JTextField( 34 );
this.aLog = new JTextArea();
this.aLog.setEditable( false );
JScrollPane vListScroller = new JScrollPane( this.aLog );
vListScroller.setPreferredSize( new Dimension(200, 200) );
vListScroller.setMinimumSize( new Dimension(100,100) );
JPanel vPanel = new JPanel();
this.aImage = new JLabel();
aHelp = new JButton("Aide");
aQuitter = new JButton("Quitter");
aHelp.addActionListener(this);
aQuitter.addActionListener(this);
vPanel.setLayout( new BorderLayout() );
vPanel.add( this.aImage, BorderLayout.NORTH );
vPanel.add( vListScroller, BorderLayout.CENTER );
vPanel.add( this.aEntryField, BorderLayout.SOUTH );
vPanel.add(aHelp, BorderLayout.EAST);
vPanel.add(aQuitter, BorderLayout.WEST);
this.aMyFrame.getContentPane().add( vPanel, BorderLayout.CENTER );
// add some event listeners to some components
this.aMyFrame.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
} );
this.aEntryField.addActionListener( this );
this.aMyFrame.pack();
this.aMyFrame.setVisible( true );
this.aEntryField.requestFocus();
} // createGUI()
/**
* Actionlistener interface for entry textfield.
*/
public void actionPerformed( final ActionEvent pE )
{
// no need to check the type of action at the moment.
// there is only one possible action: text entry
this.processCommand();
if(pE.getSource() == aQuitter)
aEngine.interpretCommand("quit");
if(pE.getSource() == aHelp)
aEngine.interpretCommand("help");
} // actionPerformed(.)
/**
* A command has been entered. Read the command and do whatever is
* necessary to process it.
*/
private void processCommand()
{
String vInput = this.aEntryField.getText();
this.aEntryField.setText( "" );
this.aEngine.interpretCommand( vInput );
} // processCommand()
} // UserInterface