abenobashi
13-04-2010, 09:34
Avrei intenzione di creare il gioco della vita con un applet sono riuscito a creare la griglia e a far ciclare il gioco pero volevo far in modo che le celle fossero cliccabili e che ci fosse un area clccabile(ma non un bottone) per lo start e uno per lo stop del gioco. A me fun zionava anche il click solo che facendo varie modifiche mi si sono rimpicciolite le caselle e mettendo anche l'area di testo è scomparsa la griglia. Qualcuno più esperto di me sarebbe in grado di darmi una mano? Sarei felicissimo se mi trovate l'errore. Vi mando le due versione quella che prima funzionava bene e poi quella che mi da problema se metto l'area di testo e le celle cliccabili.
Nel caso non conosciate la logica del gioco c' è spiegato qui :
http://it.wikipedia.org/wiki/Gioco_Life_di_Conway
Versione funzionante:
import java.awt.Graphics; //per la grafica
import java.awt.Color;//per i colori
import java.applet.*;//utilizzare gli apllet con java
import java.awt.event.*;//importazione dell'intero pacchetto per gli eventi del mouse
public class Giocodellavita extends Applet implements Runnable, MouseListener{
int i;
int j;
int k;
boolean pausa = true;//variabile per dire se il gioco è in pausa
int vicini = 0;//conteggio dei vicini
int[][] s;
int[][] m = new int[5][5];
public void run() {
while (true) {
if(!pausa){
repaint();
try {
Thread.sleep(1000);//attesa tra un ciclo e l'altro
} catch (InterruptedException ie) {
}
int[] mat_i = {-1, -1, -1, 0, 1, 1, 1, 0};
int[] mat_j = {-1, 0, 1, 1, 1, 0, -1, -1};
//nuova matrice
s = new int[5][5];
//colonna
for (i = 0; i < m.length; i++) {
//riga
for (j = 0; j < m[0].length; j++) {
vicini = 0;
//conteggio dei vicini
for (k = 0; k < 8; k++) {
//controllo
if ((i + mat_i[k]) >= 0 && ((i + mat_i[k]) < m.length) && (j + mat_j[k]) >= 0 && ((j + mat_j[k]) < m[0].length)) {
if (m[i + mat_i[k]][j + mat_j[k]] == 1) {
vicini++;
}
}
}
if (vicini < 2) {
s[i][j] = 0;
} else if (vicini == 2) {
s[i][j] = m[i][j];
} else if (vicini == 3) {
s[i][j] = 1;
} else if (vicini > 3) {
s[i][j] = 0;
}
}
}
}
m = s;
}
}
public void mouseClicked(MouseEvent me)
{
int x = me.getX();//cordinata x premuta
int y = me.getY();//cordinata y premuta
int i = x / (this.getWidth()/6);
int j = y / (this.getHeight()/ 6);
//cambio dello stato delle celle
if (m[i][j] == 1)
{
m[i][j] = 0;
}else{
m[i][j] = 1;
}
}
public void mousePressed(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
public void init() {
//inserimento dei dati nella matrice, prima azzero tutto
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[i].length; j++)
{
m[i][j] = 0;
}
}
//setto le variabili che voglio
m[1][2] = 1;
m[2][2] = 1;
m[3][2] = 1;
new Thread(this).start();
}
public void paint(Graphics g)
{
{
int w = this.getWidth() / (5 + 1);//righe
int h = this.getHeight() / (5 + 1);//colonne
int x = 0;
int y = 0;
g.setColor(Color.BLACK);
//creazione della tabella
for (int i = 0; i <= w; i++)
{
g.drawLine(x, y, this.getWidth(), y);
y += w;
}
//reset delle variabili x e y a 0
x = 0;
y = 0;
for (int i = 0; i <= h; i++)
{
g.drawLine(x, y, x, this.getWidth());
x += h;
}
this.addMouseListener(this);
//creazione delle celle di vita. Nere con vita mentre bianche con assenza di vita
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[i].length; j++)
{
if (m[i][j] == 1)//caselle vive
{
g.setColor(Color.BLACK);
g.fillRect((j * w) + 1, (i * h) + 1, w - 2, h - 2);
}else{//caselle morte
g.setColor(Color.WHITE);
g.fillRect((j * w) + 1, (i * h) + 1, w - 2, h - 2);
}
}
}
}
}
}
Versione con problema:
import java.awt.*; //per la grafica
import java.awt.image.*;
import java.awt.Color;//per i colori
import java.applet.*;//utilizzare gli apllet con java
import java.awt.event.*;//importazione dell'intero pacchetto per gli eventi del mouse
public class Giocodellavita extends Applet implements Runnable, MouseListener {
int i;
int j;
int k;
Image testt = null;
boolean pausa;
int vicini = 0;//conteggio dei vicini
int[][] m = new int[5][5];
public void run() {
while (true) {
if(!pausa){
repaint();
ProssimoCiclo();
try {
Thread.sleep(800);
} catch (Exception e) {
}
}
}
}
@Override
public void init() {
testt = createImage(300, 400);
//inserimento dei dati nella matrice, prima azzero tutto
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[i].length; j++)
{
m[i][j] = 0;
}
}
//setto le variabili che voglio
m[1][2] = 1;
m[2][2] = 1;
m[3][2] = 1;
new Thread(this).start();
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
//disegna la griglia
g.setColor(Color.BLACK);
//righe verticali
for (int i1 = 0; i1 <= 200; i1 = i1 + 20) {
g.drawLine(i1, 0, i1, 200);
}
//righe orizzontali
for (int j1 = 0; j1 <= 200; j1 = j1 + 20) {
g.drawLine(0, j1, 200, j1);
}
// controlla i valori immessi nella matrice e li disegna nell'applet
for (int i2 = 0; i2 < m.length; i2++) {
for (int j2 = 0; j2 < m.length; j2++) {
if (m[i2][j2] == 1) {
g.fillRect(j2 * 20, i2 * 20, 20, 20);
}
}
}
Graphics buffer = testt.getGraphics();
buffer.setColor(Color.black);
buffer.drawString("Attiva", 70, 300);
g.drawImage(testt, 0, 0, null);
}
public void ProssimoCiclo() {
int[][] s = new int[5][5];
int[] mat_i = {-1, -1, -1, 0, 1, 1, 1, 0};
int[] mat_j = {-1, 0, 1, 1, 1, 0, -1, -1};
//colonna
for (int i3 = 0; i3 < m.length; i3++) {
//riga
for (int j3 = 0; j3 < m[0].length; j3++) {
int vicini1 = 0;
//conta i vicini
for (int k1 = 0; k1 < 8; k1++) {
//controllo
if ((i3 + mat_i[k1]) >= 0 && ((i3 + mat_i[k1]) < m.length) && (j3 + mat_j[k1]) >= 0 && ((j3 + mat_j[k1]) < m[0].length)) {
if (m[i3 + mat_i[k1]][j3 + mat_j[k1]] == 1) {
vicini1++;
}
}
}
if (vicini1 < 2) {
s[i3][j3] = 0;
} else if (vicini1 == 2) {
s[i3][j3] = m[i3][j3];
} else if (vicini1 == 3) {
s[i3][j3] = 1;
} else if (vicini1 > 3) {
s[i3][j3] = 0;
}
}
}
m = s;
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mouseClicked(MouseEvent me) {
int x = me.getX();
int y = me.getY();
int o = x / 6;
int v = y / 6;
if(x > 60 && x < 80 && y > 310 && y < 290){
pausa =! pausa;
repaint();
}
if (m[v][o] == 1) {
m[v][o] = 0;
} else {
m[v][o] = 1;
}
repaint();
}
}
Ovviamente l'ho compilato e per metterlo in una pagina html ho usato questo semplice codice:
<html>
<head>
</head>
<body>
<div align = "center">
<h1>The game of life</h1>
<APPLET code="Giocodellavita.class" width="350" height="350"></APPLET><br>
</div>
<body>
</html>
Ringrazio in anticipo per chi proverà ad aiutarmi
Nel caso non conosciate la logica del gioco c' è spiegato qui :
http://it.wikipedia.org/wiki/Gioco_Life_di_Conway
Versione funzionante:
import java.awt.Graphics; //per la grafica
import java.awt.Color;//per i colori
import java.applet.*;//utilizzare gli apllet con java
import java.awt.event.*;//importazione dell'intero pacchetto per gli eventi del mouse
public class Giocodellavita extends Applet implements Runnable, MouseListener{
int i;
int j;
int k;
boolean pausa = true;//variabile per dire se il gioco è in pausa
int vicini = 0;//conteggio dei vicini
int[][] s;
int[][] m = new int[5][5];
public void run() {
while (true) {
if(!pausa){
repaint();
try {
Thread.sleep(1000);//attesa tra un ciclo e l'altro
} catch (InterruptedException ie) {
}
int[] mat_i = {-1, -1, -1, 0, 1, 1, 1, 0};
int[] mat_j = {-1, 0, 1, 1, 1, 0, -1, -1};
//nuova matrice
s = new int[5][5];
//colonna
for (i = 0; i < m.length; i++) {
//riga
for (j = 0; j < m[0].length; j++) {
vicini = 0;
//conteggio dei vicini
for (k = 0; k < 8; k++) {
//controllo
if ((i + mat_i[k]) >= 0 && ((i + mat_i[k]) < m.length) && (j + mat_j[k]) >= 0 && ((j + mat_j[k]) < m[0].length)) {
if (m[i + mat_i[k]][j + mat_j[k]] == 1) {
vicini++;
}
}
}
if (vicini < 2) {
s[i][j] = 0;
} else if (vicini == 2) {
s[i][j] = m[i][j];
} else if (vicini == 3) {
s[i][j] = 1;
} else if (vicini > 3) {
s[i][j] = 0;
}
}
}
}
m = s;
}
}
public void mouseClicked(MouseEvent me)
{
int x = me.getX();//cordinata x premuta
int y = me.getY();//cordinata y premuta
int i = x / (this.getWidth()/6);
int j = y / (this.getHeight()/ 6);
//cambio dello stato delle celle
if (m[i][j] == 1)
{
m[i][j] = 0;
}else{
m[i][j] = 1;
}
}
public void mousePressed(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
public void init() {
//inserimento dei dati nella matrice, prima azzero tutto
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[i].length; j++)
{
m[i][j] = 0;
}
}
//setto le variabili che voglio
m[1][2] = 1;
m[2][2] = 1;
m[3][2] = 1;
new Thread(this).start();
}
public void paint(Graphics g)
{
{
int w = this.getWidth() / (5 + 1);//righe
int h = this.getHeight() / (5 + 1);//colonne
int x = 0;
int y = 0;
g.setColor(Color.BLACK);
//creazione della tabella
for (int i = 0; i <= w; i++)
{
g.drawLine(x, y, this.getWidth(), y);
y += w;
}
//reset delle variabili x e y a 0
x = 0;
y = 0;
for (int i = 0; i <= h; i++)
{
g.drawLine(x, y, x, this.getWidth());
x += h;
}
this.addMouseListener(this);
//creazione delle celle di vita. Nere con vita mentre bianche con assenza di vita
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[i].length; j++)
{
if (m[i][j] == 1)//caselle vive
{
g.setColor(Color.BLACK);
g.fillRect((j * w) + 1, (i * h) + 1, w - 2, h - 2);
}else{//caselle morte
g.setColor(Color.WHITE);
g.fillRect((j * w) + 1, (i * h) + 1, w - 2, h - 2);
}
}
}
}
}
}
Versione con problema:
import java.awt.*; //per la grafica
import java.awt.image.*;
import java.awt.Color;//per i colori
import java.applet.*;//utilizzare gli apllet con java
import java.awt.event.*;//importazione dell'intero pacchetto per gli eventi del mouse
public class Giocodellavita extends Applet implements Runnable, MouseListener {
int i;
int j;
int k;
Image testt = null;
boolean pausa;
int vicini = 0;//conteggio dei vicini
int[][] m = new int[5][5];
public void run() {
while (true) {
if(!pausa){
repaint();
ProssimoCiclo();
try {
Thread.sleep(800);
} catch (Exception e) {
}
}
}
}
@Override
public void init() {
testt = createImage(300, 400);
//inserimento dei dati nella matrice, prima azzero tutto
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[i].length; j++)
{
m[i][j] = 0;
}
}
//setto le variabili che voglio
m[1][2] = 1;
m[2][2] = 1;
m[3][2] = 1;
new Thread(this).start();
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
//disegna la griglia
g.setColor(Color.BLACK);
//righe verticali
for (int i1 = 0; i1 <= 200; i1 = i1 + 20) {
g.drawLine(i1, 0, i1, 200);
}
//righe orizzontali
for (int j1 = 0; j1 <= 200; j1 = j1 + 20) {
g.drawLine(0, j1, 200, j1);
}
// controlla i valori immessi nella matrice e li disegna nell'applet
for (int i2 = 0; i2 < m.length; i2++) {
for (int j2 = 0; j2 < m.length; j2++) {
if (m[i2][j2] == 1) {
g.fillRect(j2 * 20, i2 * 20, 20, 20);
}
}
}
Graphics buffer = testt.getGraphics();
buffer.setColor(Color.black);
buffer.drawString("Attiva", 70, 300);
g.drawImage(testt, 0, 0, null);
}
public void ProssimoCiclo() {
int[][] s = new int[5][5];
int[] mat_i = {-1, -1, -1, 0, 1, 1, 1, 0};
int[] mat_j = {-1, 0, 1, 1, 1, 0, -1, -1};
//colonna
for (int i3 = 0; i3 < m.length; i3++) {
//riga
for (int j3 = 0; j3 < m[0].length; j3++) {
int vicini1 = 0;
//conta i vicini
for (int k1 = 0; k1 < 8; k1++) {
//controllo
if ((i3 + mat_i[k1]) >= 0 && ((i3 + mat_i[k1]) < m.length) && (j3 + mat_j[k1]) >= 0 && ((j3 + mat_j[k1]) < m[0].length)) {
if (m[i3 + mat_i[k1]][j3 + mat_j[k1]] == 1) {
vicini1++;
}
}
}
if (vicini1 < 2) {
s[i3][j3] = 0;
} else if (vicini1 == 2) {
s[i3][j3] = m[i3][j3];
} else if (vicini1 == 3) {
s[i3][j3] = 1;
} else if (vicini1 > 3) {
s[i3][j3] = 0;
}
}
}
m = s;
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mouseClicked(MouseEvent me) {
int x = me.getX();
int y = me.getY();
int o = x / 6;
int v = y / 6;
if(x > 60 && x < 80 && y > 310 && y < 290){
pausa =! pausa;
repaint();
}
if (m[v][o] == 1) {
m[v][o] = 0;
} else {
m[v][o] = 1;
}
repaint();
}
}
Ovviamente l'ho compilato e per metterlo in una pagina html ho usato questo semplice codice:
<html>
<head>
</head>
<body>
<div align = "center">
<h1>The game of life</h1>
<APPLET code="Giocodellavita.class" width="350" height="350"></APPLET><br>
</div>
<body>
</html>
Ringrazio in anticipo per chi proverà ad aiutarmi