001 002// import javax.swing.*; 003import javax.swing.JFrame; 004import javax.swing.JTextField; 005import javax.swing.JTextArea; 006import javax.swing.JLabel; 007import javax.swing.ImageIcon; 008import javax.swing.JPanel; 009import javax.swing.JScrollPane; 010import javax.swing.JButton; 011import java.awt.GridLayout; 012import java.util.HashMap; 013import java.util.Set; 014// import javax.swing.SwingUtilities; 015// import java.awt.*; 016import java.awt.Dimension; 017import java.awt.BorderLayout; 018 019// import java.awt.event.*; 020import java.awt.event.ActionListener; 021import java.awt.event.WindowAdapter; 022import java.awt.event.ActionEvent; 023import java.awt.event.WindowEvent; 024 025import java.net.URL; 026// import java.awt.image.*; 027 028/** 029 * This class implements a simple graphical user interface with a text entry 030 * area, a text output area and an optional image. 031 * 032 * @author Michael Kolling 033 * @version 1.0 (Jan 2003) DB edited (2019) 034 */ 035public class UserInterface implements ActionListener { 036 private GameEngine aEngine; 037 private JFrame aMyFrame; 038 private JTextField aEntryField; 039 private JTextArea aLog; 040 private JLabel aImage; 041 private HashMap<String,JButton> aButtons; 042 private JPanel aPanelButtons; 043 044 /** 045 * Construct a UserInterface. As a parameter, a Game Engine (an object 046 * processing and executing the game commands) is needed. 047 * 048 * @param gameEngine The GameEngine object implementing the game logic. 049 */ 050 public UserInterface(final GameEngine pGameEngine) { 051 this.aButtons = new HashMap<String,JButton>(); 052 this.aPanelButtons = new JPanel(); 053 aPanelButtons.setLayout(new GridLayout(2,1)); 054 this.aEngine = pGameEngine; 055 this.createGUI(); 056 } // UserInterface(.) 057 058 /** 059 * Print out some text into the text area. 060 */ 061 public void print(final String pText) { 062 this.aLog.append(pText); 063 this.aLog.setCaretPosition(this.aLog.getDocument().getLength()); 064 } // print(.) 065 066 /** 067 * Print out some text into the text area, followed by a line break. 068 */ 069 public void println(final String pText) { 070 this.print(pText + "\n"); 071 } // println(.) 072 073 /** 074 * Show an image file in the interface. 075 */ 076 public void showImage(final String pImageName) { 077 String vImagePath = "" + pImageName; // to change the directory 078 URL vImageURL = this.getClass().getClassLoader().getResource(vImagePath); 079 if (vImageURL == null) 080 System.out.println("Image not found : " + vImagePath); 081 else { 082 ImageIcon vIcon = new ImageIcon(vImageURL); 083 this.aImage.setIcon(vIcon); 084 this.aMyFrame.pack(); 085 } 086 } // showImage(.) 087 088 /** 089 * Enable or disable input in the input field. 090 */ 091 public void enable(final boolean pOnOff) { 092 this.aEntryField.setEditable(pOnOff); // enable/disable 093 if (!pOnOff) { // disable 094 this.aEntryField.getCaret().setBlinkRate(0); // cursor won't blink 095 this.aEntryField.removeActionListener(this); // won't react to entry 096 } 097 } // enable(.) 098 099 /** 100 * Set up graphical user interface. 101 */ 102 private void createGUI() { 103 this.aMyFrame = new JFrame("Trade Infinity Game !"); // change the title 104 this.aEntryField = new JTextField(34); 105 JButton bitcoinBtn = new JButton("Buy Bitcoin"); 106 JButton quitButton = new JButton("Quit"); 107 this.aButtons.put("bitcoin",bitcoinBtn); 108 this.aButtons.put("quit",quitButton); 109 110 this.aLog = new JTextArea(); 111 this.aLog.setEditable(false); 112 JScrollPane vListScroller = new JScrollPane(this.aLog); 113 vListScroller.setPreferredSize(new Dimension(200, 200)); 114 vListScroller.setMinimumSize(new Dimension(100, 100)); 115 116 JPanel vPanel = new JPanel(); 117 this.aImage = new JLabel(); 118 119 vPanel.setLayout(new BorderLayout()); // ==> only five places 120 vPanel.add(this.aImage, BorderLayout.NORTH); 121 vPanel.add(vListScroller, BorderLayout.CENTER); 122 vPanel.add(this.aEntryField, BorderLayout.SOUTH); 123 124 Set<String> allKeys = this.aButtons.keySet(); 125 for(String vKey : allKeys) { 126 this.aPanelButtons.add(this.aButtons.get(vKey)); 127 this.aButtons.get(vKey).addActionListener(this); 128 } 129 130 vPanel.add(this.aPanelButtons,BorderLayout.EAST); 131 this.aMyFrame.getContentPane().add(vPanel, BorderLayout.CENTER); 132 133 // add some event listeners to some components 134 this.aEntryField.addActionListener(this); 135 // to end program when window is closed 136 this.aMyFrame.addWindowListener(new WindowAdapter() { 137 public void windowClosing(WindowEvent e) { 138 System.exit(0); 139 } 140 }); 141 142 this.aMyFrame.pack(); 143 this.aMyFrame.setVisible(true); 144 this.aEntryField.requestFocus(); 145 } // createGUI() 146 147 /** 148 * Actionlistener interface for entry textfield. 149 */ 150 public void actionPerformed(final ActionEvent pE) { 151 if (pE.getActionCommand()=="Buy Bitcoin") { 152 this.aEngine.interpretCommand("buy bitcoin"); 153 } else if (pE.getActionCommand()=="Quit") { 154 this.aEngine.interpretCommand("quit"); 155 } else { 156 this.processCommand(); 157 } 158 } // actionPerformed(.) 159 160 public void disableButtons() { 161 Set<String> allButtons = this.aButtons.keySet(); 162 for (String vButton : allButtons) { 163 this.aButtons.get(vButton).setEnabled(false); 164 } 165 } 166 /** 167 * A command has been entered. Read the command and do whatever is necessary to 168 * process it. 169 */ 170 private void processCommand() { 171 String vInput = this.aEntryField.getText(); 172 this.aEntryField.setText(""); 173 174 this.aEngine.interpretCommand(vInput); 175 } // processCommand() 176} // UserInterface