-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.java
More file actions
executable file
·174 lines (147 loc) · 5.01 KB
/
UserInterface.java
File metadata and controls
executable file
·174 lines (147 loc) · 5.01 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.awt.Color;
/**
* 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)
* @author Clément Chomicki
* @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;
/**
* Construct a UserInterface. As a parameter, a Game Engine
* (an object processing and executing the game commands) is
* needed.
*
* @param pGameEngine 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.
* @param pText le texte a afficher
*/
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.
* @param pText le text a afficher
*/
public void println( final String pText )
{
this.print( pText + "\n" );
} // println(.)
/**
* Show an image file in the interface.
* @param pImageName le nom de l'image a afficher.
*/
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.
* @param pOnOff un booleen
*/
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.aEntryField.setBackground(new Color(10, 23, 0));
this.aEntryField.setForeground(new Color(150, 180, 100));
// this.aLog = new JTextArea();
this.aLog = new FancyTextArea(RessourcesFetcher.openImage("background_text"));
/*
this.aLog.setImage(
RessourcesFetcher.openImage("corgy")
);
*/
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();
vPanel.setLayout( new BorderLayout() );
vPanel.add( this.aImage, BorderLayout.NORTH );
vPanel.add( vListScroller, BorderLayout.CENTER );
vPanel.add( this.aEntryField, BorderLayout.SOUTH );
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.
* @param pE un ActionEvent
*/
public void
actionPerformed( final ActionEvent pE )
{
String vActionCmd = pE.getActionCommand();
if (vActionCmd=="El famoso")
this.println("!!VOUS AVEZ CLIQUÉ SUR EL FAMOSO!!!");
else
this.processCommand();
} // 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