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

View File

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