Skip to content
Snippets Groups Projects
Commit ee424dc9 authored by bosskkev's avatar bosskkev
Browse files

Rajout boutton restart. Fonction addButton de Kernel

parent 81b5e6da
No related branches found
No related tags found
No related merge requests found
Pipeline #24494 failed
...@@ -8,6 +8,7 @@ import engine.physic.PhysicEngine; ...@@ -8,6 +8,7 @@ import engine.physic.PhysicEngine;
import pong.PongObject; import pong.PongObject;
import javax.swing.*; import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -36,6 +37,7 @@ public class Kernel { ...@@ -36,6 +37,7 @@ public class Kernel {
graphicEngine.init(gameName, width, height); // Will start the kernel and set the keys listeners graphicEngine.init(gameName, width, height); // Will start the kernel and set the keys listeners
graphicEngine.drawRect(); graphicEngine.drawRect();
engines.add(this.graphicEngine);// engines.add(this.graphicEngine);//
this.physicEngine = new PhysicEngine(); this.physicEngine = new PhysicEngine();
engines.add(physicEngine); engines.add(physicEngine);
...@@ -173,4 +175,9 @@ public class Kernel { ...@@ -173,4 +175,9 @@ public class Kernel {
public void setGameObject(ArrayList<PongObject> gameObject) { public void setGameObject(ArrayList<PongObject> gameObject) {
gameObjects = gameObject; gameObjects = gameObject;
} }
public void addButton(JButton button){
button.setSize(50, 30);
graphicEngine.addButton(button);
}
} }
...@@ -8,10 +8,12 @@ import pong.PongObject; ...@@ -8,10 +8,12 @@ import pong.PongObject;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException; import java.io.IOException;
public class GraphicEngine extends JFrame implements Engine { public class GraphicEngine extends JFrame implements Engine {
JPanel controlPanel = new JPanel();
JPanel gamePanel = new JPanel();
public GraphicEngine() throws IOException { public GraphicEngine() throws IOException {
} }
...@@ -22,24 +24,35 @@ public class GraphicEngine extends JFrame implements Engine { ...@@ -22,24 +24,35 @@ public class GraphicEngine extends JFrame implements Engine {
this.setMinimumSize(new Dimension(width, height)); this.setMinimumSize(new Dimension(width, height));
// this.setContentPane(mainPanel); // this.setContentPane(mainPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlPanel.setPreferredSize(new Dimension(width, 30));
controlPanel.setBackground(Color.BLACK);
// Ajoutez controlPanel au nord et gamePanel au centre du mainPanel
this.getContentPane().add(controlPanel, BorderLayout.NORTH);
this.getContentPane().setBackground(Color.BLACK); this.getContentPane().setBackground(Color.BLACK);
//mainPanel.add(gamePanel, BorderLayout.CENTER);
// Ajoutez mainPanel comme contenu principal
//this.setContentPane(mainPanel);
//this.setContentPane(mainPanel);
//this.getContentPane().getGraphics().setColor(Color.BLACK);
//this.getContentPane().getGraphics().fillRect(0, 0, width, height);
//this.setMenuBar(null);
this.setLocationRelativeTo(null); this.setLocationRelativeTo(null);
this.pack(); this.pack();
this.setResizable(false); this.setResizable(false);
this.setVisible(true); this.setVisible(true);
this.setLayout(this.getLayout()); this.setLayout(this.getLayout());
this.revalidate();
this.repaint();
} }
public void draw(ImageComponent ic) throws IOException { public void draw(ImageComponent ic) throws IOException {
getContentPane().add(ic); this.getContentPane().add(ic);
repaint();
revalidate();
} }
public void start(){ public void start(){
for (PongObject po : Kernel.gameObjects for (PongObject po : Kernel.gameObjects
) { ) {
try { try {
...@@ -49,53 +62,47 @@ public class GraphicEngine extends JFrame implements Engine { ...@@ -49,53 +62,47 @@ public class GraphicEngine extends JFrame implements Engine {
} }
} }
drawRect(); drawRect();
getContentPane().add(new ScoreComponent(new Coordinates2D(100, 100), new Coordinates2D(670, 100), PongApp.score1, PongApp.score2)); this.getContentPane().add(new ScoreComponent(new Coordinates2D(100, 100), new Coordinates2D(670, 100), PongApp.score1, PongApp.score2));
repaint(); repaint();
revalidate(); revalidate();
} }
@Override @Override
public void update(){ public void update(){
//System.out.println(gamePanel.isVisible());
//updateContentPane();
repaint(); repaint();
revalidate(); revalidate();
} }
@Override @Override
public void restart() { public void restart() {
getContentPane().removeAll(); for(Component c : this.getContentPane().getComponents()){
if(!(c instanceof JPanel)){
this.getContentPane().remove(c);
}
}
start(); start();
//drawRect(); //TODO: Trouver un moyen de ne pas avoir à redessiner le rectangle à chaque restart et rendre le restart plus générique //TODO: Trouver un moyen de ne pas avoir à redessiner le rectangle à chaque restart et rendre le restart plus générique
} }
public void drawRect(){ public void drawRect(){
this.getContentPane().add(new RectangleComponent(100, 100, PongApp.rectWidth, PongApp.rectHeight)); this.getContentPane().add(new RectangleComponent(100, 100, PongApp.rectWidth, PongApp.rectHeight));
repaint();
revalidate();
} }
public void drawScore(int score1, int score2){ public void drawScore(int score1, int score2){
this.getContentPane().add(new ScoreComponent(new Coordinates2D(100, 100), new Coordinates2D(500, 100), score1, score2)); this.getContentPane().add(new ScoreComponent(new Coordinates2D(100, 100), new Coordinates2D(500, 100), score1, score2));
} }
public void addButton(JButton button){
//TODO: add button action
public void updateContentPane() {
System.out.println(getContentPane().getComponents().length);
/*for (Component jc: getContentPane().getComponents() controlPanel.add(button);
) { repaint();
//System.out.println(jc.toString()); revalidate();
System.out.println(jc.getX() + " " + jc.getY());
jc.setLocation(0, 0);
getContentPane().getComponents()[0].setLocation(0, 0);
jc.repaint();
} }
*/
getContentPane().repaint();
}
} }
...@@ -60,6 +60,7 @@ public class PhysicEngine implements Engine { ...@@ -60,6 +60,7 @@ public class PhysicEngine implements Engine {
@Override @Override
public void restart() { public void restart() {
//pongObjectEntityHashMap = new HashMap<>();
for (PongObject pongObject: Kernel.gameObjects) { for (PongObject pongObject: Kernel.gameObjects) {
Coordinates2D speed = new Coordinates2D(0, 0); Coordinates2D speed = new Coordinates2D(0, 0);
if(pongObject.getName().equals("Ball")){ if(pongObject.getName().equals("Ball")){
......
...@@ -4,6 +4,7 @@ import engine.physic.Coordinates2D; ...@@ -4,6 +4,7 @@ import engine.physic.Coordinates2D;
import engine.Kernel; import engine.Kernel;
import javax.swing.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -32,7 +33,6 @@ public class PongApp { ...@@ -32,7 +33,6 @@ public class PongApp {
int racketWidth = 20; int racketWidth = 20;
int racketHeight = 120; int racketHeight = 120;
int ballWidth = 32; int ballWidth = 32;
PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, height/2 - racketHeight/2), racketWidth, racketHeight); PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, height/2 - racketHeight/2), racketWidth, racketHeight);
...@@ -41,13 +41,23 @@ public class PongApp { ...@@ -41,13 +41,23 @@ public class PongApp {
PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, height/2 - racketHeight/2), racketWidth, racketHeight); PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, height/2 - racketHeight/2), racketWidth, racketHeight);
components.add(rightRacket); components.add(rightRacket);
// TODO: Add rackets
PongBall pongBall = new PongBall("Ball", "src/main/resources/pong/ball.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth); PongBall pongBall = new PongBall("Ball", "src/main/resources/pong/ball.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
//PongBall pongBall = new PongBall("Ball", "src/main/resources/sprint2_demo/asteroid.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
components.add(pongBall); components.add(pongBall);
Kernel kernel = new Kernel("Pong", width, height, components); Kernel kernel = new Kernel("Pong", width, height, components);
//kernel.setGameObject(components); JButton button = new JButton("Restart");
button.addActionListener(e -> {
score1 = 0;
score2 = 0;
System.out.println("Restarting");
try {
kernel.restart();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
kernel.addButton(button); // Add a button to restart the game
final long MIN_INTERVAL_TIME = 10; // Minimum time between two iterations final long MIN_INTERVAL_TIME = 10; // Minimum time between two iterations
......
...@@ -10,6 +10,7 @@ public class PongBall implements PongObject { ...@@ -10,6 +10,7 @@ public class PongBall implements PongObject {
private final int width; private final int width;
private final int height; private final int height;
private Coordinates2D position; private Coordinates2D position;
private final Coordinates2D initPosition;
private final Image image; private final Image image;
PongBall(String name, String imagePath, Coordinates2D position, int width, int height){ PongBall(String name, String imagePath, Coordinates2D position, int width, int height){
...@@ -18,6 +19,7 @@ public class PongBall implements PongObject { ...@@ -18,6 +19,7 @@ public class PongBall implements PongObject {
this.height = height; this.height = height;
this.image = Toolkit.getDefaultToolkit().getImage(imagePath); this.image = Toolkit.getDefaultToolkit().getImage(imagePath);
this.position = position; this.position = position;
this.initPosition = position;
} }
@Override @Override
...@@ -55,4 +57,9 @@ public class PongBall implements PongObject { ...@@ -55,4 +57,9 @@ public class PongBall implements PongObject {
public String getName() { public String getName() {
return name; return name;
} }
@Override
public Coordinates2D getInitPosition() {
return initPosition;
}
} }
...@@ -13,4 +13,6 @@ public interface PongObject { ...@@ -13,4 +13,6 @@ public interface PongObject {
public void setPosition(Coordinates2D position); public void setPosition(Coordinates2D position);
public String getName(); public String getName();
Coordinates2D getInitPosition();
} }
...@@ -10,6 +10,7 @@ public class PongRacket implements PongObject { ...@@ -10,6 +10,7 @@ public class PongRacket implements PongObject {
private final int width; private final int width;
private final int height; private final int height;
private Coordinates2D position; private Coordinates2D position;
private final Coordinates2D initPosition;
private final Image image; private final Image image;
public PongRacket(String name, String imagePath, Coordinates2D position, int width, int height) { public PongRacket(String name, String imagePath, Coordinates2D position, int width, int height) {
this.name = name; this.name = name;
...@@ -17,6 +18,7 @@ public class PongRacket implements PongObject { ...@@ -17,6 +18,7 @@ public class PongRacket implements PongObject {
this.height = height; this.height = height;
this.image = Toolkit.getDefaultToolkit().getImage(imagePath); this.image = Toolkit.getDefaultToolkit().getImage(imagePath);
this.position = position; this.position = position;
this.initPosition = position;
} }
@Override @Override
...@@ -53,4 +55,9 @@ public class PongRacket implements PongObject { ...@@ -53,4 +55,9 @@ public class PongRacket implements PongObject {
public String getName() { public String getName() {
return name; return name;
} }
@Override
public Coordinates2D getInitPosition() {
return initPosition;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment