import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Crt5 extends JComponent implements KeyEventDispatcher,
ActionListener {
private static JFrame f;
private static Timer timer;
private static int qtdColunas = 80;
private static int qtdLinhas = 25;
private static int fontSize = 13;
private static Font fonte = new Font("monospaced", Font.BOLD, fontSize);
private static int charWidth;
private static int charHeight;
private static int width;
private static int height;
private static Color backgroundColor = Color.BLACK;
private static Color textColor = Color.WHITE;
private static char[][] conteudo = new char[qtdLinhas][qtdColunas];
private static int cursorX = 1;
private static int cursorY = 1;
private static boolean cursorOn = true;
private static int cursorCount = 0;
private static int cursorTime = 350;
private static boolean cursorBlink = false;
private static boolean isKeyPressing = false;
private static boolean isKeyPressed = false;
private static char keyPressed = Character.UNASSIGNED;
private static int codeKeyPressed = -1;
private static int ticks = 1;
// ////m�todo de inicializa��o da janela
public static void init() {
f = new JFrame();
f.setTitle("APFJogos - Java Text Mode Emulator (JTM)");
f.setContentPane(new Crt5());
f.setBackground(backgroundColor);
f.setForeground(textColor);
f.setFont(fonte);
f.getContentPane().setBackground(backgroundColor);
f.getContentPane().setFont(fonte);
KeyboardFocusManager manager = KeyboardFocusManager
.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher((KeyEventDispatcher) f.getContentPane());
FontMetrics fonteM = f.getFontMetrics(f.getFont());
int ascent = fonteM.getMaxAscent();
int descent = fonteM.getMaxDescent();
int advance = fonteM.getMaxAdvance();
charWidth = advance;
charHeight = descent + ascent;
charWidth = 8;
charHeight = 12;
height = charHeight * qtdLinhas + 40;
width = charWidth * qtdColunas + 16;
f.setSize(width, height);
// f.setBounds(100,50,500,300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
clrScr();
timer = new Timer(ticks, (ActionListener) f.getContentPane());
timer.start();
System.out.println("ascent=" + ascent + ", descent= " + descent
+ ", advance=" + advance + ". width=" + width + ", height="
+ height);
}
// ////m�todos referentes a janela Swing: paint(), e eventos de
// KeyListener()
public void paint(Graphics g) {
for (int linha = 0; linha < qtdLinhas; linha++) {
String s = new String(conteudo[linha]);
g.drawString(s, 0, linha * charHeight + charHeight);
}
if (cursorOn) {
if (!cursorBlink) g.setColor(Color.BLACK);
g.fillRect(cursorX * charWidth - charWidth, cursorY * charHeight,
charWidth, 3);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
isKeyPressed = isKeyPressing;
if (cursorOn) {
cursorCount++;
if (cursorCount>cursorTime) {
cursorBlink=!cursorBlink;
cursorCount=0;
}
}
f.repaint();
}
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
keyPressed(e);
} else if (e.getID() == KeyEvent.KEY_RELEASED) {
keyReleased(e);
} else if (e.getID() == KeyEvent.KEY_TYPED) {
keyTyped(e);
}
return false;
}
public void keyPressed(KeyEvent e) {
keyPressed = e.getKeyChar();
codeKeyPressed = e.getKeyCode();
//isKeyPressing = true;
//isKeyPressed = true;
}
public void keyTyped(KeyEvent e) {
isKeyPressing = true;
//isKeyPressed = true;
// System.out.println("pressionei");
}
public void keyReleased(KeyEvent e) {
isKeyPressing = false;
//isKeyPressed = true;
keyPressed = '\u0000';
codeKeyPressed=-1;
// /System.out.println("Despressionei");
}
// ////m�todos referente ao cursor de impress�o
public static void gotoXY(int x, int y) {
if ((x < 1) || (x > qtdColunas)) {
x = 1;
y = 1;
}
if ((y < 1) || (y > qtdLinhas)) {
x = 1;
y = 1;
}
cursorX = x;
cursorY = y;
}
public static void cursorOn() {
cursorOn = true;
}
public static void cursorOff() {
cursorOn = false;
}
public static void setCursorMode(boolean b) {
cursorOn = b;
}
public static boolean getCursorMode() {
return cursorOn;
}
public static int getCursorX() {
return cursorX;
}
public static int getCursorY() {
return cursorY;
}
// //// m�todos referente a tela: clrScr (apagar a tela toda)
public static void clrScr() {
for (int linha = 0; linha < qtdLinhas; linha++) {
for (int coluna = 0; coluna < qtdColunas; coluna++) {
conteudo[linha][coluna] = ' ';
}
}
gotoXY(1, 1);
if (f == null)
init();
//f.repaint();
}
private static void subirLinhas() {
for (int i = 1; i < qtdLinhas; i++) {
for (int j = 0; j < qtdColunas; j++) {
conteudo[i - 1][j] = conteudo[i][j];
}
}
for (int j = 0; j < qtdColunas; j++)
conteudo[qtdLinhas - 1][j] = ' ';
}
private static void carriageReturnLineFeed() {
cursorX = 1;
cursorY++;
if (cursorY > qtdLinhas) {
subirLinhas();
cursorY = qtdLinhas;
}
}
// ////m�todos de impress�o na tela
public static void write() {
if (f == null) init();
}
public static void write(String s) {
for (int i = 0; i < s.length(); i++) {
conteudo[cursorY - 1][cursorX - 1] = s.charAt(i);
cursorX++;
if (cursorX > qtdColunas) {
carriageReturnLineFeed();
}
}
write();
}
public static void write(Object... args) {
String s = new String();
for (int i = 0; i < args.length; i++) {
s = s + args[i].toString();
}
write(s);
}
public static void writeln() {
write();
carriageReturnLineFeed();
}
public static void writeln(String s) {
write(s);
carriageReturnLineFeed();
}
public static void writeln(Object... args) {
write(args);
carriageReturnLineFeed();
}
// ////m�todo de temporiza��o
public static void delay(int milliseconds) {
// timing=milliseconds/ticks;
// while(timing>0);
try {
Thread.sleep(milliseconds);
} catch (Exception e) {
e.printStackTrace();
}
//long a = System.currentTimeMillis();
//while((System.currentTimeMillis()-a)<milliseconds);
//System.out.println("delay="+(System.currentTimeMillis()-a));
}
// ////m�todos referentes a leitura do teclado
public static boolean isKeyPressed() {
return isKeyPressed;
}
public static char readKey() {
while (!isKeyPressed) delay(1); //necess�rio para funcionar em computadores mais r�pidos!
return keyPressed;
}
public static String typeText() {
char c;
int xCursorInicial = cursorX;
int yCursorInicial = cursorY;
int delta = qtdColunas-xCursorInicial+1;
int indexString = 0;
int xCursor = xCursorInicial;
int yCursor = yCursorInicial;
boolean cursorMode = getCursorMode();
cursorBlink=true;
cursorOn();
StringBuffer s = new StringBuffer("");
do {
keyPressed='\u0000';
isKeyPressed=false;
c = readKey();
if (s.length()<256) {
if (((int) c >= 32) && ((int) c <= 126)) {
s.insert(indexString, c);
indexString++;
}
}
if ((int) c == 8) { // <BACKSPACE>
//s.replace(arg0, arg1, arg2)
indexString--;
}
if ((int) c == 127) { // <DEL>
//s.replace(arg0, arg1, arg2)
indexString--;
}
if (indexString<0) indexString=0;
if (indexString>255) indexString=255;
gotoXY(xCursorInicial, yCursorInicial);
write(s);
xCursor = 1+(indexString+xCursorInicial-1)%(qtdColunas);
yCursor = yCursorInicial+(indexString+xCursorInicial-1)/(qtdColunas);
gotoXY(xCursor, yCursor);
System.out.println("iS="+indexString+" xC="+xCursor+
" yC="+yCursor+" " +c + "=" + (int) c +
" s("+s.length()+")=" + s);
} while ((int) c != 10);
setCursorMode(cursorMode);
cursorBlink=false;
return s.toString();
}
}