diff --git a/ConnectFour/src/Board.java b/ConnectFour/src/Board.java deleted file mode 100644 index 26b643e..0000000 --- a/ConnectFour/src/Board.java +++ /dev/null @@ -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); - } -} diff --git a/ConnectFour/src/Chip.java b/ConnectFour/src/Chip.java new file mode 100644 index 0000000..6b741b8 --- /dev/null +++ b/ConnectFour/src/Chip.java @@ -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; + } +} diff --git a/ConnectFour/src/Executor.java b/ConnectFour/src/Executor.java index f73a6d7..dc7510f 100644 --- a/ConnectFour/src/Executor.java +++ b/ConnectFour/src/Executor.java @@ -7,15 +7,20 @@ public class Executor extends JFrame { } private void initUI(){ - Board board = new Board(); - add(board); + Panel panel = new Panel(); + add(panel); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); } public static void main(String[] args) { - Executor executor = new Executor(); - executor.setVisible(true); + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + Executor executor = new Executor(); + executor.setVisible(true); + } + }); } } diff --git a/ConnectFour/src/Game.java b/ConnectFour/src/Game.java deleted file mode 100644 index f7e8986..0000000 --- a/ConnectFour/src/Game.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Game { -} diff --git a/ConnectFour/src/Painter.java b/ConnectFour/src/Painter.java index 03e9250..42055fc 100644 --- a/ConnectFour/src/Painter.java +++ b/ConnectFour/src/Painter.java @@ -1,16 +1,22 @@ +import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; +import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; +import java.io.File; +import java.io.IOException; public class Painter { 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_h = height; - tileWidth = width/7; - tileHeight = height/6; + tileWidth = board_w/7; + tileHeight = board_h/6; } 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 j = 0; j < board_h/tileHeight; j++){ 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.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; - 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{ - private ImageIcon imageIcon; - private Image redChip; - private String path = "../res/redChip.png"; - public redChip(){ - imageIcon = new ImageIcon(path); - redChip = imageIcon.getImage().getScaledInstance(tileWidth, tileHeight, 0); - } - - 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; - } + public void paintYellowChip(Graphics g, int x, int y) { + String path = "ConnectFour/res/yellowChip.png"; + Graphics2D g2d = (Graphics2D) g; + Chip yellowChip = new Chip(path, false, tileWidth, tileHeight); + yellowChip.setPosition(x, y); + panel.addToBoard(yellowChip, x, y); + g2d.drawImage(yellowChip.getImage(), x , y , panel); } } diff --git a/ConnectFour/src/Panel.java b/ConnectFour/src/Panel.java new file mode 100644 index 0000000..76ae04c --- /dev/null +++ b/ConnectFour/src/Panel.java @@ -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{ + 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 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) { + + } + }; +} diff --git a/ConnectFour/src/SwingWorkerValues.java b/ConnectFour/src/SwingWorkerValues.java new file mode 100644 index 0000000..32c28f1 --- /dev/null +++ b/ConnectFour/src/SwingWorkerValues.java @@ -0,0 +1,7 @@ +public class SwingWorkerValues { + public int row, column; + public SwingWorkerValues(int row, int column){ + this.row = row; + this.column = column; + } +} diff --git a/out/production/ConnectFour/Board.class b/out/production/ConnectFour/Board.class deleted file mode 100644 index d567cfd..0000000 Binary files a/out/production/ConnectFour/Board.class and /dev/null differ diff --git a/out/production/ConnectFour/Chip.class b/out/production/ConnectFour/Chip.class new file mode 100644 index 0000000..3f5b87b Binary files /dev/null and b/out/production/ConnectFour/Chip.class differ diff --git a/out/production/ConnectFour/Executor$1.class b/out/production/ConnectFour/Executor$1.class new file mode 100644 index 0000000..721e92c Binary files /dev/null and b/out/production/ConnectFour/Executor$1.class differ diff --git a/out/production/ConnectFour/Executor.class b/out/production/ConnectFour/Executor.class index 72af857..0558b6a 100644 Binary files a/out/production/ConnectFour/Executor.class and b/out/production/ConnectFour/Executor.class differ diff --git a/out/production/ConnectFour/Game.class b/out/production/ConnectFour/Game.class deleted file mode 100644 index 69e7f7b..0000000 Binary files a/out/production/ConnectFour/Game.class and /dev/null differ diff --git a/out/production/ConnectFour/META-INF/ConnectFour.kotlin_module b/out/production/ConnectFour/META-INF/ConnectFour.kotlin_module new file mode 100644 index 0000000..a49347a Binary files /dev/null and b/out/production/ConnectFour/META-INF/ConnectFour.kotlin_module differ diff --git a/out/production/ConnectFour/Painter.class b/out/production/ConnectFour/Painter.class index f16f017..749d68b 100644 Binary files a/out/production/ConnectFour/Painter.class and b/out/production/ConnectFour/Painter.class differ diff --git a/out/production/ConnectFour/Panel$1MyExecutor.class b/out/production/ConnectFour/Panel$1MyExecutor.class new file mode 100644 index 0000000..565e3a9 Binary files /dev/null and b/out/production/ConnectFour/Panel$1MyExecutor.class differ diff --git a/out/production/ConnectFour/Panel$MovingKeyListener.class b/out/production/ConnectFour/Panel$MovingKeyListener.class new file mode 100644 index 0000000..1e66cc5 Binary files /dev/null and b/out/production/ConnectFour/Panel$MovingKeyListener.class differ diff --git a/out/production/ConnectFour/Panel.class b/out/production/ConnectFour/Panel.class new file mode 100644 index 0000000..439562f Binary files /dev/null and b/out/production/ConnectFour/Panel.class differ diff --git a/out/production/ConnectFour/SwingWorkerValues.class b/out/production/ConnectFour/SwingWorkerValues.class new file mode 100644 index 0000000..be5c986 Binary files /dev/null and b/out/production/ConnectFour/SwingWorkerValues.class differ