Added Eclipse project and basic class structure
+ Piece icons + TextureLoader + BoardPanel as a UI for the chess board + Chess as a main class
11
.classpath
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="lib" path="res"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
BIN
res/bishop_black.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
res/bishop_white.png
Normal file
After Width: | Height: | Size: 1.9 KiB |
BIN
res/king_black.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
res/king_white.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
res/knight_black.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
res/knight_white.png
Normal file
After Width: | Height: | Size: 1.8 KiB |
BIN
res/pawn_black.png
Normal file
After Width: | Height: | Size: 797 B |
BIN
res/pawn_white.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
res/queen_black.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
res/queen_white.png
Normal file
After Width: | Height: | Size: 2.6 KiB |
BIN
res/rook_black.png
Normal file
After Width: | Height: | Size: 725 B |
BIN
res/rook_white.png
Normal file
After Width: | Height: | Size: 933 B |
67
src/dev/kske/chess/BoardPanel.java
Normal file
@ -0,0 +1,67 @@
|
||||
package dev.kske.chess;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Container;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
/**
|
||||
* Project: <strong>Chess</strong><br>
|
||||
* File: <strong>BoardPanel.java</strong><br>
|
||||
* Created: <strong>01.07.2019</strong><br>
|
||||
* Author: <strong>Kai S. K. Engelbart</strong><br>
|
||||
* <br>
|
||||
* A square panel for rendering the chess board. To work correctly,
|
||||
* this must be added to a parent component that allows the child to decide the
|
||||
* size.
|
||||
*/
|
||||
public class BoardPanel extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 6771148331334310216L;
|
||||
|
||||
private int tileSize;
|
||||
|
||||
public BoardPanel() {
|
||||
// Add a component listener for adjusting the tile size on resizing
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
tileSize = getWidth() / 8;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
for (int i = 0; i < 8; i++)
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if (j > 0) g.setColor(g.getColor().equals(Color.white) ? Color.black : Color.white);
|
||||
g.fillRect(tileSize * i, tileSize * j, tileSize, tileSize);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getMinimumSize() { return getPreferredSize(); }
|
||||
|
||||
@Override
|
||||
public Dimension getMaximumSize() { return getPreferredSize(); }
|
||||
|
||||
@Override
|
||||
public Dimension getPreferredSize() {
|
||||
Dimension d = super.getPreferredSize();
|
||||
Container c = getParent();
|
||||
if (c != null) d = c.getSize();
|
||||
else return new Dimension(480, 480);
|
||||
int w = (int) d.getWidth();
|
||||
int h = (int) d.getHeight();
|
||||
int s = Math.max(w, h);
|
||||
return new Dimension(s, s);
|
||||
}
|
||||
}
|
56
src/dev/kske/chess/Chess.java
Normal file
@ -0,0 +1,56 @@
|
||||
package dev.kske.chess;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.EventQueue;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
|
||||
/**
|
||||
* Project: <strong>Chess</strong><br>
|
||||
* File: <strong>Chess.java</strong><br>
|
||||
* Created: <strong>01.07.2019</strong><br>
|
||||
* Author: <strong>Kai S. K. Engelbart</strong>
|
||||
*/
|
||||
public class Chess {
|
||||
|
||||
private JFrame mframe;
|
||||
|
||||
/**
|
||||
* Launch the application.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
Chess window = new Chess();
|
||||
window.mframe.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the application.
|
||||
*/
|
||||
public Chess() {
|
||||
initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the contents of the frame.
|
||||
*/
|
||||
private void initialize() {
|
||||
mframe = new JFrame();
|
||||
mframe.setBounds(100, 100, 740, 740);
|
||||
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
BoardPanel boardPanel = new BoardPanel();
|
||||
boardPanel.setLayout(null);
|
||||
mframe.getContentPane().add(boardPanel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
}
|
38
src/dev/kske/chess/TextureLoader.java
Normal file
@ -0,0 +1,38 @@
|
||||
package dev.kske.chess;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
/**
|
||||
* Project: <strong>Chess</strong><br>
|
||||
* File: <strong>TextureLoader.java</strong><br>
|
||||
* Created: <strong>01.07.2019</strong><br>
|
||||
* Author: <strong>Kai S. K. Engelbart</strong>
|
||||
*/
|
||||
public class TextureLoader {
|
||||
|
||||
private TextureLoader() {}
|
||||
|
||||
/**
|
||||
* Loads an image from the resource folder and scales it to a square.
|
||||
*
|
||||
* @param name The name of the file without the PNG extension in the resource
|
||||
* folder
|
||||
* @param scale The side length of the square to which the image will be scaled
|
||||
* @return The scaled image
|
||||
*/
|
||||
public static Image loadScaledImage(String name, int scale) {
|
||||
BufferedImage in = null;
|
||||
try {
|
||||
in = ImageIO.read(new File("res" + File.separator + name + ".png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Image scaled = in.getScaledInstance(scale, scale, Image.SCALE_SMOOTH);
|
||||
return scaled;
|
||||
}
|
||||
}
|