mirkolous
10-06-2011, 14:40
salve io sto creando un gioco in java per un esame all'universita'....
Praticamente ho creato buona parte del gioco, ora devo gestire gli eventi con il mouse quando si clicca sulla matrice di carte per far girare le carte....
Praticamente ho gia creato i metodi per far coprire e scoprire le carte, ma ora non riesco a gestire gli eventi con il mouse, per farle girare....
Vi prego datemi una mano......
Questa e il pannello dove creo e mischio la matrice:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* gioco.java
*
* Created on 24-mag-2011, 13.13.18
*/
package cartoonmemory;
import util.BGPanel;
/**
*
* @author Mirko
*/
public class Gioco extends BGPanel {
private carta matrice[][];
private int indx;
private int indy;
private static final int DEFAULT = -1;
private static final int size = 6;
private static final int max = 36;
private Thread t;
/** Creates new form gioco */
public Gioco() {
super("/image/sfondoGioco.jpg");
// creo matrice di carte
initComponents();
this.matrice = new carta [size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
this.matrice[i][j] = new carta(i + j);
this.add(this.matrice[i][j]);
this.matrice[i][j].setLocation(i*300, j*100);
this.matrice[i][j].setLocation(i*100, j*100);
}
}
for (int i = matrice.length - 1; i >= 0; i--) {
for (int j = matrice.length - 1; j >= 0; j--) {
//Pesco un numero random per le righe
int r1 = (int) (Math.random() * i);
//Pesco un numero random per le colonne (se la riga r1 non è la riga i
//il valore massimo di r2 è il numero di colonne della matrice)
int r2 = (int) (Math.random() * ((r1 == i) ? j : matrice[0].length));
//Scambio la cella random con la cella attuale
carta aux = matrice[i][j];
matrice[i][j] = matrice[r1][r2];
matrice[r1][r2] = aux;
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Menu = new javax.swing.JButton();
Menu.setFont(new java.awt.Font("PortagoITC TT", 0, 36)); // NOI18N
Menu.setForeground(new java.awt.Color(255, 0, 0));
Menu.setText("Menù");
Menu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(536, Short.MAX_VALUE)
.addComponent(Menu, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(Menu)
.addContainerGap(299, Short.MAX_VALUE))
);
}// </editor-fold>
private void MenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
protected javax.swing.JButton Menu;
// End of variables declaration
}
**********************************************************
questa e la classe dove ho creato i metodi per le carte, e gli ho caricato le immagini:
package cartoonmemory;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
/**
*
* @author Mirko
*/
public class carta extends JLabel {
private int ID;
private boolean scoperta;
private ImageIcon image;
public static final Dimension LABEL_SIZE = new Dimension(100, 100);
public static final Dimension IMAGE_SIZE = new Dimension(100, 100);
private static final String cover = "/image/cover.jpg";
public carta(int ID) {
this.ID = ID;
this.scoperta = false;
this.setImage();
this.setSize(LABEL_SIZE);
this.setOpaque(false);
}
/**
* Metodo che scopre le carte
*/
public void copri() {
this.scoperta = false;
this.setImage();
}
/**
* Metodo che copre le carte
*/
public void scopri() {
this.scoperta = true;
this.setImage();
}
/**
* Metodo che setta l'immagine della carta
*/
private void setImage() {
if (this.scoperta) {
this.image = new javax.swing.ImageIcon(getClass().getResource("/image/" + ID + ".jpg"));
} else {
this.image = new javax.swing.ImageIcon(getClass().getResource(carta.cover));
}
repaint();
}
/**
* Metodo che ha come return la carta scoperta
*/
public boolean isScoperta() {
return scoperta;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.image.getImage(), (carta.LABEL_SIZE.width - carta.IMAGE_SIZE.width) / 2,
(carta.LABEL_SIZE.height - carta.IMAGE_SIZE.height) / 2, null);
}
}
*************************************************
Se sapete come fare vi prego datemi una mano.....
Praticamente ho creato buona parte del gioco, ora devo gestire gli eventi con il mouse quando si clicca sulla matrice di carte per far girare le carte....
Praticamente ho gia creato i metodi per far coprire e scoprire le carte, ma ora non riesco a gestire gli eventi con il mouse, per farle girare....
Vi prego datemi una mano......
Questa e il pannello dove creo e mischio la matrice:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* gioco.java
*
* Created on 24-mag-2011, 13.13.18
*/
package cartoonmemory;
import util.BGPanel;
/**
*
* @author Mirko
*/
public class Gioco extends BGPanel {
private carta matrice[][];
private int indx;
private int indy;
private static final int DEFAULT = -1;
private static final int size = 6;
private static final int max = 36;
private Thread t;
/** Creates new form gioco */
public Gioco() {
super("/image/sfondoGioco.jpg");
// creo matrice di carte
initComponents();
this.matrice = new carta [size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
this.matrice[i][j] = new carta(i + j);
this.add(this.matrice[i][j]);
this.matrice[i][j].setLocation(i*300, j*100);
this.matrice[i][j].setLocation(i*100, j*100);
}
}
for (int i = matrice.length - 1; i >= 0; i--) {
for (int j = matrice.length - 1; j >= 0; j--) {
//Pesco un numero random per le righe
int r1 = (int) (Math.random() * i);
//Pesco un numero random per le colonne (se la riga r1 non è la riga i
//il valore massimo di r2 è il numero di colonne della matrice)
int r2 = (int) (Math.random() * ((r1 == i) ? j : matrice[0].length));
//Scambio la cella random con la cella attuale
carta aux = matrice[i][j];
matrice[i][j] = matrice[r1][r2];
matrice[r1][r2] = aux;
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Menu = new javax.swing.JButton();
Menu.setFont(new java.awt.Font("PortagoITC TT", 0, 36)); // NOI18N
Menu.setForeground(new java.awt.Color(255, 0, 0));
Menu.setText("Menù");
Menu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(536, Short.MAX_VALUE)
.addComponent(Menu, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(Menu)
.addContainerGap(299, Short.MAX_VALUE))
);
}// </editor-fold>
private void MenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
protected javax.swing.JButton Menu;
// End of variables declaration
}
**********************************************************
questa e la classe dove ho creato i metodi per le carte, e gli ho caricato le immagini:
package cartoonmemory;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
/**
*
* @author Mirko
*/
public class carta extends JLabel {
private int ID;
private boolean scoperta;
private ImageIcon image;
public static final Dimension LABEL_SIZE = new Dimension(100, 100);
public static final Dimension IMAGE_SIZE = new Dimension(100, 100);
private static final String cover = "/image/cover.jpg";
public carta(int ID) {
this.ID = ID;
this.scoperta = false;
this.setImage();
this.setSize(LABEL_SIZE);
this.setOpaque(false);
}
/**
* Metodo che scopre le carte
*/
public void copri() {
this.scoperta = false;
this.setImage();
}
/**
* Metodo che copre le carte
*/
public void scopri() {
this.scoperta = true;
this.setImage();
}
/**
* Metodo che setta l'immagine della carta
*/
private void setImage() {
if (this.scoperta) {
this.image = new javax.swing.ImageIcon(getClass().getResource("/image/" + ID + ".jpg"));
} else {
this.image = new javax.swing.ImageIcon(getClass().getResource(carta.cover));
}
repaint();
}
/**
* Metodo che ha come return la carta scoperta
*/
public boolean isScoperta() {
return scoperta;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.image.getImage(), (carta.LABEL_SIZE.width - carta.IMAGE_SIZE.width) / 2,
(carta.LABEL_SIZE.height - carta.IMAGE_SIZE.height) / 2, null);
}
}
*************************************************
Se sapete come fare vi prego datemi una mano.....