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
| import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.awt.image.*;
public class UserInterface implements ActionListener
{
private GameEngine engine;
private JFrame myFrame;
private JTextField entryField;
private JTextArea log;
private JLabel image;
public UserInterface(GameEngine gameEngine)
{
engine = gameEngine;
createGUI();
}
public void print(String text)
{
log.append(text);
log.setCaretPosition(log.getDocument().getLength());
}
public void println(String text)
{
log.append(text + "\n");
log.setCaretPosition(log.getDocument().getLength());
}
public void showImage(String imageName)
{
URL imageURL = this.getClass().getClassLoader().getResource(imageName);
if(imageURL == null)
System.out.println("image not found");
else {
ImageIcon icon = new ImageIcon(imageURL);
image.setIcon(icon);
myFrame.pack();
}
}
public void enable(boolean on)
{
entryField.setEditable(on);
if(!on)
entryField.getCaret().setBlinkRate(0);
}
private void createGUI()
{
myFrame = new JFrame("13 minutes Chronos!");
entryField = new JTextField(100);
log = new JTextArea();
log.setEditable(false);
JScrollPane listScroller = new JScrollPane(log);
listScroller.setPreferredSize(new Dimension(500, 300));
listScroller.setMinimumSize(new Dimension(300,300));
JPanel panel = new JPanel();
image = new JLabel();
panel.setLayout(new BorderLayout());
panel.add(image, BorderLayout.NORTH);
panel.add(listScroller, BorderLayout.CENTER);
panel.add(entryField, BorderLayout.SOUTH);
myFrame.getContentPane().add(panel, BorderLayout.CENTER);
// add some event listeners to some components
myFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
entryField.addActionListener(this);
myFrame.pack();
myFrame.setVisible(true);
entryField.requestFocus();
}
public void actionPerformed(ActionEvent e)
{
// no need to check the type of action at the moment.
// there is only one possible action: text entry
processCommand();
}
private void processCommand()
{
boolean finished = false;
String input = entryField.getText();
entryField.setText("");
engine.interpretCommand(input);
}
} |