import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Console extends JFrame {
public final PipedInputStream in = new PipedInputStream();
private PrintStream commandOut;
private JTextPane displayPane;
private JScrollPane displayPaneScroll;
private JTextField inputField;
private DefaultStyledDocument doc;
// Prompt
private String input = "> ";
public Console() throws IOException {
commandOut = new PrintStream(new PipedOutputStream(in));
setTitle("Meine Konsole");
setSize(500, 600);
setLocation(100,100);
Container myPane = getContentPane();
doc = new DefaultStyledDocument();
inputField = new JTextField();
inputField.setText(input);
inputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
commandOut.println(e.getActionCommand());
commandOut.flush();
inputField.setText(input);
}
});
displayPane = new JTextPane(doc);
displayPaneScroll = new JScrollPane(displayPane,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
displayPane.setBackground(Color.black);
displayPane.setEditable(false);
inputField.setBackground(Color.black);
inputField.setForeground(Color.white);
myPane.add(inputField, BorderLayout.SOUTH);
myPane.add(displayPaneScroll, BorderLayout.CENTER);
// Window Listener zum Schließen der Konsole
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void print(String text) {
print(text, 255, 255, 255);
}
public void print(String text, int r, int g, int b) {
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, 12);
StyleConstants.setForeground(attr, new Color(r, g, b));
try {
doc.insertString(doc.getLength(), text, attr);
} catch (BadLocationException ex) {
}
}
public void println(String text) {
println(text, 255, 255, 255);
}
public void println(String text, int r, int g, int b) {
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, 12);
StyleConstants.setForeground(attr, new Color(r, g, b));
try {
doc.insertString(doc.getLength(), text + "\n", attr);
} catch (BadLocationException ex) {
}
}
public void clearScreen() {
displayPane.setText("");
}
}