diff --git a/ConnectFour/src/Chip.java b/ConnectFour/src/Chip.java index 6b741b8..5e25699 100644 --- a/ConnectFour/src/Chip.java +++ b/ConnectFour/src/Chip.java @@ -1,19 +1,25 @@ 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 static int tileWidth, tileHeight = 100; + private String red = "ConnectFour/res/redChip.png"; + private String yellow = "ConnectFour/res/yellowChip.png"; private Point position; private boolean isRed; private Image chip; - public Chip(String path, boolean isRed, int tileWidth, int tileHeight) { + public Chip(boolean isRed, int x, int y) { this.isRed = isRed; position = new Point(); + position.setLocation(x, y); try { - chip = ImageIO.read(new File(path)).getScaledInstance(tileWidth - 15, tileHeight - 15, Image.SCALE_DEFAULT); + if (isRed){ + chip = ImageIO.read(new File(red)).getScaledInstance(tileWidth - 15, tileHeight - 15, Image.SCALE_DEFAULT); + } else { + chip = ImageIO.read(new File(yellow)).getScaledInstance(tileWidth - 15, tileHeight - 15, Image.SCALE_DEFAULT); + } } catch (IOException e) { e.printStackTrace(); } diff --git a/ConnectFour/src/Painter.java b/ConnectFour/src/Painter.java index 42055fc..e9557c6 100644 --- a/ConnectFour/src/Painter.java +++ b/ConnectFour/src/Painter.java @@ -23,6 +23,8 @@ public class Painter { public void paintBoard(Graphics g){ Graphics2D g2d = (Graphics2D) g; + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); g2d.setStroke(new BasicStroke(2)); for (int i = 0; i < board_w/tileWidth; i++){ for (int j = 0; j < board_h/tileHeight; j++){ @@ -34,22 +36,12 @@ public class Painter { } } - public void paintRedChip(Graphics g, int x, int y) { - String path = "ConnectFour/res/redChip.png"; + public void paintChip(Graphics g, boolean isRed, int x, int y) { Graphics2D g2d = (Graphics2D) 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); - } - - 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); + Chip chip = new Chip(isRed,x, y); + chip.setPosition(x, y); + panel.addToBoard(chip); + g2d.drawImage(chip.getImage(), x * tileWidth + 10 , y * tileHeight + 5, panel); } } diff --git a/ConnectFour/src/Panel.java b/ConnectFour/src/Panel.java index 76ae04c..6e9b43b 100644 --- a/ConnectFour/src/Panel.java +++ b/ConnectFour/src/Panel.java @@ -7,12 +7,13 @@ import java.util.concurrent.ThreadLocalRandom; public class Panel extends JPanel implements ActionListener { - private final int DELAY = 50; + private final int DELAY = 10; 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 isFinished = true; private boolean right,left,space; + private int times = 0; private Painter painter; private Timer timer; @@ -31,65 +32,63 @@ public class Panel extends JPanel implements ActionListener { private void initGame() { painter = new Painter(this, BOARD_W, BOARD_H); timer = new Timer(DELAY, this); - //timer.start(); + timer.start(); } @Override public void actionPerformed(ActionEvent e) { - repaint(); + repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); painter.paintBoard(g); - if (!isInitialized) { - fillBoard(g); - isInitialized = true; + actualizeBoard(g); + if (isFinished){ + fillBoard(); + isFinished = false; } } - private void fillBoard(Graphics g) { - class MyExecutor extends SwingWorker{ - Graphics g; - public MyExecutor(Graphics g){ - this.g = g; + public void actualizeBoard(Graphics g){ + for (Chip[] chips : board) { + for (Chip chip: chips) { + if (chip != null){ + painter.paintChip(g, chip.isRed(), chip.getPosition().x, chip.getPosition().y); + } } + } + } + + public void fillBoard(){ + SwingWorker worker = new SwingWorker() { @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)); + for (int column = 0; column < 7; column++){ + for (int row = 0; row < 7; row++){ + Thread.sleep(10); + addToBoard(new Chip(times % 2 == 0, 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); - + times++; + isFinished = true; + System.out.println(times); } - } - MyExecutor executor = new MyExecutor(g); - executor.execute(); + }; + worker.execute(); } - public void addToBoard (Chip chip, int x, int y){ - board[x][y] = chip; + public void addToBoard (Chip chip){ + board[chip.getPosition().x][chip.getPosition().y] = chip; } class MovingKeyListener implements KeyListener { diff --git a/ConnectFour/src/SwingWorkerValues.java b/ConnectFour/src/SwingWorkerValues.java deleted file mode 100644 index 32c28f1..0000000 --- a/ConnectFour/src/SwingWorkerValues.java +++ /dev/null @@ -1,7 +0,0 @@ -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/Chip.class b/out/production/ConnectFour/Chip.class index 3f5b87b..6a9e95b 100644 Binary files a/out/production/ConnectFour/Chip.class and b/out/production/ConnectFour/Chip.class differ diff --git a/out/production/ConnectFour/META-INF/ConnectFour.kotlin_module b/out/production/ConnectFour/META-INF/ConnectFour.kotlin_module deleted file mode 100644 index a49347a..0000000 Binary files a/out/production/ConnectFour/META-INF/ConnectFour.kotlin_module and /dev/null differ diff --git a/out/production/ConnectFour/Painter.class b/out/production/ConnectFour/Painter.class index 749d68b..d360373 100644 Binary files a/out/production/ConnectFour/Painter.class and b/out/production/ConnectFour/Painter.class differ diff --git a/out/production/ConnectFour/Panel$1.class b/out/production/ConnectFour/Panel$1.class new file mode 100644 index 0000000..6bc4829 Binary files /dev/null and b/out/production/ConnectFour/Panel$1.class differ diff --git a/out/production/ConnectFour/Panel$1MyExecutor.class b/out/production/ConnectFour/Panel$1MyExecutor.class deleted file mode 100644 index 565e3a9..0000000 Binary files a/out/production/ConnectFour/Panel$1MyExecutor.class and /dev/null differ diff --git a/out/production/ConnectFour/Panel$MovingKeyListener.class b/out/production/ConnectFour/Panel$MovingKeyListener.class index 1e66cc5..ef44497 100644 Binary files a/out/production/ConnectFour/Panel$MovingKeyListener.class and b/out/production/ConnectFour/Panel$MovingKeyListener.class differ diff --git a/out/production/ConnectFour/Panel.class b/out/production/ConnectFour/Panel.class index 439562f..916b9d6 100644 Binary files a/out/production/ConnectFour/Panel.class and b/out/production/ConnectFour/Panel.class differ diff --git a/out/production/ConnectFour/SwingWorkerValues.class b/out/production/ConnectFour/SwingWorkerValues.class deleted file mode 100644 index be5c986..0000000 Binary files a/out/production/ConnectFour/SwingWorkerValues.class and /dev/null differ