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

You need to provide logic or better method that on press

  • reads mouse coordinates
  • finds out in which square of the board the coordinates are located
  • either return you X,Y coordinates of top left corner of the square for further processing or directly use these coordinates to change colour of the square

Also would be good idea to monitor mouseDragged() in case user move around while mouse pressed. For that you would need to monitor mouse coordinates in repeated event like every 100ms so use of TimerTask would be in place. Keeping mouse last know coordinates would be essentials in case that user moves out of original square and you will need to replace last square colour to original and change colour of new square to selected. Keep in mind user may stray out of board...

You need to provide logic or better method that on press

  • reads mouse coordinates
  • finds out in which square of the board the coordinates are located
  • either return you X,Y coordinates of top left corner of the square for further processing or directly use these coordinates to change colour of the square

Also would be good idea to monitor mouseDragged() in case user move around while mouse pressed. For that you would need to monitor mouse coordinates in repeated event like every 100ms so use of TimerTask would be in place. Keeping mouse last know coordinates would be essentials in case that user moves out of original square and you will need to replace last square colour to original and change colour of new square to selected. Keep in mind user may stray out of board...

No,
I want only when i pressed on any square a blue rect appear and the white or black color not disappear also
run my 2 classes then you understand what i want ..
The problem is that when i pressed all squares have blue rect and
black , white color disappear

please help me .. illustrate your answer with simple code to understand you
thanks a lot

please any one answer me

please any one answer me

  1. This is not 24/7 "JUST ME!" service
  2. I actually tried your code so I seen what is happening and based on that I provided recommendations
  3. On mouse press/release you calling repaint() method that doesn't seems to "work the magic". Rethink the process and do not forget to apply what I mentioned previously.
  4. Now if you excuse me I will get back to my work and maybe, only maybe, I will look at this later on
  1. This is not 24/7 "JUST ME!" service
  2. I actually tried your code so I seen what is happening and based on that I provided recommendations
  3. On mouse press/release you calling repaint() method that doesn't seems to "work the magic". Rethink the process and do not forget to apply what I mentioned previously.
  4. Now if you excuse me I will get back to my work and maybe, only maybe, I will look at this later on

i do this : " download my code from this :
http://www.4shared.com/file/140858938/8cf47f25/_2__MovePawn.html

I try but i can't .
please illustrate your answer with code

thanks

show us what you tried and we'll work from there. follow what peter_budo have suggested.

Hi to all,
--------------
I could adjust to make blue rectangle appear only in the first two row ...
but the problem is when i press on the place larger than8 col the rect blue appear and when i press on place less the 1 row the rect blue appear although i made conditions
in mouse pressed
-----------------------------------------
The second problem :
when i press by mouse the black and white colors disappear although i made the method that color the board is always true...
please see my code and you will know all things
--------------------------------------------------------------

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, rangeXY = false, boardColor = 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);
            if (rangeXY && boardColor) {
                affectOnSquares(g2d, xStart, yStart);
                coloreingBoard(g);
            }
//            if (boardColor) {
//                coloreingBoard(g);
//            }
        }
    }

    public void affectOnSquares(Graphics2D g2d, int xStart, int yStart) {
        g2d.drawRect(xStart, yStart, 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();
            if (xStart < x || xStart > (8 * WIDTH_SQUARE) &&
                    yStart < y || yStart >= (3 * HEIGHT_SQUARE)) {
                rangeXY = false;
            } else {
                rangeXY = true;
            }
            affectColor = true;
            boardColor = true;
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            xEnd = e.getX();
            yEnd = e.getY();
            affectColor = false;
            repaint();
        }
    }
}

please help me and illustrate your answer by code ..

thanks
alot

Hi to all,
--------------
I could adjust to make blue rectangle appear only in the first two row ...
but the problem is when i press on the place larger than8 col the rect blue appear and when i press on place less the 1 row the rect blue appear although i made conditions
in mouse pressed
-----------------------------------------
The second problem :
when i press by mouse the black and white colors disappear although i made the method that color the board is always true...
please see my code and you will know all things
--------------------------------------------------------------

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, rangeXY = false, boardColor = 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);
            if (rangeXY && boardColor) {
                affectOnSquares(g2d, xStart, yStart);
                coloreingBoard(g);
            }
//            if (boardColor) {
//                coloreingBoard(g);
//            }
        }
    }

    public void affectOnSquares(Graphics2D g2d, int xStart, int yStart) {
        g2d.drawRect(xStart, yStart, 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();
            if (xStart < x || xStart > (8 * WIDTH_SQUARE) &&
                    yStart < y || yStart >= (3 * HEIGHT_SQUARE)) {
                rangeXY = false;
            } else {
                rangeXY = true;
            }
            affectColor = true;
            boardColor = true;
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            xEnd = e.getX();
            yEnd = e.getY();
            affectColor = false;
            repaint();
        }
    }
}

please help me and illustrate your answer by code ..

thanks
alot

I tried and put my try , so please any one help me
thanks

I'm not worried about blue selection box, which is obviously problem of your logic on the line 83-84 and can be quickly solved.
What is bad is that I cannot figure out why the black & white square appear bellow the frame and then disappear of the frame (count on the X and Y coordinates is somehow messed up and they continue increment to infinity, that is why they give impression of not displaying correctly)

I'm not worried about blue selection box, which is obviously problem of your logic on the line 83-84 and can be quickly solved.
What is bad is that I cannot figure out why the black & white square appear bellow the frame and then disappear of the frame (count on the X and Y coordinates is somehow messed up and they continue increment to infinity, that is why they give impression of not displaying correctly)

I do this but not solve the problem ...
------------------------------------------------------

if (boardColor == true || rangeXY == false || rangeXY == true) {
            coloreingBoard(g2d);
        }

In this piece of code i want to say if boardcolor =true
and if you press on any square to get blue rect or not press
leave the black and white color
------------------------------------------------------------------------
The whole code :
------------------------------

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, rangeXY = false, boardColor = 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);
        if (boardColor == true || rangeXY == false || rangeXY == true) {
            coloreingBoard(g2d);
        }
        if (affectColor) {
            Stroke stroke = new BasicStroke(5.0f);
            g2d.setColor(Color.BLUE);
            g2d.setStroke(stroke);
            if (rangeXY) {
                affectOnSquares(g2d, xStart, yStart);
                //            }
            }
        }
    }

    public void affectOnSquares(Graphics2D g2d, int xStart, int yStart) {
//        boardColor = true;
//        if (boardColor) {
//            coloreingBoard(g2d);
//        }
        g2d.drawRect(xStart, yStart, 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();
            if (xStart < x || xStart > (8 * WIDTH_SQUARE) &&
                    yStart < y || yStart >= (3 * HEIGHT_SQUARE)) {
                rangeXY = false;
            } else {
                rangeXY = true;
            }
            affectColor = true;
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            xEnd = e.getX();
            yEnd = e.getY();
            affectColor = false;
            repaint();
        }
    }
}

please if you find the correct way help me ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.