package ratsuk;
import java.util.Random;
import javax.swing.JFrame;
/**
* import javax.swing.JFrame; import javax.swing.SwingUtilities; import
* javax.swing.UIManager;
*/
public class Ratsuk extends JFrame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random rad;
rad = new Random();
int row = rad.nextInt(8);
int column = rad.nextInt(8);
Tablero newtablero = new Tablero();
newtablero.caballo(row, column);
}
}
package ratsuk;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JPanel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
/**
*
* @author Melvin
*/
public class Tablero {
private static final int HEIGHT = 8;
private static final int WIDTH = 8;
private JButton[][] mesa;
private Icon image;
private JPanel panel;
public Tablero() {
mesa = new JButton[HEIGHT][WIDTH];
panel = new JPanel(new GridLayout(HEIGHT, WIDTH));
image = new ImageIcon(getClass().getResource("redKnight.gif"));
crearventana();
crearmesa();
pintarmesa();
}
private void crearventana() {
JFrame ventana = new JFrame("Juego de Ratsuk");
ventana.setVisible(true);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setContentPane(panel);
ventana.setSize(500, 500);
ventana.setVisible(true);
}
private void crearmesa() {
for (int row = 0; row < HEIGHT; row++) {
for (int column = 0; column < WIDTH; column++) {
JButton button = new JButton();
button.setPreferredSize(new Dimension(40, 40));
mesa[row][column] = button;
panel.add(button);
}
}
}
private void pintarmesa() {
Color fondo;
for (int r = 0; r < HEIGHT; r++) {
for (int t = 0; t < WIDTH; t++) {
fondo = getBackgroundColor(r, t);
mesa[r][t].setBackground(fondo);
}
}
}
private Color getBackgroundColor(int r, int t) {
Color fondo;
if (r % 2 == 0 || r == 0) {
if (t % 2 == 0 || t == 0) {
fondo = Color.BLACK;
} else {
fondo = Color.WHITE;
}
} else {
if (t % 2 == 0 || t == 0) {
fondo = Color.WHITE;
} else {
fondo = Color.BLACK;
}
}
return fondo;
}
public void caballo(final int row, final int column) {
final JButton current = mesa[row][column];
current.setIcon(image);
panel.repaint();
acciones(row, column, current);
}
private void acciones(final int row, final int column, final JButton current) {
final int nextRow = row + 2;
final int nextColumn = column - 1;
if (tienebotton(nextRow, nextColumn)) {
JButton next = mesa[nextRow][nextColumn];
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
current.setIcon(null);
caballo(nextRow, nextColumn);
((AbstractButton) e.getSource()).setEnabled(false);
}
});
}
final int nextColumn1 = column + 1;
if (tienebotton(nextRow, nextColumn1)) {
JButton next1 = mesa[nextRow][nextColumn1];
next1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent d) {
current.setIcon(null);
caballo(nextRow, nextColumn1);
((AbstractButton) d.getSource()).setEnabled(false);
}
});
}
final int nextRow1 = row - 2;
if (tienebotton(nextRow1, nextColumn)) {
JButton next2 = mesa[nextRow1][nextColumn];
next2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent c) {
current.setIcon(null);
caballo(nextRow1, nextColumn);
((AbstractButton)c.getSource()).setEnabled(false);
}
});
}
}
private boolean tienebotton(int row, int column) {
return (row >= 0 && row < HEIGHT && column >= 0 && column < WIDTH);
}
}