solved the animation problem which was irrelevant but i solved it

This commit is contained in:
2020-08-19 23:13:17 +02:00
parent 818786cfba
commit 8b8e5f6c92
12 changed files with 48 additions and 58 deletions

View File

@@ -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();
}

View File

@@ -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);
}
}

View File

@@ -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,7 +32,7 @@ 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();
}
@@ -45,51 +46,49 @@ public class Panel extends JPanel implements ActionListener {
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<Void, SwingWorkerValues>{
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<Void, Void> worker = new SwingWorker<Void, Void>() {
@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<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);
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 {

View File

@@ -1,7 +0,0 @@
public class SwingWorkerValues {
public int row, column;
public SwingWorkerValues(int row, int column){
this.row = row;
this.column = column;
}
}

Binary file not shown.