Fixed issue with wrong WindowConstant

This commit is contained in:
delvh 2020-07-01 20:31:55 +02:00
parent a039837e89
commit b584589459
2 changed files with 17 additions and 27 deletions

View File

@ -12,7 +12,7 @@ import java.util.TimerTask;
* Project: <strong>Snake</strong><br> * Project: <strong>Snake</strong><br>
* File: <strong>Viewport.java</strong><br> * File: <strong>Viewport.java</strong><br>
* Created: <strong>01.07.2020</strong><br> * Created: <strong>01.07.2020</strong><br>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since Snake 1.2 * @since Snake 1.2
*/ */
@ -42,24 +42,22 @@ public class Viewport extends Canvas {
/** /**
* Starts the render task. * Starts the render task.
* *
* @since Snake 1.2 * @since Snake 1.2
*/ */
public void start() { public void start() {
if (renderTask != null) if (renderTask != null) renderTask.cancel();
renderTask.cancel(); else createBufferStrategy(2);
else
createBufferStrategy(2);
renderTask = new TimerTask() { renderTask = new TimerTask() {
private long lastTime = System.currentTimeMillis(); // private long lastTime = System.currentTimeMillis();
@Override @Override
public void run() { public void run() {
long time = System.currentTimeMillis(); // final long time = System.currentTimeMillis();
double dt = (time - lastTime) * 1E-3; // final double dt = (time - lastTime) * 1E-3;
lastTime = time; // lastTime = time;
// TODO: Delta time adjustment // TODO: Delta time adjustment
gameRoot.tick(); gameRoot.tick();
render(); render();
@ -71,15 +69,13 @@ public class Viewport extends Canvas {
/** /**
* Stops the render task. * Stops the render task.
* *
* @since Snake 1.2 * @since Snake 1.2
*/ */
public void stop() { public void stop() { renderTask.cancel(); }
renderTask.cancel();
}
private void render() { private void render() {
Graphics2D g = (Graphics2D) getBufferStrategy().getDrawGraphics(); final Graphics2D g = (Graphics2D) getBufferStrategy().getDrawGraphics();
// Clear the screen // Clear the screen
g.setColor(Color.BLACK); g.setColor(Color.BLACK);

View File

@ -32,31 +32,25 @@ public class Endscreen extends JDialog {
public Endscreen(int score) { public Endscreen(int score) {
this.score = score; this.score = score;
setTitle("Endscreen"); setTitle("Endscreen");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100, 100, 700, 700); setBounds(100, 100, 700, 700);
getContentPane().setLayout(new BorderLayout()); getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPanel.setLayout(new BorderLayout(0, 0)); contentPanel.setLayout(new BorderLayout(0, 0));
getContentPane().add(contentPanel, BorderLayout.CENTER); getContentPane().add(contentPanel, BorderLayout.CENTER);
JButton btnNewButton = new JButton("Play again"); final JButton btnNewButton = new JButton("Play again");
btnNewButton.setMnemonic(KeyEvent.VK_ENTER); btnNewButton.setMnemonic(KeyEvent.VK_ENTER);
btnNewButton.addActionListener(e -> { btnNewButton.addActionListener(e -> { Main.startGame(); dispose(); });
Main.startGame();
dispose();
});
btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15)); btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15));
contentPanel.add(btnNewButton, BorderLayout.SOUTH); contentPanel.add(btnNewButton, BorderLayout.SOUTH);
JLabel lblDeinPunktestand = new JLabel("Dein Punktestand: " + String.valueOf(score)); final JLabel lblDeinPunktestand = new JLabel("Dein Punktestand: " + String.valueOf(score));
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25)); lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH); contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
Image resultImage = Toolkit.getDefaultToolkit() final Image resultImage = Toolkit.getDefaultToolkit()
.getImage( .getImage(this.getClass().getResource(score < goodOrBadResult ? "/Try_Again.jpg" : "/1211548-200.png"));
this.getClass()
.getResource((score < goodOrBadResult) ? "/Try_Again.jpg" : "/1211548-200.png")
);
resultImage.flush(); resultImage.flush();
} }