Wednesday, 7 August 2013

Allowing different styles in JAVA to work per line and per users choice

Allowing different styles in JAVA to work per line and per users choice

I run an online game that has been around for a while and I am doing some
revamping to the cosmetics to add some more functionality to the chat
area. I would like people when they click, Plain, Bold or Italic for the
game to update their individual line with their prefered Font.STYLE. Like
they do on those fancy Java based chat applications.
How would I set it to work like I want it to? via the "PrintLine" or
"TextArea"
This is my current code.
import java.awt.Panel;
import java.awt.Button;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.BorderLayout;
import java.awt.event.*;
public class chatPne extends Panel implements ActionListener, KeyListener,
constants {
private static final long serialVersionUID = -8774683716313001058L;
pClient parent = null;
TextArea textArea = null;
Button sendButton = null;
Button clearButton = null;
Button whisperButton = null;
Button boldButton = null;
Button plainButton = null;
Button italicButton = null;
TextField theTextField = null;
boolean active = false;
Panel controlPane = new Panel();
Panel buttonPane = new Panel();
Panel buttonPane2 = new Panel();
public chatPne(pClient c) {
parent = c;
setBackground(backgroundColor);
textArea = new TextArea("", 3, 60, TextArea.SCROLLBARS_VERTICAL_ONLY);
textArea.setFont(ChatFont);
textArea.setEditable(false);
textArea.addKeyListener(parent);
sendButton = new Button("Send");
sendButton.setFont(ButtonFont);
sendButton.addActionListener(this);
sendButton.setEnabled(false);
clearButton = new Button("Clear");
clearButton.setFont(ButtonFont);
clearButton.setActionCommand("Clear");
clearButton.addActionListener(this);
whisperButton = new Button ("Whisper");
whisperButton.setFont (ButtonFont);
whisperButton.setActionCommand ("Whisper");
//whisperButton.addActionListener (this);
whisperButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
theTextField.setText("/whisper NAMEHERE MSGHERE");
}
});
whisperButton.setEnabled (false);
boldButton = new Button("B");
boldButton.setFont (BoldFont);
boldButton.setActionCommand ("Bold");
boldButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
boldboxActionPerformed(evt);
}
});
boldButton.setEnabled (false);
plainButton = new Button("P");
plainButton.setFont (ButtonFont);
plainButton.setActionCommand ("Plain");
plainButton.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
plainboxActionPerformed(evt);
}
});
plainButton.setEnabled (false);
italicButton = new Button("I");
italicButton.setFont (ItalicFont);
italicButton.setActionCommand ("Italic");
italicButton.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
italicboxActionPerformed(evt);
}
});
italicButton.setEnabled (false);
theTextField = new TextField();
theTextField.setFont(ChatFont);
theTextField.addActionListener(this);
theTextField.addKeyListener(this);
buttonPane.setLayout(new BorderLayout());
buttonPane.add("West", clearButton);
buttonPane.add("Center", whisperButton);
buttonPane.add("East", sendButton);
controlPane.setLayout(new BorderLayout());
controlPane.add("West", buttonPane);
controlPane.add("Center", theTextField);
controlPane.add("East", buttonPane2);
buttonPane2.setLayout(new BorderLayout());
buttonPane2.add("West", boldButton);
buttonPane2.add("Center", italicButton);
buttonPane2.add("East", plainButton);
this.setLayout(new BorderLayout());
this.add("South", controlPane);
this.add("Center", textArea);
}
public void PrintLine() {
textArea.append(parent.readString() + "\n");
}
public void actionPerformed(ActionEvent evt) {
if (active) {
if (evt.getActionCommand().equals("Clear")) {
textArea.selectAll();
textArea.replaceRange("", textArea.getSelectionStart(),
textArea.getSelectionEnd());
} else {
String theString = theTextField.getText();
parent.sendString(C_CHAT_PACKET + theString + "\0");
theTextField.setText("");
}
}
return;
}
public void activateChat() {
active = true;
sendButton.setEnabled(true);
whisperButton.setEnabled (true);
boldButton.setEnabled(true);
plainButton.setEnabled(true);
italicButton.setEnabled(true);
}
public void deactivateChat() {
active = false;
sendButton.setEnabled(false);
whisperButton.setEnabled(false);
boldButton.setEnabled(false);
plainButton.setEnabled(false);
italicButton.setEnabled(false);
}
public void takeFocus() {
textArea.requestFocus();
}
/* handle function keys pressed in the chat window */
public void keyPressed(KeyEvent evt) {
int theKey;
theKey = evt.getKeyCode();
if (theKey == KeyEvent.VK_F1 || theKey == KeyEvent.VK_F2
|| theKey == KeyEvent.VK_F3 || theKey == KeyEvent.VK_F4
|| theKey == KeyEvent.VK_F5 || theKey == KeyEvent.VK_F6
|| theKey == KeyEvent.VK_F7 || theKey == KeyEvent.VK_F8) {
parent.keyPressed(evt);
}
return;
}
public void keyReleased(KeyEvent evt) {
;
}
public void keyTyped(KeyEvent evt) {
;
}
private void boldboxActionPerformed(java.awt.event.ActionEvent evt) {
textArea.setFont(BoldFont);
}
private void plainboxActionPerformed(java.awt.event.ActionEvent evt) {
textArea.setFont(ChatFont);
}
private void italicboxActionPerformed(java.awt.event.ActionEvent evt) {
textArea.setFont(ItalicFont);
}
}

No comments:

Post a Comment