Está en la página 1de 7

package Form;

import
import
import
import
import
import
import
import

Clases.Juego;
Clases.Jugada;
Clases.Posicion;
java.awt.Graphics;
java.util.logging.Level;
java.util.logging.Logger;
javax.swing.ImageIcon;
javax.swing.JOptionPane;

/**
*
* @author
*/
public class FormJuego extends javax.swing.JFrame {
private
private
private
private
private
private
private
private
private
private
private

int controlClick;
double espacioX;
double espacioY;
double espacioSobraX;
double espacioSobraY;
double tamanoImagenX;
double tamanoImagenY;
int nivel;
int nivelc;
Juego juego;
Jugada jugada;

/**
* Creates new form FormJuego
*/
public FormJuego() {
initComponents();
setSize(1100, 800);
nivel = 2;
espacioX = (getWidth() * 0.05) / 2;
espacioY = (getHeight() * 0.2) / 2;
espacioSobraX = getWidth() - espacioX * 2;
espacioSobraY = getHeight() - espacioY * 2;
tamanoImagenX = espacioSobraX / nivel;
tamanoImagenY = espacioSobraY / nivel;
juego = new Juego(nivel);
controlClick=0;
jugada=new Jugada();
paint(getGraphics());
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.clearRect(0,0,getWidth(),getHeight());
int posX = (int) espacioX;
int posY = (int) espacioY;
int tamX = (int) tamanoImagenX;
int tamY = (int) tamanoImagenY;
int idImagen;
for (int i = 0; i < nivel; i++) {
for (int j = 0; j < nivel; j++) {
idImagen = juego.devolverValor(i, j);

if(!juego.abiert(new Posicion(i,j))){
g.drawImage(new ImageIcon("src/Imagenes/im.jpg").getImage(),
posX, posY, tamX, tamY, this);
}else{
g.drawImage(new ImageIcon("src/Imagenes/im" + idImagen + ".j
pg").getImage(), posX, posY, tamX, tamY, this);
}
posX += tamanoImagenX;
}
posY += tamanoImagenY;
posX = (int) espacioX;
}
juego.Imprimir();
}
/**
* 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() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
}
});
jButton1.setText("Nivel 4x4");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Nivel 6x6");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("nivel 8x8");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP
ane());

getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createS
equentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addComponent(jButton3)
.addContainerGap(249, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addGap(0, 312, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void formMouseClicked(java.awt.event.MouseEvent evt) {
int x=evt.getX();
int y=evt.getY();
if(x>espacioX && x<tamanoImagenX*nivel+espacioX && y>espacioY && y<taman
oImagenY*nivel+espacioY){
controlClick++;
Posicion aux=recuperarPos(x,y);
dibujarImagen(aux,getGraphics());
if(controlClick==1){
jugada.setPrimeraJ(aux);
}else{
jugada.setSegundaJ(aux);
if(juego.mismoValor(jugada.getPrimeraJ(),jugada.getSegundaJ())){
juego.modificarValor(jugada.getPrimeraJ(),jugada.getSegundaJ
());
}
controlClick=0;
try {
Thread.sleep(700);
} catch (InterruptedException ex) {
Logger.getLogger(FormJuego.class.getName()).log(Level.SEVERE
, null, ex);
}
paint(getGraphics());
if(juego.gano()){
siguientenivel();
}

}
//JOptionPane.showMessageDialog(this,"X: "+aux.getX()+" Y: "+aux.get
Y());
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cambiarNivel(4);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cambiarNivel(6);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cambiarNivel(8);
}
public Posicion recuperarPos(int x,int y){
Posicion p=new Posicion(0,0);
int contX=0;
int contY=0;
int posX=(int) espacioX;
int posY=(int) espacioY;
while(!(x>posX && x<(posX+tamanoImagenX))){
contX++;
posX+=tamanoImagenX;
}
while(!(y>posY && y<(posY+tamanoImagenY))){
contY++;
posY+=tamanoImagenY;
}
p.setX(contX);
p.setY(contY);
return p;
}
private void dibujarImagen(Posicion aux, Graphics g) {
System.out.println(aux.getX()+" "+aux.getY());
int posX = (int) (espacioX+tamanoImagenX*aux.getX());
int posY = (int) (espacioY+tamanoImagenY*aux.getY());
int tamX = (int) tamanoImagenX;
int tamY = (int) tamanoImagenY;
int dato = juego.devolverValor(aux.getY(),aux.getX());
System.out.println("DATO: "+dato);
g.drawImage(new ImageIcon("src/Imagenes/im" + dato + ".jpg").getImage()
,posX,posY,tamX,tamY,this);
}
public void cambiarNivel(int ni) {
{
nivel = ni;
juego = new Juego(nivel);

espacioX = (getWidth() * 0.2) / 2;


espacioY = (getHeight() * 0.2) / 2;
tamanoImagenX = (getWidth() - espacioX * 2) / nivel;
tamanoImagenY = (getHeight() - espacioY * 2) / nivel;
// juego.modificarValor(new Posicion(0,0),new Posicion(1,1));
juego.Imprimir();
paint(getGraphics());
}
}
public void siguientenivel() {
boolean sw = true;
if (juego.gano() == sw) {
nivel = nivel+2;
espacioX = (getWidth() * 0.05) / 2;
espacioY = (getHeight() * 0.2) / 2;
espacioSobraX = getWidth() - espacioX * 2;
espacioSobraY = getHeight() - espacioY * 2;
tamanoImagenX = espacioSobraX / nivel;
tamanoImagenY = espacioSobraY / nivel;
juego = new Juego(nivel);
controlClick = 0;
jugada = new Jugada();
paint(getGraphics());
}
}
public void matrizrectangular(int nivF, int nivC){
//cree este proceso para hacer que la matriz se dibuje de forma rectangu
lar
initComponents();
setSize(1100, 800);
nivel = nivF;
nivelc=nivC;
espacioX = (getWidth() * 0.05) / 2;
espacioY = (getHeight() * 0.2) / 2;
espacioSobraX = getWidth() - espacioX * 2;
espacioSobraY = getHeight() - espacioY * 2;
tamanoImagenX = espacioSobraX / nivel;
tamanoImagenY = espacioSobraY / nivelc;
juego = new Juego(nivel);
controlClick=0;
jugada=new Jugada();
paint2(getGraphics());
}
/* copie este proceso cambiando de nombre y pense que modificando el nivel d
e columnas funcionaria*/
public void paint2(Graphics g) {
super.paint(g);
g.clearRect(0,0,getWidth(),getHeight());
int posX = (int) espacioX;
int posY = (int) espacioY;
int tamX = (int) tamanoImagenX;
int tamY = (int) tamanoImagenY;
int idImagen;
for (int i = 0; i < nivel; i++) {
for (int j = 0; j < nivelc; j++){ //modificando aqui
idImagen = juego.devolverValor(i, j);
if(!juego.abiert(new Posicion(i,j))){

g.drawImage(new ImageIcon("src/Imagenes/im.jpg").getImage(),
posX, posY, tamX, tamY, this);
}else{
g.drawImage(new ImageIcon("src/Imagenes/im" + idImagen + ".j
pg").getImage(), posX, posY, tamX, tamY, this);
}
posX += tamanoImagenX;
}
posY += tamanoImagenY;
posX = (int) espacioX;
}
juego.Imprimir();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting cod
e (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the d
efault look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lo
okandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIMana
ger.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FormJuego.class.getName()).log(ja
va.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FormJuego.class.getName()).log(ja
va.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FormJuego.class.getName()).log(ja
va.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FormJuego.class.getName()).log(ja
va.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FormJuego().setVisible(true);
}
});
}
// Variables declaration - do not modify

private javax.swing.JButton jButton1;


private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
// End of variables declaration

También podría gustarte