crashtest/src/de/ctdo/crashtest/gui/MainGui.java

170 lines
4.9 KiB
Java

package de.ctdo.crashtest.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
public class MainGui extends JFrame implements IGuiControl {
private final JTextArea textWall = new JTextArea();
private final JLabel countDown = new JLabel();
private final JLabel extraField = new JLabel();
private final JPanel lowerPanel = new JPanel();
private final List<GuiEventListener> listenerList = new ArrayList<GuiEventListener>();
public MainGui() {
initGui();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(0,0, 1280, 1024);
setExtendedState(Frame.MAXIMIZED_BOTH);
setUndecorated(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char lastKey = e.getKeyChar();
for(GuiEventListener listener: listenerList) {
listener.keyPress(lastKey);
}
}
});
addWindowStateListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
for(GuiEventListener listener: listenerList) {
listener.windowClosing();
}
}
});
setVisible(true);
}
private void setCountDownText(final int tseconds) {
int mins = tseconds / 600;
int secs = tseconds % 600 / 10 ;
int tsecs = tseconds % 9;
countDown.setText(" " + mins + ":" + secs + "." + tsecs);
if(tseconds < 400) {
double percentile = ((tseconds-100.0)/300.0);
double red = 255.0 - percentile * 255.0;
double green = 255.0 - red;
double blue = percentile * 100.0;
// System.out.println("red= " + red + " green=" + green + " blue="+blue);
if(red > 255) red = 255;
if(red < 0) red = 0;
if(green > 255) green = 255;
if(green < 0) green = 0;
if(blue > 255) blue = 255;
if(blue < 0) blue = 0;
Color fColor = new Color((int)red,(int)green, (int)blue);
countDown.setForeground(fColor);
}
else {
countDown.setForeground(new Color(0x00, 190, 100));
}
}
private void initGui() {
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(textWall, BorderLayout.NORTH);
container.add(lowerPanel, BorderLayout.SOUTH);
lowerPanel.setLayout(new BorderLayout());
lowerPanel.add(countDown, BorderLayout.WEST);
lowerPanel.add(extraField, BorderLayout.EAST);
Font wallFont = new Font("Arcade Interlaced", Font.PLAIN, 66);
Font lowerFont = new Font("Arcade Interlaced", Font.PLAIN, 80);
Color fontColor = new Color(0x00, 190, 100);
textWall.setFont(wallFont);
textWall.setForeground(fontColor);
textWall.setBackground(Color.BLACK);
textWall.setText("follow the white rabbit...");
textWall.setLineWrap(true);
textWall.setWrapStyleWord(true);
textWall.setFocusable(false);
countDown.setFont(lowerFont);
countDown.setHorizontalAlignment(SwingConstants.LEFT);
countDown.setForeground(fontColor);
extraField.setFont(lowerFont);
extraField.setHorizontalAlignment(SwingConstants.RIGHT);
extraField.setForeground(fontColor);
lowerPanel.setBackground(Color.BLACK);
container.setBackground(Color.BLACK);
countDown.setText(" 15:00.0");
extraField.setText(" ");
}
@Override
public void setWall(final String message) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
textWall.setText(message);
}
};
SwingUtilities.invokeLater(runnable);
}
@Override
public void setExtra(final String text) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
extraField.setText(text + " ");
}
};
SwingUtilities.invokeLater(runnable);
}
@Override
public void setCountDown(final int tseconds) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
setCountDownText(tseconds);
}
};
SwingUtilities.invokeLater(runnable);
}
@Override
public void showCountDown(final Boolean show) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
countDown.setVisible(show);
}
};
SwingUtilities.invokeLater(runnable);
}
@Override
public void addListener(final GuiEventListener listener) {
listenerList.add(listener);
}
}