martes, 10 de febrero de 2009

Clase ContenedorTablero

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
*
* @author cvleonardo
*/
public class ContenedorTablero extends Canvas {

private JPanel panelTablero;
protected BufferedImage sprite;
private BufferStrategy strategy;
private Vector cub;
public int posX = 0, valX, possX;
public int posY = 0, valY;
public int valC;
private Cubo cubo;
public String col = "";
public String colorCubo = "";

public ContenedorTablero(JPanel panelTablero) {
this.panelTablero = (JPanel) panelTablero;
setBounds(0, 0, 192, 414);
panelTablero.setPreferredSize(new Dimension(192, 414));
panelTablero.add(this);
requestFocusInWindow();
createBufferStrategy(2);
strategy = getBufferStrategy();
cub = new Vector();

}

public void paintTablero() {
Graphics g = strategy.getDrawGraphics(); // Corresponde al double buffer
try {
java.net.URL url = getClass().getClassLoader().getResource("res/fondo.png");
sprite = ImageIO.read(url);
g.drawImage(sprite, 0, 0, this);

} catch (IOException ex) {
ex.printStackTrace();

}

colorCubo = colorCubo();
cubo = new Cubo(32, 32, colorCubo);
cub.add(cubo);


for (Cubo c : cub) {
c.paintCubo(g);
}
strategy.show();

}

public String colorCubo() {
valC = valorColor();
if (valC == 0) {
col = "blue.png";
} else if (valC == 1) {
col = "green.png";
} else if (valC == 2) {
col = "pink.png";
} else if (valC == 3) {
col = "red.png";
} else if (valC == 4) {
col = "yellow.png";
} else {
colorCubo();
}
return col;

}

public int valorColor() {
int auxC;
Random rc = new Random();
auxC = rc.nextInt(5);
return auxC;

}
}

No hay comentarios:

Publicar un comentario