solved the animation problem which was irrelevant but i solved it
This commit is contained in:
@@ -1,19 +1,25 @@
|
|||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.swing.text.Position;
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Chip {
|
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 Point position;
|
||||||
private boolean isRed;
|
private boolean isRed;
|
||||||
private Image chip;
|
private Image chip;
|
||||||
public Chip(String path, boolean isRed, int tileWidth, int tileHeight) {
|
public Chip(boolean isRed, int x, int y) {
|
||||||
this.isRed = isRed;
|
this.isRed = isRed;
|
||||||
position = new Point();
|
position = new Point();
|
||||||
|
position.setLocation(x, y);
|
||||||
try {
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ public class Painter {
|
|||||||
|
|
||||||
public void paintBoard(Graphics g){
|
public void paintBoard(Graphics g){
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||||
|
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
g2d.setStroke(new BasicStroke(2));
|
g2d.setStroke(new BasicStroke(2));
|
||||||
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++){
|
||||||
@@ -34,22 +36,12 @@ public class Painter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paintRedChip(Graphics g, int x, int y) {
|
public void paintChip(Graphics g, boolean isRed, int x, int y) {
|
||||||
String path = "ConnectFour/res/redChip.png";
|
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
Chip redChip = new Chip(path, true, tileWidth, tileHeight);
|
Chip chip = new Chip(isRed,x, y);
|
||||||
redChip.setPosition(x, y);
|
chip.setPosition(x, y);
|
||||||
panel.addToBoard(redChip, x, y);
|
panel.addToBoard(chip);
|
||||||
g2d.drawImage(redChip.getImage(), x * tileWidth + 10, y * tileHeight + 5, panel);
|
g2d.drawImage(chip.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||||||
|
|
||||||
public class Panel extends JPanel implements ActionListener {
|
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_W = 700;
|
||||||
private final int BOARD_H = 600;
|
private final int BOARD_H = 600;
|
||||||
private Chip[][] board = new Chip[7][6];
|
private Chip[][] board = new Chip[7][6];
|
||||||
private boolean isInitialized = false;
|
private boolean isFinished = true;
|
||||||
private boolean right,left,space;
|
private boolean right,left,space;
|
||||||
|
private int times = 0;
|
||||||
|
|
||||||
private Painter painter;
|
private Painter painter;
|
||||||
private Timer timer;
|
private Timer timer;
|
||||||
@@ -31,65 +32,63 @@ public class Panel extends JPanel implements ActionListener {
|
|||||||
private void initGame() {
|
private void initGame() {
|
||||||
painter = new Painter(this, BOARD_W, BOARD_H);
|
painter = new Painter(this, BOARD_W, BOARD_H);
|
||||||
timer = new Timer(DELAY, this);
|
timer = new Timer(DELAY, this);
|
||||||
//timer.start();
|
timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
painter.paintBoard(g);
|
painter.paintBoard(g);
|
||||||
if (!isInitialized) {
|
actualizeBoard(g);
|
||||||
fillBoard(g);
|
if (isFinished){
|
||||||
isInitialized = true;
|
fillBoard();
|
||||||
|
isFinished = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fillBoard(Graphics g) {
|
public void actualizeBoard(Graphics g){
|
||||||
class MyExecutor extends SwingWorker<Void, SwingWorkerValues>{
|
for (Chip[] chips : board) {
|
||||||
Graphics g;
|
for (Chip chip: chips) {
|
||||||
public MyExecutor(Graphics g){
|
if (chip != null){
|
||||||
this.g = g;
|
painter.paintChip(g, chip.isRed(), chip.getPosition().x, chip.getPosition().y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fillBoard(){
|
||||||
|
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground() throws Exception {
|
protected Void doInBackground() throws Exception {
|
||||||
for (int column = 5; column >= 0; column--){
|
for (int column = 0; column < 7; column++){
|
||||||
for (int row = 0; row < 7; row++) {
|
for (int row = 0; row < 7; row++){
|
||||||
Thread.sleep(100);
|
Thread.sleep(10);
|
||||||
publish(new SwingWorkerValues(row, column));
|
addToBoard(new Chip(times % 2 == 0, row, column));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
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
|
@Override
|
||||||
protected void done() {
|
protected void done() {
|
||||||
super.done();
|
super.done();
|
||||||
painter.paintYellowChip(g, 2,2);
|
times++;
|
||||||
|
isFinished = true;
|
||||||
|
System.out.println(times);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
MyExecutor executor = new MyExecutor(g);
|
worker.execute();
|
||||||
executor.execute();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addToBoard (Chip chip, int x, int y){
|
public void addToBoard (Chip chip){
|
||||||
board[x][y] = chip;
|
board[chip.getPosition().x][chip.getPosition().y] = chip;
|
||||||
}
|
}
|
||||||
|
|
||||||
class MovingKeyListener implements KeyListener {
|
class MovingKeyListener implements KeyListener {
|
||||||
|
|||||||
@@ -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.
Binary file not shown.
Binary file not shown.
BIN
out/production/ConnectFour/Panel$1.class
Normal file
BIN
out/production/ConnectFour/Panel$1.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user