Added javadoc
This commit is contained in:
parent
67b3871815
commit
2627b221eb
|
@ -2,17 +2,48 @@ import java.awt.*;
|
|||
import java.awt.geom.Ellipse2D;
|
||||
import java.awt.geom.Path2D;
|
||||
|
||||
/**
|
||||
* Abstract piece class
|
||||
*/
|
||||
public abstract class AbstractPiece implements IPiece {
|
||||
|
||||
/**
|
||||
* Chessboard instance
|
||||
*/
|
||||
protected Chessboard chessboard;
|
||||
|
||||
/**
|
||||
* Piece's X location
|
||||
*/
|
||||
protected int x;
|
||||
|
||||
/**
|
||||
* Piece's Y location
|
||||
*/
|
||||
protected int y;
|
||||
|
||||
/**
|
||||
* Piece's X override location
|
||||
*/
|
||||
public double overrideX;
|
||||
|
||||
/**
|
||||
* Piece's Y override location
|
||||
*/
|
||||
public double overrideY;
|
||||
|
||||
/**
|
||||
* Piece's color
|
||||
*/
|
||||
protected PieceColor color;
|
||||
|
||||
/**
|
||||
* Create new piece
|
||||
* @param chessboard chessboard
|
||||
* @param x piece's X location
|
||||
* @param y piece's Y location
|
||||
* @param color piece's color
|
||||
*/
|
||||
public AbstractPiece(Chessboard chessboard, int x, int y, PieceColor color) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
@ -21,6 +52,12 @@ public abstract class AbstractPiece implements IPiece {
|
|||
chessboard.addPiece(this, x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Path2D object from points
|
||||
* @param xPoints object's X points
|
||||
* @param yPoints object's Y points
|
||||
* @return Path2D object
|
||||
*/
|
||||
public Path2D getObject(double[] xPoints, double[] yPoints) {
|
||||
Path2D object = new Path2D.Double();
|
||||
object.moveTo(xPoints[0], yPoints[0]);
|
||||
|
@ -31,10 +68,22 @@ public abstract class AbstractPiece implements IPiece {
|
|||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Ellipse2D from specified coordinates
|
||||
* @param x ellipse's X coordinate
|
||||
* @param y ellipse's Y coordinate
|
||||
* @param w ellipse width
|
||||
* @param h ellipse height
|
||||
* @return Ellipse2D object
|
||||
*/
|
||||
public Ellipse2D getEllipse(double x, double y, double w, double h) {
|
||||
return new Ellipse2D.Double(x, y, w, h);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create piece's stand
|
||||
* @return Path2D stand
|
||||
*/
|
||||
public Path2D getStand() {
|
||||
|
||||
double[] xObject2 = new double[]{30, 70, 70, 60, 69, 90, 90, 10, 10, 31, 40, 30, 30};
|
||||
|
@ -43,6 +92,11 @@ public abstract class AbstractPiece implements IPiece {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Paint specified objects
|
||||
* @param g2 Graphics2D
|
||||
* @param objects painted objects
|
||||
*/
|
||||
public void paintObjects(Graphics2D g2, Shape[] objects) {
|
||||
g2.setStroke(new BasicStroke(7));
|
||||
for (Shape object : objects) {
|
||||
|
@ -60,7 +114,7 @@ public abstract class AbstractPiece implements IPiece {
|
|||
chessboard.addPiece(this, x, y);
|
||||
}
|
||||
|
||||
public void setOffset(double x, double y) {
|
||||
public void setOverride(double x, double y) {
|
||||
overrideX = x;
|
||||
overrideY = y;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
import javax.swing.JFrame;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Main program layout
|
||||
*/
|
||||
public class BasicDrawing {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -3,25 +3,59 @@ import java.awt.*;
|
|||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
/**
|
||||
* Chessboard class
|
||||
*/
|
||||
public class Chessboard extends JPanel {
|
||||
|
||||
private double squareSize = 100;
|
||||
private double boardWidth;
|
||||
|
||||
public Graphics2D g;
|
||||
public AffineTransform beforeBoard;
|
||||
|
||||
public double boardScale;
|
||||
public double pieceScale;
|
||||
private double startX;
|
||||
private double startY;
|
||||
|
||||
private IPiece floating;
|
||||
/**
|
||||
* Size of single square
|
||||
*/
|
||||
private final double SQUARE_SIZE = 100;
|
||||
|
||||
/**
|
||||
* Count of squares on the board
|
||||
*/
|
||||
public final int SQUARE_COUNT = 8;
|
||||
|
||||
/**
|
||||
* Width of the chessboard
|
||||
*/
|
||||
private final double BOARD_WIDTH = SQUARE_COUNT*SQUARE_SIZE;;
|
||||
|
||||
/**
|
||||
* Scale of the chessboard
|
||||
*/
|
||||
public double boardScale;
|
||||
|
||||
/**
|
||||
* Scale of single piece
|
||||
*/
|
||||
public double pieceScale;
|
||||
|
||||
/**
|
||||
* Top location of the board
|
||||
*/
|
||||
private double startX;
|
||||
|
||||
/**
|
||||
* Left location of the board
|
||||
*/
|
||||
private double startY;
|
||||
|
||||
/**
|
||||
* Instance of the floating piece
|
||||
*/
|
||||
private IPiece floating;
|
||||
|
||||
/**
|
||||
* Two-dimensional array of pieces
|
||||
*/
|
||||
public IPiece[][] pieces = new IPiece[SQUARE_COUNT][SQUARE_COUNT];
|
||||
|
||||
/**
|
||||
* Constructor of the chessboard
|
||||
*/
|
||||
public Chessboard() {
|
||||
|
||||
ChessboardMouseAdapter ma = new ChessboardMouseAdapter();
|
||||
|
@ -30,16 +64,32 @@ public class Chessboard extends JPanel {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add passed piece to the specified coordinates
|
||||
* @param piece piece
|
||||
* @param x piece's X location
|
||||
* @param y piese's Y location
|
||||
*/
|
||||
public void addPiece(IPiece piece, int x, int y) {
|
||||
pieces[y][x] = piece;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove piece at specified coordinates
|
||||
* @param x piece's X location
|
||||
* @param y piece's Y location
|
||||
* @return removed piece
|
||||
*/
|
||||
public IPiece removePiece(int x, int y) {
|
||||
IPiece piece = pieces[y][x];
|
||||
pieces[y][x] = null;
|
||||
return piece;
|
||||
}
|
||||
|
||||
/**
|
||||
* Paint the chessboard
|
||||
* @param g the <code>Graphics</code> context in which to paint
|
||||
*/
|
||||
public void paint(Graphics g) {
|
||||
|
||||
Graphics2D g2 = getGraphics2D(g);
|
||||
|
@ -47,14 +97,12 @@ public class Chessboard extends JPanel {
|
|||
double cx = this.getWidth() / 2.0;
|
||||
double cy = this.getHeight() / 2.0;
|
||||
|
||||
boardWidth = SQUARE_COUNT*squareSize;
|
||||
boardScale = Math.min(this.getWidth(), this.getHeight())/ BOARD_WIDTH;
|
||||
|
||||
boardScale = Math.min(this.getWidth(), this.getHeight())/boardWidth;
|
||||
startX = cx-(BOARD_WIDTH /2.0*boardScale);
|
||||
startY = cy-(BOARD_WIDTH /2.0*boardScale);
|
||||
|
||||
startX = cx-(boardWidth/2.0*boardScale);
|
||||
startY = cy-(boardWidth/2.0*boardScale);
|
||||
|
||||
beforeBoard = g2.getTransform();
|
||||
AffineTransform beforeBoard = g2.getTransform();
|
||||
g2.translate(startX, startY);
|
||||
g2.scale(boardScale, boardScale);
|
||||
|
||||
|
@ -66,19 +114,19 @@ public class Chessboard extends JPanel {
|
|||
if(isBlack) g2.setColor(Color.LIGHT_GRAY);
|
||||
else g2.setColor(Color.WHITE);
|
||||
isBlack = !isBlack;
|
||||
Rectangle2D rect = new Rectangle2D.Double(squareSize * i, squareSize * j, squareSize, squareSize);
|
||||
Rectangle2D rect = new Rectangle2D.Double(SQUARE_SIZE * i, SQUARE_SIZE * j, SQUARE_SIZE, SQUARE_SIZE);
|
||||
g2.fill(rect);
|
||||
}
|
||||
}
|
||||
|
||||
AffineTransform beforePieces = g2.getTransform();
|
||||
pieceScale = squareSize/140.0;
|
||||
pieceScale = SQUARE_SIZE /140.0;
|
||||
|
||||
for (IPiece[] pieces2: pieces) {
|
||||
for (IPiece piece : pieces2) {
|
||||
if(piece != null && piece != floating) {
|
||||
double xOffset = 20*pieceScale + squareSize*piece.getX();
|
||||
double yOffset = 20*pieceScale + squareSize*piece.getY();
|
||||
double xOffset = 20*pieceScale + SQUARE_SIZE *piece.getX();
|
||||
double yOffset = 20*pieceScale + SQUARE_SIZE *piece.getY();
|
||||
g2.translate(xOffset, yOffset);
|
||||
g2.scale(pieceScale, pieceScale);
|
||||
piece.paint(g2);
|
||||
|
@ -94,11 +142,15 @@ public class Chessboard extends JPanel {
|
|||
floating.paint(g2);
|
||||
}
|
||||
|
||||
this.g = g2;
|
||||
g2.setTransform(beforeBoard);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup Graphics2D with anti-aliasing
|
||||
* @param g Graphics instance
|
||||
* @return Graphics2D instance
|
||||
*/
|
||||
public Graphics2D getGraphics2D(Graphics g) {
|
||||
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
|
@ -110,19 +162,35 @@ public class Chessboard extends JPanel {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get coordinates of piece from mouse X and Y location
|
||||
* @param x mouse X location
|
||||
* @param y mouse Y location
|
||||
* @return piece position
|
||||
*/
|
||||
public PiecePosition getPieceCoordinates(int x, int y) {
|
||||
int pieceX = (int) ((x-startX)/(squareSize*boardScale));
|
||||
int pieceY = (int) ((y-startY)/(squareSize*boardScale));
|
||||
int pieceX = (int) ((x-startX)/(SQUARE_SIZE*boardScale));
|
||||
int pieceY = (int) ((y-startY)/(SQUARE_SIZE*boardScale));
|
||||
return new PiecePosition(
|
||||
Math.min(Math.max(pieceX, 0), SQUARE_COUNT-1),
|
||||
Math.min(Math.max(pieceY, 0), SQUARE_COUNT-1)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get piece on specified piece position
|
||||
* @param pos piece position
|
||||
* @return piece
|
||||
*/
|
||||
public IPiece getPiece(PiecePosition pos) {
|
||||
return pieces[pos.y][pos.x];
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab piece at specified position and mark it as floating
|
||||
* @param pos piece position
|
||||
* @return piece
|
||||
*/
|
||||
public IPiece grabPiece(PiecePosition pos) {
|
||||
IPiece piece = getPiece(pos);
|
||||
floating = piece;
|
||||
|
@ -130,14 +198,22 @@ public class Chessboard extends JPanel {
|
|||
return piece;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current floating piece
|
||||
* @return floating piece
|
||||
*/
|
||||
public IPiece getFloatingPiece() {
|
||||
return floating;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return piece at specified position to the board and unmark it from floating
|
||||
* @param pos piece position
|
||||
*/
|
||||
public void returnPiece(PiecePosition pos) {
|
||||
if(floating != null) {
|
||||
floating.setPosition(pos);
|
||||
floating.setOffset(0, 0);
|
||||
floating.setOverride(0, 0);
|
||||
}
|
||||
floating = null;
|
||||
this.repaint();
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
/**
|
||||
* Chessboard mouse adapter class
|
||||
*/
|
||||
public class ChessboardMouseAdapter extends MouseAdapter {
|
||||
|
||||
|
||||
public void mousePressed(MouseEvent me) {
|
||||
Chessboard c = (Chessboard) me.getSource();
|
||||
PiecePosition pos = c.getPieceCoordinates(me.getX(), me.getY());
|
||||
IPiece piece = c.grabPiece(pos);
|
||||
if (piece != null) {
|
||||
double totalScale = c.pieceScale * c.boardScale;
|
||||
piece.setOffset(me.getX() - 50 * totalScale, me.getY() - 50 * totalScale);
|
||||
piece.setOverride(me.getX() - 50 * totalScale, me.getY() - 50 * totalScale);
|
||||
c.repaint();
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +23,7 @@ public class ChessboardMouseAdapter extends MouseAdapter {
|
|||
IPiece piece = c.getFloatingPiece();
|
||||
if (piece != null) {
|
||||
double totalScale = c.pieceScale * c.boardScale;
|
||||
piece.setOffset(me.getX() - 50 * totalScale, me.getY() - 50 * totalScale);
|
||||
piece.setOverride(me.getX() - 50 * totalScale, me.getY() - 50 * totalScale);
|
||||
c.getRootPane().repaint();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,51 @@
|
|||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Piece's interface
|
||||
*/
|
||||
public interface IPiece {
|
||||
|
||||
/**
|
||||
* Paint piece
|
||||
* @param g2 Graphics2D
|
||||
*/
|
||||
void paint(Graphics2D g2);
|
||||
|
||||
/**
|
||||
* Get piece's X location
|
||||
* @return piece's X location
|
||||
*/
|
||||
int getX();
|
||||
|
||||
/**
|
||||
* Get piece's Y location
|
||||
* @return piece's Y location
|
||||
*/
|
||||
int getY();
|
||||
|
||||
void setOffset(double x, double y);
|
||||
/**
|
||||
* Set override location if piece is floating
|
||||
* @param x floating piece's X location
|
||||
* @param y floating piece's Y location
|
||||
*/
|
||||
void setOverride(double x, double y);
|
||||
|
||||
/**
|
||||
* Get piece's override X location
|
||||
* @return override X location
|
||||
*/
|
||||
double getOverrideX();
|
||||
|
||||
/**
|
||||
* Get piece's override Y location
|
||||
* @return override Y location
|
||||
*/
|
||||
double getOverrideY();
|
||||
|
||||
/**
|
||||
* Set piece's new position
|
||||
* @param pos piece position
|
||||
*/
|
||||
void setPosition(PiecePosition pos);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue