From ff479be47a1db1453369c34d54a20dc0feae8ca2 Mon Sep 17 00:00:00 2001 From: "Brady J. Garvin" <bgarvin@cse.unl.edu> Date: Tue, 29 Sep 2020 13:19:10 -0500 Subject: [PATCH] As a temporary workaround for rendering problems under VMware Fusion, switched to the thread-unsafe usage of `paintImmediately`. --- .../edu/unl/cse/soft160/tetris/Tetris.java | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/edu/unl/cse/soft160/tetris/Tetris.java b/src/main/java/edu/unl/cse/soft160/tetris/Tetris.java index c7aaac8..eb2841b 100644 --- a/src/main/java/edu/unl/cse/soft160/tetris/Tetris.java +++ b/src/main/java/edu/unl/cse/soft160/tetris/Tetris.java @@ -7,6 +7,7 @@ import java.awt.Font; import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; +import javax.swing.JPanel; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -184,7 +185,8 @@ public class Tetris extends JFrame { protected void placeShape() { drawShape(COLORS[shape]); - repaint(); + JPanel panel = (JPanel)getContentPane(); + panel.paintImmediately(0, 0, panel.getWidth(), panel.getHeight()); } protected void unplaceShape() { @@ -271,28 +273,29 @@ public class Tetris extends JFrame { maybeAdjustShape(0, 1, 0); } - @Override - public void paint(Graphics graphics) { - super.paint(graphics); - int xScale = getWidth() / WIDTH; - int yScale = getHeight() / HEIGHT; - for (int x = 0; x < WIDTH; ++x) { - for (int y = 0; y < HEIGHT; ++y) { - graphics.setColor(board[x][y]); - graphics.fillRect(xScale * x, yScale * y, xScale, yScale); - } - } - String scoreString = (playing ? ROWS_CLEARED : FINAL_SCORE) + SCORE_SEPARATOR + String.valueOf(score); - graphics.setFont(TEXT_FONT); - graphics.setColor(TEXT_COLOR); - graphics.drawString(scoreString, (getWidth() - graphics.getFontMetrics().stringWidth(scoreString)) / 2, - (getHeight() / 2 - graphics.getFontMetrics().getHeight()) / 2); - } - public Tetris(int width, int height) { super("Tetris"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(width, height); + setContentPane(new JPanel() { + @Override + public void paint(Graphics graphics) { + super.paint(graphics); + int xScale = getWidth() / Tetris.WIDTH; + int yScale = getHeight() / Tetris.HEIGHT; + for (int x = 0; x < Tetris.WIDTH; ++x) { + for (int y = 0; y < Tetris.HEIGHT; ++y) { + graphics.setColor(board[x][y]); + graphics.fillRect(xScale * x, yScale * y, xScale, yScale); + } + } + String scoreString = (playing ? ROWS_CLEARED : FINAL_SCORE) + SCORE_SEPARATOR + String.valueOf(score); + graphics.setFont(TEXT_FONT); + graphics.setColor(TEXT_COLOR); + graphics.drawString(scoreString, (getWidth() - graphics.getFontMetrics().stringWidth(scoreString)) / 2, + (getHeight() / 2 - graphics.getFontMetrics().getHeight()) / 2); + } + }); addKeyListener(new KeyListener() { public void keyPressed(KeyEvent event) { switch (event.getKeyCode()) { -- GitLab