Hi,
Here i draw chess boardgame ,but i want when i pressed by mouse on any square , blue rectangle appear ..
I did this but when i pressed on any square the black and white coloe
disappear and not only the square which i pressed has blue rect but also all the board ..
please see my code and tell me the correct way :
-------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class MovePawnTest extends JFrame {
private BoardGame board;
public MovePawnTest() {
board = new BoardGame();
getContentPane().add(board, BorderLayout.CENTER);
}
public static void main(String[] args) {
MovePawnTest movePawnTest = new MovePawnTest();
movePawnTest.setDefaultCloseOperation(MovePawnTest.EXIT_ON_CLOSE);
movePawnTest.setSize(new Dimension(600, 300));
movePawnTest.setLocationRelativeTo(null);
movePawnTest.setVisible(true);
movePawnTest.setExtendedState(MAXIMIZED_BOTH);
}
}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
public class BoardGame extends JPanel {
private final int ROW = 8, COL = 8, WIDTH_SQUARE = 70, HEIGHT_SQUARE = 70;
private int x = 70, y = 70, coX = x, coY = y, xStart, yStart, xEnd, yEnd;
private boolean flage = false, affectColor = false;
private MyMouse myMouse;
public BoardGame() {
myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawingBoard(g2d);
coloreingBoard(g2d);
if (affectColor) {
coloreingBoard(g2d);
Stroke stroke = new BasicStroke(5.0f);
g2d.setColor(Color.BLUE);
g2d.setStroke(stroke);
xStart = x;
yStart = y;
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
g2d.drawRect(((xStart * i) + WIDTH_SQUARE),
(yStart*j)+HEIGHT_SQUARE, WIDTH_SQUARE, HEIGHT_SQUARE);
}
}
}
}
private void drawingBoard(Graphics g) {
for (int i = 0; i <= ROW; ++i) {
for (int j = 0; j <= COL; ++j) {
g.drawLine((x * i) + WIDTH_SQUARE, (y * j) + HEIGHT_SQUARE, (x * i) + WIDTH_SQUARE, y * 9);
g.drawLine(x, (y * j) + HEIGHT_SQUARE, x * 9, (y * j) + HEIGHT_SQUARE);
}
}
}
private void coloreingBoard(Graphics g) {
for (int i = 0; i < ROW; ++i) {
flage = (i % 2 == 0) ? true : false;
coX = x;
for (int j = 0; j < COL; ++j) {
if (flage == true) {
g.setColor(Color.WHITE);
g.fillRect(coX, coY, WIDTH_SQUARE, HEIGHT_SQUARE);
flage = false;
} else {
g.setColor(Color.BLACK);
g.fillRect(coX, coY, WIDTH_SQUARE, HEIGHT_SQUARE);
flage = true;
}
coX += WIDTH_SQUARE;
}
coY += HEIGHT_SQUARE;
}
}
private class MyMouse extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
xStart = e.getX();
yStart = e.getY();
affectColor = true;
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
xEnd = e.getX();
yEnd = e.getY();
affectColor = false;
repaint();
}
}
}
Thanks alot in advance