001import javax.swing.*; 002import java.awt.*; 003import java.awt.event.*; 004import java.net.URL; 005import java.awt.image.*; 006import javax.swing.JButton; 007import javax.swing.JFrame; 008import javax.swing.JPanel; 009import java.awt.event.ActionListener; 010/** 011 * This class is part of the "Pere-noel" application. 012 * "Pere Noel" is a very simple, text based adventure game. 013 * 014 * This class implements a simple graphical user interface with a text entry 015 * area, a text output area and an optional image. 016 * 017 * @author Célia PRIOL & Benoît CHAUVEAU 018 */ 019public class UserInterface implements ActionListener 020{ 021 private GameEngine engine; 022 private JFrame myFrame; 023 private JTextField entryField; 024 private JTextArea log; 025 private JLabel image; 026 private JButton help, North, South, East, West; 027 028 029 /** 030 * Construct a UserInterface. As a parameter, a Game Engine 031 * (an object processing and executing the game commands) is 032 * needed. 033 * 034 * @param gameEngine The GameEngine object implementing the game logic. 035 */ 036 public UserInterface(GameEngine gameEngine) 037 { 038 engine = gameEngine; 039 createGUI(); 040 } 041 042 /** 043 * Print out some text into the text area. 044 */ 045 public void print(String text) 046 { 047 log.append(text); 048 log.setCaretPosition(log.getDocument().getLength()); 049 } 050 051 /** 052 * Print out some text into the text area, followed by a line break. 053 */ 054 public void println(String text) 055 { 056 log.append(text + "\n"); 057 log.setCaretPosition(log.getDocument().getLength()); 058 } 059 060 /** 061 * Show an image file in the interface. 062 */ 063 public void showImage(String imageName) 064 { 065 URL imageURL = this.getClass().getClassLoader().getResource(imageName); 066 if(imageURL == null) 067 System.out.println("Error : image not found."); 068 else { 069 ImageIcon icon = new ImageIcon(imageURL); 070 image.setIcon(icon); 071 myFrame.pack(); 072 } 073 } 074 075 /** 076 * Enable or disable input in the input field. 077 */ 078 public void enable(boolean on) 079 { 080 entryField.setEditable(on); 081 if(!on) 082 entryField.getCaret().setBlinkRate(0); 083 084 help.setEnabled(on); 085 North.setEnabled(on); 086 South.setEnabled(on); 087 East.setEnabled(on); 088 West.setEnabled(on); 089 } 090 091 /** 092 * Set up graphical user interface. 093 */ 094 private void createGUI() 095 { 096 myFrame = new JFrame("À la recherche du Père Noël"); 097 entryField = new JTextField(34); 098 099 // Attentif aux actions sur les boutons : 100 log = new JTextArea(); 101 log.setEditable(false); 102 JScrollPane listScroller = new JScrollPane(log); 103 listScroller.setPreferredSize(new Dimension(700, 180)); 104 listScroller.setMinimumSize(new Dimension(100,100)); 105 106 JPanel panel = new JPanel(); 107 image = new JLabel(); 108 109 JPanel bouttons = new JPanel(); 110 bouttons.setSize(300, 300); 111 bouttons.setLayout(new BorderLayout()); 112 113 help = new JButton ("AIDE"); 114 help.addActionListener(this); 115 bouttons.add(help, BorderLayout.CENTER); 116 117 North = new JButton("NORD"); 118 North.addActionListener(this); 119 bouttons.add(North, BorderLayout.NORTH); 120 121 South = new JButton("SUD"); 122 South.addActionListener(this); 123 bouttons.add(South, BorderLayout.SOUTH); 124 125 East = new JButton("EST"); 126 East.addActionListener(this); 127 bouttons.add(East, BorderLayout.EAST); 128 129 West = new JButton("OUEST"); 130 West.addActionListener(this); 131 bouttons.add(West, BorderLayout.WEST); 132 133 134 JPanel centerPanel = new JPanel(); 135 centerPanel.setLayout(new BorderLayout()); 136 centerPanel.add(listScroller,BorderLayout.CENTER); 137 centerPanel.add(bouttons,BorderLayout.EAST); 138 139 panel.setLayout(new BorderLayout()); 140 panel.add(image, BorderLayout.NORTH); 141 panel.add(centerPanel, BorderLayout.CENTER); 142 panel.add(entryField, BorderLayout.SOUTH); 143 144 myFrame.getContentPane().add(panel, BorderLayout.CENTER); 145 146 147 // add some event listeners to some components 148 myFrame.addWindowListener(new WindowAdapter() 149 { 150 public void windowClosing(WindowEvent e) {System.exit(0); 151 } 152 153 }); 154 155 entryField.addActionListener(this); 156 157 myFrame.pack(); 158 myFrame.setVisible(true); 159 entryField.requestFocus(); 160 } 161 162 /** 163 * Actionlistener interface for entry textfield. 164 */ 165 public void actionPerformed(ActionEvent e) 166 { 167 if (e.getSource()==North) 168 engine.interpretCommand("aller nord"); 169 else if (e.getSource()==South) 170 engine.interpretCommand("aller sud"); 171 else if (e.getSource()==East) 172 engine.interpretCommand("aller est"); 173 else if (e.getSource()==West) 174 engine.interpretCommand("aller ouest"); 175 else if (e.getSource()==help) 176 engine .interpretCommand ("aide"); 177 else 178 processCommand(); 179 180 } 181 182 /** 183 * A command has been entered. Read the command and do whatever is 184 * necessary to process it. 185 */ 186 private void processCommand() 187 { 188 boolean finished = false; 189 String input = entryField.getText(); 190 entryField.setText(""); 191 192 engine.interpretCommand(input); 193 } 194 195}