Vista
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Vista{
private JFrame ventana;
private JButton b1, b3;
private JButton b2 [][] = new JButton [8][8];
private JPanel p1 = new JPanel();
private JPanel der,izq;
private JPanel p2, p3,arr,aba;
private JLabel l2;
private JMenuBar barra;
JMenu archivo, ayuda;
JMenuItem jugar,salir,ver;
Image img;
public Vista(){
ventana = new JFrame ();
ventana.setTitle("DAMAS");
menu();
ventanai();
ventana.setResizable(false);
ventana.setSize(600,600);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setVisible(true);
}
public void menu(){
barra = new JMenuBar();
archivo = new JMenu("Juego");
ayuda = new JMenu("Ayuda");
jugar = new JMenuItem("Jugar");
salir = new JMenuItem("Salir");
ver = new JMenuItem("Ver");
archivo.add(jugar);
archivo.add(salir);
ayuda.add(ver);
barra.add(archivo);
barra.add(ayuda);
ventana.setJMenuBar(barra);
}
public void ventanai (){
p1 = new JPanel();
l2 = new JLabel();
ImageIcon ima = new ImageIcon("damas.jpg");
p1.setLayout(null);
p1.setBackground(Color.WHITE);
b1 = new JButton ("JUGAR");
b1.setBackground(Color.WHITE);
b1.setBounds(250, 240, 100, 50);
p1.add(l2);
l2.setIcon(ima);
l2.setSize(600,600);
l2.setVisible(true);
p1.add(b1);
ventana.add(p1);
ventana.pack();
p1.setVisible(true);
}
public void agregarActionListener(ActionListener listener)
{
b1.addActionListener(listener);
b1.setActionCommand("JUGAR");
salir.addActionListener(listener);
salir.setActionCommand("SALIR");
}
public void tablero(){
p2 = new JPanel();
p2.setBackground(Color.WHITE);
p2.setLayout ( new GridLayout(8,8));
for( int i = 0; i < 3; i++){
for( int j = 0; j < 8; j++){
if((i + j) % 2 != 0){
b2[i][j] = new JButton("", new ImageIcon("rojo.png"));
b2[i][j].setBackground(Color.BLACK);
}
else {
b2[i][j] = new JButton("");
b2[i][j].setBackground(Color.WHITE);
}
}
}
for( int i = 3; i <= 4; i++){
for( int j = 0; j < 8; j++){
if((i + j) % 2 != 0){
b2[i][j] = new JButton("");
b2[i][j].setBackground(Color.BLACK);
}
else {
b2[i][j] = new JButton("");
b2[i][j].setBackground(Color.WHITE);
}
}
}
for( int i = 5; i < 8; i++){
for( int j = 0; j < 8; j++){
if((i + j) % 2 != 0){
b2[i][j] = new JButton("", new ImageIcon("azul.png"));
b2[i][j].setBackground(Color.BLACK);
}
else {
b2[i][j] = new JButton("");
b2[i][j].setBackground(Color.WHITE);
}
}
}
for( int i = 0; i < 8; i++){
for( int j = 0; j < 8; j++){
p2.add(b2[i][j]);
}
}
ventana.add(p2);
}
}
Controlador
import java.awt.event.*;
public class Controlador implements ActionListener{
Vista vista;
Modelo modelo;
public Controlador(Vista vista, Modelo modelo){
this.vista = vista;
this.modelo = modelo;
vista.agregarActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
vista = new Vista();
String evento = e.getActionCommand();
if(evento.equals("JUGAR"))
{
vista.tablero();
}
else if(evento.equals("SALIR")){
System.exit(0);
}
}
}
Modelo
public class Modelo {
public Modelo(){
}
public void iniciar(){
}
public void salir(){
}
public void terminar(){
}
}
Main
public class Damas
{
public static void main (String[] args)
{
Vista vista1= new Vista();
Modelo modelo1 = new Modelo();
Controlador contr1 = new Controlador(vista1, modelo1);
}
}
Ok. Faltó todavía implementar tu modelo.
ResponderEliminarCalificación: 4.5/5