beelzibub
2008-05-28 01:12:06 UTC
package testjava;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.event.*;
class ChessBoard extends JFrame implements MouseListener,
MouseMotionListener {
GridLayout Board = new GridLayout(8, 8);
int xMove, yMove, labelw, labelh;
JLabel label;
int Xpos, Ypos;
String[] manrow = {"Pawn0", "Pawn1", "Pawn2", "Pawn3", "Pawn4",
"Pawn5", "Pawn6", "Pawn7"};
String[] manrowx = {"Pawn0", "Pawn1", "Pawn2", "Pawn3", "Pawn4",
"Pawn5", "Pawn6", "Pawn7"};
String[] piecerow = {"Rook", "Knight", "Bishop", "Queen", "King",
"Bishop", "Knight", "Rook"};
String[] piecerowx = {"Rook", "Knight", "Bishop", "Queen", "King",
"Bishop", "Knight", "Rook"};
// update these labels position, which is parent jpanel, at each move
JPanel[][] grids;
JLabel[][] pieces;
Dimension pieceSize = new Dimension(100, 100);
Container con;
Component DraggingComp;
GridLayout board;
public ChessBoard() {
super("ChessBoard");
setDefaultCloseOperation(EXIT_ON_CLOSE);
board = new GridLayout(8, 8);
Board.setHgap(4);
Board.setVgap(4);
Dimension boardSize = new Dimension(800, 800);
con = getContentPane();/*
* i understand what you are saying
* but i c an't quite get it to take
*/
//con.setLayout(new GridChessBoard()); // this is what i want
con.addMouseListener(this);
con.addMouseMotionListener(this);
grids = new JPanel[8][8];
pieces = new JLabel[8][8];
label = new JLabel("0123456789");
Dimension d = label.getPreferredSize();
labelw = d.width;
labelh = d.height;
label.setBounds(Xpos, Ypos, labelw, labelh);
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
//MySquares();
addSquares(i, j);
}
}
setSize(boardSize);
setVisible(true);
} // end constructor
public JFrame addAction(JFrame frame) {
frame.getContentPane().addMouseMotionListener(this);
return frame;
}
public void seeIt(Component c) {
System.out.println("in seeIt");
add(c);
((JPanel) con).revalidate();
}
public void moveComponent(Component c, int newX, int newY, int
width, int height) {
System.out.println("in moveComponent");
c.setBounds(newX, newY, width, height);
seeIt(c);
}
public String getMan(int i) {
return manrow[i];
}
public String getPiece(int i) {
return piecerow[i];
}
public boolean isOdd(int i) {
return i % 2 != 0;
}
public boolean isEven(int i) {
return i % 2 == 0;
}
public JLabel setBlue(JLabel b1) {
b1.setOpaque(true);
b1.setBackground(Color.blue);
return b1;
}
public JLabel setYellow(JLabel b1) {
b1.setOpaque(true);
b1.setBackground(Color.yellow);
return b1;
}
public void addColors() {
for (int i = 0; i < 64; i++) {
JLabel square = new JLabel(); //new BorderLayout());
square.setOpaque(true);
Board.addLayoutComponent(null, square);
int row = (i / 8) % 2;
if (row == 0) {
square.setBackground(i % 2 == 0 ? Color.blue :
Color.yellow);
} else {
square.setBackground(i % 2 == 0 ? Color.yellow :
Color.blue);
}
}
}
public void addBlankRow(int i, int j) {
if (isEven(j)) {
setBackground(i % 2 == 0 ? Color.yellow : Color.blue);
} else {
setBackground(i % 2 == 0 ? Color.blue : Color.yellow);
}
}
public void addManRow(int i, int j) {
String[] m = i == 1 ? manrowx : manrow;
int col = j + 1;
if (isEven(col)) {
setBackground(Color.yellow);
add((new JLabel(m[j])), JLabel.CENTER);
} else {
setBackground(Color.blue);
add((new JLabel(m[j])), JLabel.CENTER);
}
}
public void addPieceRow(int i, int j) {
String[] m = i == 1 ? piecerow : piecerowx;
int col = j;
if (isEven(col) && (col < 8)) {
add(setYellow(new JLabel(m[j])), JLabel.CENTER);
setBackground(Color.yellow);
} else {
add(setBlue(new JLabel(m[j])), JLabel.CENTER);
setBackground(Color.blue);
}
}
public void addPieceRowx(int i, int j) {
String[] m = i == 1 ? piecerowx : piecerow;
int col = j + 1;
if (isEven(col)) {
setBackground(Color.yellow);
add((new JLabel(m[j])), JLabel.CENTER);
} else {
setBackground(Color.blue);
add((new JLabel(m[j])), JLabel.CENTER);
}
}
public void addManRowx(int i, int j) {
String[] m = i == 1 ? manrowx : manrow;
int col = j + 1;
if (isEven(col)) {
setBackground(Color.blue);
add(setBlue(new JLabel(m[j])), JLabel.CENTER);
} else {
setBackground(Color.yellow);
add(setYellow(new JLabel(m[j])), JLabel.CENTER);
}
}
public void mousePressed(MouseEvent me) {
System.out.println("in mouse pressed");
// get orriginal coordinates
if (DraggingComp != null) {
Xpos = me.getX();
Ypos = me.getY();
DraggingComp = getComponentAt(Xpos, Ypos);
}
}
public void mouseDragged(MouseEvent me) {
System.out.println("in mouse dragged");
DraggingComp = con.getComponentAt(Xpos, Ypos);
if (DraggingComp == null) {
return;
} else {
Xpos = getX();
Ypos = getY();
}
moveComponent(DraggingComp, Xpos, Ypos, labelw, labelh);
}
public void mouseReleased(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mouseMoved(MouseEvent me) {
}
public void mouseClicked(MouseEvent me) {
}
public void addSquares(int i, int j) {
JLabel square = new JLabel();
square.setLayout(new BorderLayout());
square.setOpaque(true);
int col = j;
int row = i;
switch (row) {
case 0:
addPieceRow(row, col);
break;
case 1:
addManRow(row, col);
break;
case 2:
addBlankRow(row, col);
break;
case 3:
addBlankRow(row, col);
break;
case 4:
addBlankRow(row, col);
break;
case 5:
addBlankRow(row, col);
break;
case 6:
addManRowx(row, col);
break;
case 7:
addPieceRowx(row, col);
break;
}
square.setBounds(row, col, labelw, labelh);
// add(square);
//return;
}
class GridChessBoard {
JFrame frame;
Container con;
public GridChessBoard() {
frame = new JFrame("Chess Board made of a grid of colored panels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = frame.getContentPane();
con.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel(new BorderLayout());
con.add(square);
int row = (i / 8) % 2;
if (row == 0) {
square.setBackground(i % 2 == 0 ? Color.red : Color.white);
} else {
square.setBackground(i % 2 == 0 ? Color.white : Color.red);
}
frame.setSize(800, 800);
frame.setVisible(true);
}
}
}
public static void main(String[] args) {
// GridChessBoard gcb = new GridChessBoard();
new ChessBoard();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.event.*;
class ChessBoard extends JFrame implements MouseListener,
MouseMotionListener {
GridLayout Board = new GridLayout(8, 8);
int xMove, yMove, labelw, labelh;
JLabel label;
int Xpos, Ypos;
String[] manrow = {"Pawn0", "Pawn1", "Pawn2", "Pawn3", "Pawn4",
"Pawn5", "Pawn6", "Pawn7"};
String[] manrowx = {"Pawn0", "Pawn1", "Pawn2", "Pawn3", "Pawn4",
"Pawn5", "Pawn6", "Pawn7"};
String[] piecerow = {"Rook", "Knight", "Bishop", "Queen", "King",
"Bishop", "Knight", "Rook"};
String[] piecerowx = {"Rook", "Knight", "Bishop", "Queen", "King",
"Bishop", "Knight", "Rook"};
// update these labels position, which is parent jpanel, at each move
JPanel[][] grids;
JLabel[][] pieces;
Dimension pieceSize = new Dimension(100, 100);
Container con;
Component DraggingComp;
GridLayout board;
public ChessBoard() {
super("ChessBoard");
setDefaultCloseOperation(EXIT_ON_CLOSE);
board = new GridLayout(8, 8);
Board.setHgap(4);
Board.setVgap(4);
Dimension boardSize = new Dimension(800, 800);
con = getContentPane();/*
* i understand what you are saying
* but i c an't quite get it to take
*/
//con.setLayout(new GridChessBoard()); // this is what i want
con.addMouseListener(this);
con.addMouseMotionListener(this);
grids = new JPanel[8][8];
pieces = new JLabel[8][8];
label = new JLabel("0123456789");
Dimension d = label.getPreferredSize();
labelw = d.width;
labelh = d.height;
label.setBounds(Xpos, Ypos, labelw, labelh);
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
//MySquares();
addSquares(i, j);
}
}
setSize(boardSize);
setVisible(true);
} // end constructor
public JFrame addAction(JFrame frame) {
frame.getContentPane().addMouseMotionListener(this);
return frame;
}
public void seeIt(Component c) {
System.out.println("in seeIt");
add(c);
((JPanel) con).revalidate();
}
public void moveComponent(Component c, int newX, int newY, int
width, int height) {
System.out.println("in moveComponent");
c.setBounds(newX, newY, width, height);
seeIt(c);
}
public String getMan(int i) {
return manrow[i];
}
public String getPiece(int i) {
return piecerow[i];
}
public boolean isOdd(int i) {
return i % 2 != 0;
}
public boolean isEven(int i) {
return i % 2 == 0;
}
public JLabel setBlue(JLabel b1) {
b1.setOpaque(true);
b1.setBackground(Color.blue);
return b1;
}
public JLabel setYellow(JLabel b1) {
b1.setOpaque(true);
b1.setBackground(Color.yellow);
return b1;
}
public void addColors() {
for (int i = 0; i < 64; i++) {
JLabel square = new JLabel(); //new BorderLayout());
square.setOpaque(true);
Board.addLayoutComponent(null, square);
int row = (i / 8) % 2;
if (row == 0) {
square.setBackground(i % 2 == 0 ? Color.blue :
Color.yellow);
} else {
square.setBackground(i % 2 == 0 ? Color.yellow :
Color.blue);
}
}
}
public void addBlankRow(int i, int j) {
if (isEven(j)) {
setBackground(i % 2 == 0 ? Color.yellow : Color.blue);
} else {
setBackground(i % 2 == 0 ? Color.blue : Color.yellow);
}
}
public void addManRow(int i, int j) {
String[] m = i == 1 ? manrowx : manrow;
int col = j + 1;
if (isEven(col)) {
setBackground(Color.yellow);
add((new JLabel(m[j])), JLabel.CENTER);
} else {
setBackground(Color.blue);
add((new JLabel(m[j])), JLabel.CENTER);
}
}
public void addPieceRow(int i, int j) {
String[] m = i == 1 ? piecerow : piecerowx;
int col = j;
if (isEven(col) && (col < 8)) {
add(setYellow(new JLabel(m[j])), JLabel.CENTER);
setBackground(Color.yellow);
} else {
add(setBlue(new JLabel(m[j])), JLabel.CENTER);
setBackground(Color.blue);
}
}
public void addPieceRowx(int i, int j) {
String[] m = i == 1 ? piecerowx : piecerow;
int col = j + 1;
if (isEven(col)) {
setBackground(Color.yellow);
add((new JLabel(m[j])), JLabel.CENTER);
} else {
setBackground(Color.blue);
add((new JLabel(m[j])), JLabel.CENTER);
}
}
public void addManRowx(int i, int j) {
String[] m = i == 1 ? manrowx : manrow;
int col = j + 1;
if (isEven(col)) {
setBackground(Color.blue);
add(setBlue(new JLabel(m[j])), JLabel.CENTER);
} else {
setBackground(Color.yellow);
add(setYellow(new JLabel(m[j])), JLabel.CENTER);
}
}
public void mousePressed(MouseEvent me) {
System.out.println("in mouse pressed");
// get orriginal coordinates
if (DraggingComp != null) {
Xpos = me.getX();
Ypos = me.getY();
DraggingComp = getComponentAt(Xpos, Ypos);
}
}
public void mouseDragged(MouseEvent me) {
System.out.println("in mouse dragged");
DraggingComp = con.getComponentAt(Xpos, Ypos);
if (DraggingComp == null) {
return;
} else {
Xpos = getX();
Ypos = getY();
}
moveComponent(DraggingComp, Xpos, Ypos, labelw, labelh);
}
public void mouseReleased(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mouseMoved(MouseEvent me) {
}
public void mouseClicked(MouseEvent me) {
}
public void addSquares(int i, int j) {
JLabel square = new JLabel();
square.setLayout(new BorderLayout());
square.setOpaque(true);
int col = j;
int row = i;
switch (row) {
case 0:
addPieceRow(row, col);
break;
case 1:
addManRow(row, col);
break;
case 2:
addBlankRow(row, col);
break;
case 3:
addBlankRow(row, col);
break;
case 4:
addBlankRow(row, col);
break;
case 5:
addBlankRow(row, col);
break;
case 6:
addManRowx(row, col);
break;
case 7:
addPieceRowx(row, col);
break;
}
square.setBounds(row, col, labelw, labelh);
// add(square);
//return;
}
class GridChessBoard {
JFrame frame;
Container con;
public GridChessBoard() {
frame = new JFrame("Chess Board made of a grid of colored panels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = frame.getContentPane();
con.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel(new BorderLayout());
con.add(square);
int row = (i / 8) % 2;
if (row == 0) {
square.setBackground(i % 2 == 0 ? Color.red : Color.white);
} else {
square.setBackground(i % 2 == 0 ? Color.white : Color.red);
}
frame.setSize(800, 800);
frame.setVisible(true);
}
}
}
public static void main(String[] args) {
// GridChessBoard gcb = new GridChessBoard();
new ChessBoard();
}
}