I'm currently working on a project which includes as a front-end a swing interface. This interface consists of little more than a JTextPane and a with JScrollPane/Bar, and a JTextField: the former for output and the latter for input.
The major components of the program itself, aside from the front-end, include a
thread pool, a bunch of tasks initiated by 'monster' objects which wait on a queue, and the client inputting commands to control his own 'player' object.
Currently I'm experiencing what I believe to be an inordinate amount of 'lag' or slowdown when several monsters and the player are all interacting with each other simultaneously. I believe this to be inordinate due to the fact that:
A) I am on a reasonably powerful computer(able to run Doom 3 with all the bells and whistles).
B) It's a text-based game, for crying out loud =)
I'm looking into the other aspects of my program, but I was wondering if anyone better acquainted with swing/awt could let me know what kind of overhead I'm using in regards to system resources with my 'Display' class, which acts as the front-end.
Thanks a ton, and here it is (abridged):
<blockquote>
code:
<pre name="code" class="core">
// Creates and manages swing components
// Receives output information and displays it
// Can and should request input information via prompt(String query) method
package navea.system;
import java.util.*;
import navea.mobs.Player;
import navea.mobs.Mob;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Display implements ActionListener {
static Display display;
static JFrame frame;
static JTextPane textArea;
static StyledDocument styledDocument;
static JScrollPane scroller;
static JScrollBar verticalScroll;
static JTextField textField;
static Player player;
// The following boolean indicates whether or not client has entered information
static boolean actionPerformed = false;
// Initialization
public Display() {
frame = new JFrame();
textArea = new JTextPane();
Font font = new Font("Sans Serif", Font.PLAIN, 13);
textArea.setBackground(Color.BLACK);
Display.addStyle("ordinary", font, Color.getHSBColor(153, 153, 153));
textArea.setEditable(false);
styledDocument = textArea.getStyledDocument();
scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
verticalScroll = scroller.getVerticalScrollBar();
textField = new JTextField();
textField.setFont(font);
textField.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER, scroller);
frame.getContentPane().add(BorderLayout.SOUTH, textField);
frame.setSize(800, 750);
frame.setVisible(true); }
public static Display getInstance() {
if(display == null) { display = new Display(); } return display; }
public static void setPlayer(Player p) { player = p; }
// Output
// Output removed for ease of reading.
// ****Input****
// Where all the magic is supposed to happen.
// Sets actionPerformed to false, then just waits for the actionListener
// to indicate input has occured
public static ArrayList<String> prompt(String query) {
actionPerformed = false;
try { styledDocument.insertString(styledDocument.getLength(),
"\n " + query, styledDocument.getStyle("ordinary"));
} catch (Exception ex) { }
scrollToBottom();
while(actionPerformed == false) {
Thread.yield(); }
try { styledDocument.insertString(styledDocument.getLength(),
"\n " + textField.getText(), styledDocument.getStyle("ordinary"));
} catch (Exception ex) { }
String input[] = textField.getText().split(" ");
ArrayList<String> inputTokens = new ArrayList<String>();
for(String i: input) { inputTokens.add(i); }
for(int i = inputTokens.size(); i < 4; i++) {
inputTokens.add(" "); }
return inputTokens;
}
// Stylistic
public static void scrollToBottom() {
textArea.setCaretPosition(textArea.getDocument().getLength()); }
public static String capitalizeFirst(String text) {
String capitalizedStart = text.substring(0, 1).toUpperCase();
StringBuilder builder = new StringBuilder(text);
return builder.replace(0, 1, capitalizedStart).toString(); }
public static void addStyle(String name, Font font, Color color) {
Style style = textArea.addStyle("ordinary", null);
StyleConstants.setFontFamily(style, font.getFamily());
StyleConstants.setFontSize(style, font.getSize());
StyleConstants.setForeground(style, color);
StyleConstants.setBold(style, true); }
// TextField Listener
public void actionPerformed(ActionEvent av) {
actionPerformed = true;
try { styledDocument.insertString(styledDocument.getLength(),
"\n ", styledDocument.getStyle("ordinary"));
} catch (Exception ex) { }
textField.selectAll();
scrollToBottom(); }
} </pre>
</blockquote>
[ July 15, 2008: Message edited by: Alex Birmingham ]