Boa noite dsimao!
Cara não sei se um applet abre janelas mas para executar como applet e stand alone, vc precisa criar uma classe que tenha o metodo main, e essa classe não pode ser a mesma classe que extends Applet!!!
olha o seu codigo abaixo com minhas pequenas modificações:
PuzzleNumerico.java
[code]import javax.swing.JFrame;
import java.applet.Applet;
public class PuzzleNumerico extends Applet {
PuzzleGrafico frame = null;
public void init() {
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
showWindow();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
public void showWindow() {
JFrame window = new JFrame("Number Puzzle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new PuzzleGrafico());
window.pack(); // finaliza layout
window.setVisible(true); // torna janela visivel
window.setResizable(false);
}
}[/code]
PuzzleGrafico.java
[code]import java.awt.;
import java.awt.event.;
import javax.swing.;
import javax.swing.event.;
public class PuzzleGrafico extends JPanel {
private PuzzleGrafico.PainelGrafico _puzzleGraphics;
private PuzzleModelo _puzzleModel = new PuzzleModelo();
public PuzzleGrafico() {
// Criar um botão novo jogo, adicionar um listener para ele
JButton newGameButton = new JButton("Novo Jogo");
newGameButton.addActionListener(new PuzzleGrafico.NovoJogo());
// Criar painel de controlo
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(newGameButton);
// Criar graficos
_puzzleGraphics = new PuzzleGrafico.PainelGrafico();
// definir o Layout e adicionar componentes
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.NORTH);
this.add(_puzzleGraphics, BorderLayout.CENTER);
}
class PainelGrafico extends JPanel implements MouseListener {
private static final int ROWS = 3;
private static final int COLS = 3;
private static final int CELL_SIZE = 100; // Pixeis
private Font _biggerFont;
public PainelGrafico() {
_biggerFont = new Font("Times New Roman", Font.BOLD, CELL_SIZE/2);
this.setPreferredSize(
new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
this.setBackground(Color.blue);
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
int x = c * CELL_SIZE;
int y = r * CELL_SIZE;
String text = _puzzleModel.getFace(r, c);
if (text != null) {
g.setColor(Color.white);
g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
g.setColor(Color.black);
g.setFont(_biggerFont);
g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
}
}
}
}
public void mousePressed(MouseEvent e) {
// Mapa de coordenadas x,y em linha e coluna
int col = e.getX()/CELL_SIZE;
int row = e.getY()/CELL_SIZE;
if (!_puzzleModel.moveTile(row, col)) {
// moveTile move a peça,senao torna false
Toolkit.getDefaultToolkit().beep();
}
this.repaint(); // Mostra todas as actualizaçoes
}
// ignora estes eventos
public void mouseClicked (MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
}
public class NovoJogo implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
}
}
}[/code]
PuzzleModelo.java
[code]public class PuzzleModelo {
private static final int ROWS = 3;
private static final int COLS = 3;
private Peça[][] _contents;
private Peça _emptyTile;
public PuzzleModelo() {
_contents = new Peça[ROWS][COLS];
reset();
}
String getFace(int row, int col) {
return _contents[row][col].getFace();
}
public void reset() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
_contents[r][c] = new Peça(r, c, "" + (r*COLS+c+1));
}
}
_emptyTile = _contents[ROWS-1][COLS-1];
_emptyTile.setFace(null);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
exchangeTiles(r, c, (int)(Math.random()*ROWS)
, (int)(Math.random()*COLS));
}
}
}
public boolean moveTile(int r, int c) {
return checkEmpty(r, c, -1, 0) || checkEmpty(r, c, 1, 0)
|| checkEmpty(r, c, 0, -1) || checkEmpty(r, c, 0, 1);
}
private boolean checkEmpty(int r, int c, int rdelta, int cdelta) {
int rNeighbor = r + rdelta;
int cNeighbor = c + cdelta;
if (isLegalRowCol(rNeighbor, cNeighbor)
&& _contents[rNeighbor][cNeighbor] == _emptyTile) {
exchangeTiles(r, c, rNeighbor, cNeighbor);
return true;
}
return false;
}
public boolean isLegalRowCol(int r, int c) {
return r>=0 && r<ROWS && c>=0 && c<COLS;
}
private void exchangeTiles(int r1, int c1, int r2, int c2) {
Peça temp = _contents[r1][c1];
_contents[r1][c1] = _contents[r2][c2];
_contents[r2][c2] = temp;
}
public boolean isGameOver() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<ROWS; c++) {
Peça trc = _contents[r][c];
return trc.isInFinalPosition(r, c);
}
}
return true;
}
}
class Peça {
private int _row;
private int _col;
private String _face;
public Peça(int row, int col, String face) {
_row = row;
_col = col;
_face = face;
}
public void setFace(String newFace) {
_face = newFace;
}
public String getFace() {
return _face;
}
public boolean isInFinalPosition(int r, int c) {
return r==_row && c==_col;
}
}[/code]
ExecStandAlone.java
[code]public class ExecStandAlone {
public static void main(String[] args) {
PuzzleNumerico puzz = new PuzzleNumerico();
puzz.showWindow();
}
}[/code]
O arquivo ExecStandAlone.java deve ser definido como main-class dentro do manifest do jar
o arquivo PuzzleNumerico.java deve ser referenciado dentro do seu html na tag
igual tá abaixo:
[code]
Puzzle Number
Applet Puzzle Number
[/code]
não esquece que a versão do plugin JRE no seu navegador tem que ser a mesma versão do seu compilador java, ou seja, se vc ta usando um compilador java 1.6.31 vc tem que usar um plugin JRE 1.6.31 no seu firefox para não dar aquele problema de incompatibilidade de versão sacou?
O problema agora é o seguinte:
o Applet não abre uma nova janela (JFrame) então o JFrame tem que ficar só na parte do stand alone manja?
ai quando vc cria um novo objeto PuzzleNumerico lá na classe ExecStandAlone, vc tem que adicionar o JFrame lá…
não manjo nada de applets, por isso não posso ajudar muito mais 