diff --git a/src/main/java/engine/Kernel.java b/src/main/java/engine/Kernel.java index 92b6d528a9e52d33f16791045f08128027d26600..e06b279352eca1947fe1efdbe9baf2c59afc58c8 100644 --- a/src/main/java/engine/Kernel.java +++ b/src/main/java/engine/Kernel.java @@ -8,6 +8,7 @@ import engine.physic.PhysicEngine; import pong.PongObject; import javax.swing.*; +import java.awt.*; import java.awt.event.ActionEvent; import java.io.IOException; import java.util.ArrayList; @@ -36,6 +37,7 @@ public class Kernel { graphicEngine.init(gameName, width, height); // Will start the kernel and set the keys listeners graphicEngine.drawRect(); + engines.add(this.graphicEngine);// this.physicEngine = new PhysicEngine(); engines.add(physicEngine); @@ -173,4 +175,9 @@ public class Kernel { public void setGameObject(ArrayList<PongObject> gameObject) { gameObjects = gameObject; } + + public void addButton(JButton button){ + button.setSize(50, 30); + graphicEngine.addButton(button); + } } diff --git a/src/main/java/engine/graphic/GraphicEngine.java b/src/main/java/engine/graphic/GraphicEngine.java index 5c210bea30051b40abcfa38d0e2d5f7ab54c9fc6..7757f48f26455174b2f7b6f5e4c29a3495dc534c 100644 --- a/src/main/java/engine/graphic/GraphicEngine.java +++ b/src/main/java/engine/graphic/GraphicEngine.java @@ -8,10 +8,12 @@ import pong.PongObject; import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; import java.io.IOException; public class GraphicEngine extends JFrame implements Engine { - + JPanel controlPanel = new JPanel(); + JPanel gamePanel = new JPanel(); public GraphicEngine() throws IOException { } @@ -22,24 +24,35 @@ public class GraphicEngine extends JFrame implements Engine { this.setMinimumSize(new Dimension(width, height)); // this.setContentPane(mainPanel); 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); + //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.pack(); this.setResizable(false); this.setVisible(true); this.setLayout(this.getLayout()); - this.revalidate(); - this.repaint(); } public void draw(ImageComponent ic) throws IOException { - getContentPane().add(ic); + this.getContentPane().add(ic); + repaint(); + revalidate(); } public void start(){ + for (PongObject po : Kernel.gameObjects ) { try { @@ -49,53 +62,47 @@ public class GraphicEngine extends JFrame implements Engine { } } 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(); - revalidate(); } @Override public void update(){ - - //updateContentPane(); - + //System.out.println(gamePanel.isVisible()); repaint(); - revalidate(); } @Override public void restart() { - getContentPane().removeAll(); + for(Component c : this.getContentPane().getComponents()){ + if(!(c instanceof JPanel)){ + this.getContentPane().remove(c); + } + } 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(){ this.getContentPane().add(new RectangleComponent(100, 100, PongApp.rectWidth, PongApp.rectHeight)); + repaint(); + revalidate(); } public void drawScore(int score1, int 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() - ) { - //System.out.println(jc.toString()); - System.out.println(jc.getX() + " " + jc.getY()); - jc.setLocation(0, 0); - getContentPane().getComponents()[0].setLocation(0, 0); - jc.repaint(); - } - - */ - getContentPane().repaint(); + controlPanel.add(button); + repaint(); + revalidate(); } + } diff --git a/src/main/java/engine/physic/PhysicEngine.java b/src/main/java/engine/physic/PhysicEngine.java index f56f2e8335a3af1a724221d959bf399e7d68f734..e0f2234f6b3aa3ee87b3a101d070c694eda6bc6c 100644 --- a/src/main/java/engine/physic/PhysicEngine.java +++ b/src/main/java/engine/physic/PhysicEngine.java @@ -60,6 +60,7 @@ public class PhysicEngine implements Engine { @Override public void restart() { + //pongObjectEntityHashMap = new HashMap<>(); for (PongObject pongObject: Kernel.gameObjects) { Coordinates2D speed = new Coordinates2D(0, 0); if(pongObject.getName().equals("Ball")){ diff --git a/src/main/java/pong/PongApp.java b/src/main/java/pong/PongApp.java index 5cbd114459f3685e5db221db17fe7232fd2cc213..e6e563e53b3c16685eb5bfdbe35df28ccbd4e399 100644 --- a/src/main/java/pong/PongApp.java +++ b/src/main/java/pong/PongApp.java @@ -4,6 +4,7 @@ import engine.physic.Coordinates2D; import engine.Kernel; +import javax.swing.*; import java.io.IOException; import java.util.ArrayList; @@ -32,7 +33,6 @@ public class PongApp { int racketWidth = 20; int racketHeight = 120; - 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); @@ -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); 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/sprint2_demo/asteroid.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth); components.add(pongBall); 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 diff --git a/src/main/java/pong/PongBall.java b/src/main/java/pong/PongBall.java index 7b3a70fb879d901b3ba5b443d661838bc770f71e..673469c4c2a69dae5293ff1e78676125f2560829 100644 --- a/src/main/java/pong/PongBall.java +++ b/src/main/java/pong/PongBall.java @@ -10,6 +10,7 @@ public class PongBall implements PongObject { private final int width; private final int height; private Coordinates2D position; + private final Coordinates2D initPosition; private final Image image; PongBall(String name, String imagePath, Coordinates2D position, int width, int height){ @@ -18,6 +19,7 @@ public class PongBall implements PongObject { this.height = height; this.image = Toolkit.getDefaultToolkit().getImage(imagePath); this.position = position; + this.initPosition = position; } @Override @@ -55,4 +57,9 @@ public class PongBall implements PongObject { public String getName() { return name; } + + @Override + public Coordinates2D getInitPosition() { + return initPosition; + } } diff --git a/src/main/java/pong/PongObject.java b/src/main/java/pong/PongObject.java index e12fdbbd201597f2d77dbd4953f66043d97233c0..b40f5bea2af6a2d08781a0a811301a6eeb42d386 100644 --- a/src/main/java/pong/PongObject.java +++ b/src/main/java/pong/PongObject.java @@ -13,4 +13,6 @@ public interface PongObject { public void setPosition(Coordinates2D position); public String getName(); + + Coordinates2D getInitPosition(); } diff --git a/src/main/java/pong/PongRacket.java b/src/main/java/pong/PongRacket.java index d87be47695fc5b3b4703fbc2625f710aa14bb100..59009daa706f6649e6a5aae49d7b2ddf803c669f 100644 --- a/src/main/java/pong/PongRacket.java +++ b/src/main/java/pong/PongRacket.java @@ -10,6 +10,7 @@ public class PongRacket implements PongObject { private final int width; private final int height; private Coordinates2D position; + private final Coordinates2D initPosition; private final Image image; public PongRacket(String name, String imagePath, Coordinates2D position, int width, int height) { this.name = name; @@ -17,6 +18,7 @@ public class PongRacket implements PongObject { this.height = height; this.image = Toolkit.getDefaultToolkit().getImage(imagePath); this.position = position; + this.initPosition = position; } @Override @@ -53,4 +55,9 @@ public class PongRacket implements PongObject { public String getName() { return name; } + + @Override + public Coordinates2D getInitPosition() { + return initPosition; + } }