Trying to animate the filling of the board.
This is just for educational purposes and at no means necessary for the game. Yeah I'm pretty productive LOL
This commit is contained in:
@@ -1,42 +0,0 @@
|
|||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
public class Board extends JPanel implements ActionListener {
|
|
||||||
|
|
||||||
private final int DELAY = 15;
|
|
||||||
private final int BOARD_W = 700;
|
|
||||||
private final int BOARD_H = 600;
|
|
||||||
private boolean isInitialized = false;
|
|
||||||
|
|
||||||
private Painter painter;
|
|
||||||
private Timer timer;
|
|
||||||
|
|
||||||
public Board(){
|
|
||||||
initBoard();
|
|
||||||
initGame();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initBoard(){
|
|
||||||
setPreferredSize(new Dimension(BOARD_W,BOARD_H));
|
|
||||||
setBackground(Color.LIGHT_GRAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initGame(){
|
|
||||||
painter = new Painter();
|
|
||||||
timer = new Timer(15, this);
|
|
||||||
timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void paintComponent(Graphics g) {
|
|
||||||
super.paintComponent(g);
|
|
||||||
painter.paintBoard(g, BOARD_W, BOARD_H);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
35
ConnectFour/src/Chip.java
Normal file
35
ConnectFour/src/Chip.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.text.Position;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Chip {
|
||||||
|
private String path = "ConnectFour/res/yellowChip.png";
|
||||||
|
private Point position;
|
||||||
|
private boolean isRed;
|
||||||
|
private Image chip;
|
||||||
|
public Chip(String path, boolean isRed, int tileWidth, int tileHeight) {
|
||||||
|
this.isRed = isRed;
|
||||||
|
position = new Point();
|
||||||
|
try {
|
||||||
|
chip = ImageIO.read(new File(path)).getScaledInstance(tileWidth - 15, tileHeight - 15, Image.SCALE_DEFAULT);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Image getImage() { return chip; }
|
||||||
|
|
||||||
|
public boolean isRed(){
|
||||||
|
return isRed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPosition(int x, int y){
|
||||||
|
position.setLocation(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Point getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,15 +7,20 @@ public class Executor extends JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initUI(){
|
private void initUI(){
|
||||||
Board board = new Board();
|
Panel panel = new Panel();
|
||||||
add(board);
|
add(panel);
|
||||||
pack();
|
pack();
|
||||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
Executor executor = new Executor();
|
Executor executor = new Executor();
|
||||||
executor.setVisible(true);
|
executor.setVisible(true);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
public class Game {
|
|
||||||
}
|
|
||||||
@@ -1,16 +1,22 @@
|
|||||||
|
import javax.imageio.ImageIO;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.ImageObserver;
|
import java.awt.image.ImageObserver;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Painter {
|
public class Painter {
|
||||||
|
|
||||||
private int board_w, board_h, tileWidth, tileHeight;
|
private int board_w, board_h, tileWidth, tileHeight;
|
||||||
|
private Panel panel;
|
||||||
|
|
||||||
public Painter(int width, int height){
|
public Painter(Panel panel, int width, int height){
|
||||||
|
this.panel = panel;
|
||||||
board_w = width;
|
board_w = width;
|
||||||
board_h = height;
|
board_h = height;
|
||||||
tileWidth = width/7;
|
tileWidth = board_w/7;
|
||||||
tileHeight = height/6;
|
tileHeight = board_h/6;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Color holeBlue = new Color(61, 209, 165);
|
private Color holeBlue = new Color(61, 209, 165);
|
||||||
@@ -21,44 +27,29 @@ public class Painter {
|
|||||||
for (int i = 0; i < board_w/tileWidth; i++){
|
for (int i = 0; i < board_w/tileWidth; i++){
|
||||||
for (int j = 0; j < board_h/tileHeight; j++){
|
for (int j = 0; j < board_h/tileHeight; j++){
|
||||||
g2d.setColor(holeBlue);
|
g2d.setColor(holeBlue);
|
||||||
g2d.fillOval(i * tileWidth + 10, j * tileHeight + 5, tileWidth - 15, tileHeight - 20);
|
g2d.fillOval(i * tileWidth + 10, j * tileHeight + 5, tileWidth - 15, tileHeight - 15);
|
||||||
g2d.setColor(Color.BLACK);
|
g2d.setColor(Color.BLACK);
|
||||||
g2d.drawOval(i * tileWidth+ 10 , j * tileHeight+5, tileWidth - 15, tileHeight - 20);
|
g2d.drawOval(i * tileWidth+ 10 , j * tileHeight+5, tileWidth - 15, tileHeight - 15);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paintYellowChip(Graphics g){
|
public void paintRedChip(Graphics g, int x, int y) {
|
||||||
|
String path = "ConnectFour/res/redChip.png";
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
g.drawImage(new redChip().getRedChip(), 0,0, g);
|
Chip redChip = new Chip(path, true, tileWidth, tileHeight);
|
||||||
|
redChip.setPosition(x, y);
|
||||||
|
panel.addToBoard(redChip, x, y);
|
||||||
|
g2d.drawImage(redChip.getImage(), x * tileWidth + 10, y * tileHeight + 5, panel);
|
||||||
}
|
}
|
||||||
|
|
||||||
class redChip{
|
public void paintYellowChip(Graphics g, int x, int y) {
|
||||||
private ImageIcon imageIcon;
|
String path = "ConnectFour/res/yellowChip.png";
|
||||||
private Image redChip;
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
private String path = "../res/redChip.png";
|
Chip yellowChip = new Chip(path, false, tileWidth, tileHeight);
|
||||||
public redChip(){
|
yellowChip.setPosition(x, y);
|
||||||
imageIcon = new ImageIcon(path);
|
panel.addToBoard(yellowChip, x, y);
|
||||||
redChip = imageIcon.getImage().getScaledInstance(tileWidth, tileHeight, 0);
|
g2d.drawImage(yellowChip.getImage(), x , y , panel);
|
||||||
}
|
|
||||||
|
|
||||||
public Image getRedChip() {
|
|
||||||
return redChip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class yellowChip{
|
|
||||||
private ImageIcon imageIcon;
|
|
||||||
private Image redChip;
|
|
||||||
private String path = "../res/yellowChip.png";
|
|
||||||
public yellowChip(){
|
|
||||||
imageIcon = new ImageIcon(path);
|
|
||||||
redChip = imageIcon.getImage().getScaledInstance(tileWidth, tileHeight, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Image getRedChip() {
|
|
||||||
return redChip;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
123
ConnectFour/src/Panel.java
Normal file
123
ConnectFour/src/Panel.java
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class Panel extends JPanel implements ActionListener {
|
||||||
|
|
||||||
|
private final int DELAY = 50;
|
||||||
|
private final int BOARD_W = 700;
|
||||||
|
private final int BOARD_H = 600;
|
||||||
|
private Chip[][] board = new Chip[7][6];
|
||||||
|
private boolean isInitialized = false;
|
||||||
|
private boolean right,left,space;
|
||||||
|
|
||||||
|
private Painter painter;
|
||||||
|
private Timer timer;
|
||||||
|
|
||||||
|
public Panel() {
|
||||||
|
initBoard();
|
||||||
|
initGame();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initBoard() {
|
||||||
|
setPreferredSize(new Dimension(BOARD_W, BOARD_H));
|
||||||
|
setBackground(Color.LIGHT_GRAY);
|
||||||
|
addKeyListener(new MovingKeyListener());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initGame() {
|
||||||
|
painter = new Painter(this, BOARD_W, BOARD_H);
|
||||||
|
timer = new Timer(DELAY, this);
|
||||||
|
//timer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
painter.paintBoard(g);
|
||||||
|
if (!isInitialized) {
|
||||||
|
fillBoard(g);
|
||||||
|
isInitialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillBoard(Graphics g) {
|
||||||
|
class MyExecutor extends SwingWorker<Void, SwingWorkerValues>{
|
||||||
|
Graphics g;
|
||||||
|
public MyExecutor(Graphics g){
|
||||||
|
this.g = g;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground() throws Exception {
|
||||||
|
for (int column = 5; column >= 0; column--){
|
||||||
|
for (int row = 0; row < 7; row++) {
|
||||||
|
Thread.sleep(100);
|
||||||
|
publish(new SwingWorkerValues(row, column));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void process(java.util.List<SwingWorkerValues> chunks) {
|
||||||
|
super.process(chunks);
|
||||||
|
SwingWorkerValues values = chunks.get(chunks.size()-1);
|
||||||
|
System.out.println(values.row + " " + values.column);
|
||||||
|
painter.paintRedChip(g, values.row, values.column);
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void done() {
|
||||||
|
super.done();
|
||||||
|
painter.paintYellowChip(g, 2,2);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MyExecutor executor = new MyExecutor(g);
|
||||||
|
executor.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addToBoard (Chip chip, int x, int y){
|
||||||
|
board[x][y] = chip;
|
||||||
|
}
|
||||||
|
|
||||||
|
class MovingKeyListener implements KeyListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyTyped(KeyEvent e) {
|
||||||
|
int keyPressed = e.getKeyCode();
|
||||||
|
switch (keyPressed) {
|
||||||
|
case KeyEvent.VK_RIGHT:
|
||||||
|
right = true;
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_LEFT:
|
||||||
|
left = true;
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_SPACE:
|
||||||
|
space = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
7
ConnectFour/src/SwingWorkerValues.java
Normal file
7
ConnectFour/src/SwingWorkerValues.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
public class SwingWorkerValues {
|
||||||
|
public int row, column;
|
||||||
|
public SwingWorkerValues(int row, int column){
|
||||||
|
this.row = row;
|
||||||
|
this.column = column;
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
BIN
out/production/ConnectFour/Chip.class
Normal file
BIN
out/production/ConnectFour/Chip.class
Normal file
Binary file not shown.
BIN
out/production/ConnectFour/Executor$1.class
Normal file
BIN
out/production/ConnectFour/Executor$1.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
out/production/ConnectFour/META-INF/ConnectFour.kotlin_module
Normal file
BIN
out/production/ConnectFour/META-INF/ConnectFour.kotlin_module
Normal file
Binary file not shown.
Binary file not shown.
BIN
out/production/ConnectFour/Panel$1MyExecutor.class
Normal file
BIN
out/production/ConnectFour/Panel$1MyExecutor.class
Normal file
Binary file not shown.
BIN
out/production/ConnectFour/Panel$MovingKeyListener.class
Normal file
BIN
out/production/ConnectFour/Panel$MovingKeyListener.class
Normal file
Binary file not shown.
BIN
out/production/ConnectFour/Panel.class
Normal file
BIN
out/production/ConnectFour/Panel.class
Normal file
Binary file not shown.
BIN
out/production/ConnectFour/SwingWorkerValues.class
Normal file
BIN
out/production/ConnectFour/SwingWorkerValues.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user