package javachip8applet;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
import chip8emu.*;
import chip8emu.games.*;
public class Chip8Applet
extends Applet {
private boolean isStandalone = false;
private Chip8Emu emu;
private EmulatorPane emuPane;
private int zoom = 2;
private int keyDown = -1;
class EmulatorPane
extends Canvas
implements Chip8IO {
private BufferedImage pixelsImage;
private Dimension minSize = new Dimension(128 * zoom, 64 * zoom);
public boolean isOpaque()
{
return true;
}
public EmulatorPane() {
pixelsImage = new BufferedImage(128, 64, BufferedImage.TYPE_3BYTE_BGR);
emu = new Chip8Emu(this);
emu.loadGame(chip8emu.games.Games.getGameAsStream("Alien"));
emu.start(1);
setBackground(Color.black);
}
public Dimension preferredSize() {
return minimumSize();
}
public synchronized Dimension minimumSize() {
return minSize;
}
public int getKeys() {
return keyDown;
}
public void drawPixels(int[] pixels) {
pixelsImage.setRGB(0, 0, 128, 64, pixels, 0, 128);
repaint();
}
public void beep() {
}
public void paint(Graphics g) {
int x=(getWidth()-128*zoom)/2;
int y=(getHeight()-64*zoom)/2;
g.drawImage(pixelsImage, x, y, 128 * zoom, 64 * zoom, null);
}
}
//Initialize the applet
public void init() {
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
class KeyButton
extends Button {
private String key;
public KeyButton(String k) {
super(k);
key = k;
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
String ch=""+e.getKeyChar();
ch=ch.toUpperCase();
keyDown ="1234QWERASDFZXCV".indexOf(key);
};
public void keyReleased(KeyEvent e) {
keyDown=-1;
};
});
addMouseListener(new MouseAdapter() {
//public void mouseExited(MouseEvent e) {keyDown=-1;};
public void mousePressed(MouseEvent e) {
keyDown = "0123456789ABCDEF".indexOf(key);
};
public void mouseReleased(MouseEvent e) {
keyDown = -1;
};
});
}
}
private void createGUI() {
emuPane = new EmulatorPane();
setLayout(new BorderLayout());
Panel choicesPanel = new Panel();
final Choice choice = new Choice();
choice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
emu.stop();
int i=choice.getSelectedIndex();
if (i>=0 && i