import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
public class GamePanel extends JPanel implements KeyListener {
private JPanel commandPanel = null;
private JPanel healthManaPanel = null;
private JLabel hplbl = null;
private JProgressBar healthPB = null;
private JLabel mplbl = null;
private JProgressBar manaPB = null;
private JProgressBar experiencePB = null;
private JPanel chatMenuPanel = null;
private JPanel charUsePanel = null;
private JPanel chatPanel = null;
private JPanel menuPanel = null;
private JButton btnStart = null;
private JScrollPane chatAreaSP = null;
private JTextField inputTF = null;
private JTextArea chatAreaTA = null;
public GamePanel() {
super();
initialize();
}
private void initialize() {
this.setLayout(new BorderLayout());
this.setSize(800,600);
this.setOpaque(false);
this.add(getCommandPanel(), java.awt.BorderLayout.SOUTH);
this.setFocusable(true);
this.addKeyListener(this);
}
public void keyPressed(KeyEvent e){
System.out.println("KeyPressed"+ e.getKeyCode());
int key = e.getKeyCode();
if(key == KeyEvent.VK_ENTER){
String text = inputTF.getText().trim();
if(inputTF.isVisible()&& text.equals("")){
inputTF.setVisible(false);
repaint();
}
else{
if(!inputTF.isVisible()){
inputTF.setVisible(true);
repaint();
}
if(!text.trim().equals("")){
//To be send to the server to all clients
// This is a simulation
chatAreaTA.append("testuser: "+ text);
inputTF.setText("");
}
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
private JPanel getCommandPanel() {
if (commandPanel == null) {
commandPanel = new JPanel();
commandPanel.setLayout(new BorderLayout());
commandPanel.add(getChatMenuPanel(), java.awt.BorderLayout.EAST);
commandPanel.add(getCharUsePanel(), java.awt.BorderLayout.CENTER);
}
return commandPanel;
}
private JPanel getHealthManaPanel() {
if (healthManaPanel == null) {
hplbl = new JLabel();
mplbl = new JLabel();
healthManaPanel = new JPanel();
GridBagConstraints gbc = new GridBagConstraints();
healthManaPanel.setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.ipadx = 15;
gbc.insets = new Insets(5,10,5,0);
hplbl.setText("HP");
mplbl.setText("MP");
healthManaPanel.add(hplbl, gbc);
gbc.gridx++;
healthManaPanel.add(getHealthPB(), gbc);
gbc.gridx = 0;
gbc.gridy++;
healthManaPanel.add(mplbl, gbc);
gbc.gridx++;
healthManaPanel.add(getManaPB(), gbc);
}
return healthManaPanel;
}
private JProgressBar getHealthPB() {
if (healthPB == null) {
healthPB = new JProgressBar();
healthPB.setValue(75);
healthPB.setString("Test");
healthPB.setStringPainted(true);
}
return healthPB;
}
private JProgressBar getManaPB() {
if (manaPB == null) {
manaPB = new JProgressBar();
manaPB.setString("test2");
manaPB.setStringPainted(true);
manaPB.setValue(65);
}
return manaPB;
}
private JProgressBar getExperiencePB() {
if (experiencePB == null) {
experiencePB = new JProgressBar();
experiencePB.setStringPainted(true);
}
return experiencePB;
}
private JPanel getChatMenuPanel() {
if (chatMenuPanel == null) {
chatMenuPanel = new JPanel();
chatMenuPanel.setLayout(new BorderLayout());
chatMenuPanel.add(getChatPanel(), java.awt.BorderLayout.WEST);
chatMenuPanel.add(getMenuPanel(), java.awt.BorderLayout.CENTER);
}
return chatMenuPanel;
}
private JPanel getCharUsePanel() {
if (charUsePanel == null) {
charUsePanel = new JPanel();
charUsePanel.setLayout(new BorderLayout());
charUsePanel.add(getExperiencePB(), java.awt.BorderLayout.NORTH);
charUsePanel.add(getHealthManaPanel(), java.awt.BorderLayout.WEST);
}
return charUsePanel;
}
private JPanel getChatPanel() {
if (chatPanel == null) {
chatPanel = new JPanel();
chatPanel.setLayout(new BorderLayout());
chatPanel.add(getInputTF(), java.awt.BorderLayout.NORTH);
chatPanel.add(getChatAreaSP(), java.awt.BorderLayout.CENTER);
}
return chatPanel;
}
private JPanel getMenuPanel() {
if (menuPanel == null) {
menuPanel = new JPanel();
menuPanel.add(getBtnStart(), null);
}
return menuPanel;
}
private JButton getBtnStart() {
if (btnStart == null) {
btnStart = new JButton();
btnStart.setText("Start");
btnStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return btnStart;
}
private JScrollPane getChatAreaSP() {
if (chatAreaSP == null) {
chatAreaSP = new JScrollPane();
chatAreaSP.setViewportView(getChatAreaTA());
}
return chatAreaSP;
}
private JTextField getInputTF() {
if (inputTF == null) {
inputTF = new JTextField();
inputTF.setVisible(false);
inputTF.setColumns(15);
inputTF.setDocument(new FixedSizePlainDocument(40));
}
return inputTF;
}
class FixedSizePlainDocument extends PlainDocument {
int maxSize;
public FixedSizePlainDocument(int limit) {
maxSize = limit;
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if ((getLength() + str.length()) <= maxSize) {
super.insertString(offs, str, a);
} else {
throw new BadLocationException("Insertion exceeds max size of document", offs);
}
}
}
private JTextArea getChatAreaTA() {
if (chatAreaTA == null) {
chatAreaTA = new JTextArea();
chatAreaTA.setEditable(false);
chatAreaTA.setColumns(15);
chatAreaTA.setLineWrap(true);
}
return chatAreaTA;
}
}
Hi I would like to know how to implement the KeyPressed function… I have place in the code. But for some reason it does not work with the main application. I have to minimize the application, is then the KeyPressed would detect the KeyPressed. However once I place in the game logic for it. It just refuse to work!
Basically what I am trying to do is that upon the pressed of the Enter button (VK_ENTER) by the user the TextField would be setVisible(true). Then when the enter button is press again, the software would detect for any text within the field, if there is it would append into the text area just below the textfield. But if there are no text, then the textfield would be setVisible(false).
So I hope there is a solution for this… Many Thanks.
