Moving a piece

I would like to move the piece using my mouse.

For example, say there's a pawn sitting on a square on the chess board. If my mouse was to click (press and release) in the square that the pawn is in, it would be selected. Afterwards, I would click (press and release) in an appropriate square. Say, the square in front of it since that is a proper move in Chess. The pawn would move (get erased from the square it was on, then redrawn on the new one) to the final selected square.

Currently, the Pawn is just sitting on a square when execution is finished. If it's any help, I am using Ready To Program Java (my teacher told me to) as well as the c console (c = new Console();).

Here is the source code of what I have done so far :)

import hsa.Console;
import java.awt.*;

public class Chess
{
    static Console c;

    public static void main(String[] args)
    {
        c = new Console(30, 100);    // Rows, Columns (X = 790px && Y = 600px)   

        Board();
        Pawn();
    }

    public static void Board()      // Board: 504px x 504px     Square: 63px x 63px
    {
        int Horizontal = 143;   // Board's origin point (X)
        int Vertical = 48;      // Board's origin point (Y)

        for (Vertical = 48; Vertical < 552; Vertical+=63)       // Moving onto the next "line"
        {
            for (Horizontal = 143; Horizontal < 647; Horizontal+=63)    // Filling the "line" with squares
            {
                c.drawRect(Horizontal, Vertical, 63, 63);   // Drawing the Squares
            }
            Horizontal = 143;       // Resetting the "line"
        }
    }

    public static void Pawn()       // Image and properties of a PAWN piece
    {
        c.setColor(Color.red);          // How the Pawn looks
        c.drawOval(143, 111, 63, 63);   
    }
}

The Console Java class documentation can be found here:

http://stbenedict.wcdsb.ca/hsa/Console.html

As it specifies, it implements the EventListener interface, which has a subinterface, MouseListener. There are many guides online which document how to handle events sent from the MouseListener. Here is one from Oracle's documentation:

https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

链接地址: http://www.djcxy.com/p/84626.html

上一篇: 棋子不能正确渲染

下一篇: 移动一块