crashtest-player/src/de/ctdo/crashtest/cardplayer/MainFrame.java

161 lines
5.4 KiB
Java

package de.ctdo.crashtest.cardplayer;
import de.ctdo.crashtest.cardplayer.irc.*;
import javax.swing.*;
import javax.swing.UIManager.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.net.URL;
import java.util.Vector;
public class MainFrame extends JFrame {
private IrcClient irc;
private JList<String> jListWall;
private JList<String> jListStates;
public MainFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("CrastestCardPlayer");
setPreferredSize(new Dimension(800,500));
irc = new IrcClient(HostHelper.getHostName().toLowerCase(), "#crashtest", "irc.ctdo.de", 6667);
setIcon();
setLookAndFeel();
initGui();
setVisible(true);
}
private void setIcon() {
try {
URL url = Main.class.getResource("/icon.png");
if(url != null) {
ImageIcon icon = new ImageIcon(url);
this.setIconImage(icon.getImage());
}
} catch (Exception ignored) { }
}
private void setLookAndFeel() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
UIManager.put("nimbusLightBackground", Color.BLACK);
UIManager.put("text", new Color(0, 240, 0));
UIManager.put("nimbusSelectionBackground", new Color(0 ,98, 0));
UIManager.put("nimbusFocus", new Color(0 ,255, 0));
UIManager.put("RootPane.background", Color.BLACK);
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
}
private void initGui() {
Container contentPane = this.getContentPane();
jListWall = new JList<String>(readData());
jListWall.setLayoutOrientation(JList.VERTICAL);
jListWall.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jListWall.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = jListWall.locationToIndex(e.getPoint());
ListModel dlm = jListWall.getModel();
Object item = dlm.getElementAt(index);
jListWall.ensureIndexIsVisible(index);
if (irc != null) irc.say(">wall " + item);
}
}
});
JScrollPane listScroller = new JScrollPane(jListWall);
Vector<String> states = new Vector<String>();
states.add("-> reset game");
states.add("IDLE");
states.add("ENTERED_ROOM");
states.add("TABLE_GAME_ONE");
states.add("TABLE_GAME_TWO");
states.add("TABLE_GAME_THREE");
states.add("TABLE_GAME_FOUR");
states.add("TABLE_GAME_FIVE");
states.add("TABLE_GAME_SIX");
states.add("TABLE_GAME_DONE");
states.add("TABLE_GAME_WRONG");
states.add("ROKET_STARTED");
states.add("ROKET_DONE");
states.add("NEUTRAL");
jListStates = new JList<String>(states);
jListStates.setLayoutOrientation(JList.VERTICAL);
jListStates.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jListStates.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = jListStates.locationToIndex(e.getPoint());
ListModel dlm = jListStates.getModel();
Object item = dlm.getElementAt(index);
jListStates.ensureIndexIsVisible(index);
if (irc != null) {
if("-> reset game".contentEquals(item.toString())) {
irc.say(">reset");
} else {
irc.say(">state " + item);
}
}
}
}
});
JScrollPane listScroller2 = new JScrollPane(jListStates);
listScroller2.setPreferredSize(new Dimension(150, 500));
contentPane.setLayout(new BorderLayout());
contentPane.add(listScroller2, BorderLayout.EAST);
contentPane.add(listScroller);
pack();
}
private Vector<String> readData() {
try {
File file;
BufferedReader br;
file = new File ("messages.txt");
if(file.exists()) {
br = new BufferedReader(new FileReader (file));
} else {
InputStream is = Main.class.getResourceAsStream("/messages.txt");
br = new BufferedReader(new InputStreamReader(is));
}
Vector<String> lines = new Vector<String>();
String line;
while((line=br.readLine())!=null) {
lines.add(line);
}
return lines;
} catch(Exception e) {
e.printStackTrace();
Vector<String> vector = new Vector<String>();
vector.add("could not read the input file...");
return vector;
}
}
}