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 c7aaac81b1086735a9d128320b111a99e0d389c0..eb2841b67bf47a33de6b03b6fe884bf69edf72b5 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()) {