Added chessboard & abstract piece
This commit is contained in:
commit
e3ee468f4e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/out/
|
3
.idea/.gitignore
vendored
Executable file
3
.idea/.gitignore
vendored
Executable file
|
@ -0,0 +1,3 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
6
.idea/misc.xml
Executable file
6
.idea/misc.xml
Executable file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Chess.iml" filepath="$PROJECT_DIR$/Chess.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
11
Chess.iml
Normal file
11
Chess.iml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
61
src/AbstractPiece.java
Normal file
61
src/AbstractPiece.java
Normal file
|
@ -0,0 +1,61 @@
|
|||
import java.awt.*;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
import java.awt.geom.Path2D;
|
||||
|
||||
public abstract class AbstractPiece implements IPiece {
|
||||
|
||||
protected Chessboard chessboard;
|
||||
|
||||
protected int x;
|
||||
protected int y;
|
||||
|
||||
public AbstractPiece(Chessboard c, int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
chessboard = c;
|
||||
c.addPiece(this, x, y);
|
||||
}
|
||||
|
||||
public Path2D getObject(double[] xPoints, double[] yPoints) {
|
||||
Path2D object = new Path2D.Double();
|
||||
double squareSize = chessboard.getSquareSize();
|
||||
double scale = squareSize/120;
|
||||
double xOffset = chessboard.getStartX() + 10*scale + squareSize*x;
|
||||
double yOffset = chessboard.getStartY() + 10*scale + squareSize*y;
|
||||
object.moveTo(xOffset+(xPoints[0]*scale), yOffset+(yPoints[0]*scale));
|
||||
for (int i = 1; i < xPoints.length; i++) {
|
||||
object.lineTo(xOffset+(xPoints[i]*scale), yOffset+(yPoints[i]*scale));
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
public Ellipse2D getEllipse(double x, double y, double w, double h) {
|
||||
double squareSize = chessboard.getSquareSize();
|
||||
double scale = squareSize/120;
|
||||
double xOffset = chessboard.getStartX() + squareSize*this.x + scale*(10+x);
|
||||
double yOffset = chessboard.getStartY() + squareSize*this.y + scale*(10+y);
|
||||
Ellipse2D ellipse = new Ellipse2D.Double(xOffset, yOffset, scale*w, scale*h);
|
||||
return ellipse;
|
||||
}
|
||||
|
||||
public Path2D getStand() {
|
||||
|
||||
double[] xObject2 = new double[]{30, 70, 70, 60, 69, 90, 90, 10, 10, 31, 40, 30, 30};
|
||||
double[] yObject2 = new double[]{64, 64, 69, 69, 80, 87, 100, 100, 87, 80, 69, 69, 64};
|
||||
return getObject(xObject2, yObject2);
|
||||
|
||||
}
|
||||
|
||||
public void paintObjects(Graphics2D g2, Shape[] objects) {
|
||||
double squareSize = chessboard.getSquareSize();
|
||||
double scale = squareSize/120;
|
||||
g2.setStroke(new BasicStroke((float) (4*scale)));
|
||||
for (Shape object : objects) {
|
||||
g2.setColor(Color.WHITE);
|
||||
g2.draw(object);
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.fill(object);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
18
src/BasicDrawing.java
Executable file
18
src/BasicDrawing.java
Executable file
|
@ -0,0 +1,18 @@
|
|||
import javax.swing.JFrame;
|
||||
|
||||
public class BasicDrawing {
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame okno = new JFrame();
|
||||
okno.setTitle("Filip Znachor, A22B0042P");
|
||||
okno.setSize(640, 480);
|
||||
|
||||
okno.add(new DrawingPanel()); //prida komponentu
|
||||
okno.pack(); //udela resize okna dle komponent
|
||||
|
||||
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
okno.setLocationRelativeTo(null); //vycentrovat na obrazovce
|
||||
okno.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
70
src/Chessboard.java
Normal file
70
src/Chessboard.java
Normal file
|
@ -0,0 +1,70 @@
|
|||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
public class Chessboard {
|
||||
|
||||
private int boardWidth;
|
||||
private int squareSize;
|
||||
private double startX;
|
||||
private double startY;
|
||||
|
||||
public final int SQUARE_COUNT = 8;
|
||||
|
||||
private IPiece[][] pieces = new IPiece[SQUARE_COUNT][SQUARE_COUNT];
|
||||
|
||||
private JPanel panel;
|
||||
|
||||
public Chessboard(JPanel p) {
|
||||
panel = p;
|
||||
}
|
||||
|
||||
public double getSquareSize() {
|
||||
return squareSize;
|
||||
}
|
||||
|
||||
public double getStartX() {
|
||||
return startX;
|
||||
}
|
||||
|
||||
public double getStartY() {
|
||||
return startY;
|
||||
}
|
||||
|
||||
public void addPiece(IPiece piece, int x, int y) {
|
||||
pieces[x][y] = piece;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2) {
|
||||
|
||||
double cx = panel.getWidth() / 2;
|
||||
double cy = panel.getHeight() / 2;
|
||||
|
||||
boardWidth = Math.min(panel.getWidth(), panel.getHeight());
|
||||
squareSize = boardWidth/SQUARE_COUNT;
|
||||
|
||||
startX = cx-(boardWidth/2);
|
||||
startY = cy-(boardWidth/2);
|
||||
|
||||
boolean isBlack = true;
|
||||
|
||||
for(int i=0; i<SQUARE_COUNT; i++) {
|
||||
isBlack = !isBlack;
|
||||
for(int j=0; j<SQUARE_COUNT; j++) {
|
||||
if(isBlack) g2.setColor(Color.LIGHT_GRAY);
|
||||
else g2.setColor(Color.WHITE);
|
||||
isBlack = !isBlack;
|
||||
Rectangle2D rect = new Rectangle2D.Double(startX + squareSize * i, startY + squareSize * j, squareSize, squareSize);
|
||||
g2.fill(rect);
|
||||
}
|
||||
}
|
||||
|
||||
for (IPiece[] pieces2: pieces) {
|
||||
for (IPiece piece : pieces2) {
|
||||
if(piece != null) piece.paint(g2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
29
src/DrawingPanel.java
Executable file
29
src/DrawingPanel.java
Executable file
|
@ -0,0 +1,29 @@
|
|||
import org.w3c.dom.css.Rect;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.util.ArrayList;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.StrokeBorder;
|
||||
|
||||
public class DrawingPanel extends JPanel {
|
||||
|
||||
public DrawingPanel() {
|
||||
this.setPreferredSize(new Dimension(800, 600));
|
||||
chessboard = new Chessboard(this);
|
||||
}
|
||||
|
||||
private Chessboard chessboard;
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
RenderingHints rh = new RenderingHints(RenderingHints.KEY_RENDERING,
|
||||
RenderingHints.VALUE_RENDER_QUALITY);
|
||||
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
g2.setRenderingHints(rh);
|
||||
super.paint(g);
|
||||
chessboard.draw(g2);
|
||||
}
|
||||
|
||||
}
|
8
src/IPiece.java
Normal file
8
src/IPiece.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
import java.awt.*;
|
||||
|
||||
public interface IPiece {
|
||||
|
||||
void paint(Graphics2D g2);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in a new issue