undefined
undefined

Reputation: 479

Why is my string not being updated in my paint method?

I'm trying to change the text at position (0, 1) in my grid system which works but for some reason, the number it was before stays there so if I change it from a 0 to a 1, it's a 0 with a 1 overlapping it. What's wrong with my code that's causing this?

Main class:

package Tetris;

import java.awt.Color;
import javax.swing.JFrame;

class Tetris {

    JFrame frame;

    public Tetris() {
        frame = new JFrame("Tetris");
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setResizable(false);
        //frame.setLocationRelativeTo(null);
        frame.getContentPane().setBackground(Color.LIGHT_GRAY);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Board(frame));
    }

    public static void main(String[] args) {
        new Tetris();
    }
}

Game class:

package Tetris;

import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel {

    private static final long serialVersionUID = -5635526290001171288L;

    private int[][] boardLayout;
    private int[] piecePos;

    private float timerCount = 0.0f;

    private String boardLayoutString;

    private Timer timer;

    public Board(JFrame frame) {
        timer = new Timer(10, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });
        timer.start();

        boardLayout = new int[8][16];
        piecePos = new int[] { 1, 0 };
        boardLayoutString = "";

        for(int y = 0; y < 16; y++) { // grid system
            for(int x = 0; x < 8; x++) {
                boardLayout[x][y] = 0;
                if(x == 7) {
                    boardLayoutString += boardLayout[x][y] + "\n";
                } else {
                    boardLayoutString += boardLayout[x][y] + " ";
                }
            }
        }

        frame.addKeyListener(new KeyListener() { // maybe put this somewhere else
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    System.exit(0);
                }
                if(e.getKeyCode() == KeyEvent.VK_Q) {
                    boardLayout[1][0] = 2;
                }
            }
            public void keyReleased(KeyEvent e) {
                //
            }
            public void keyTyped(KeyEvent e) {
                //
            }
        });
    }

    private String boardLayoutToString(int[][] boardLayout) {
        boardLayoutString = "";
        for(int y = 0; y < 16; y++) { // grid system
            for(int x = 0; x < 8; x++) {
                if(x == 7) {
                    boardLayoutString += boardLayout[x][y] + "\n";
                } else {
                    boardLayoutString += boardLayout[x][y] + " ";
                }
            }
        }
        return boardLayoutString;
    }

    private void update() {
        timerCount += 0.01f; // every 10ms, add 0.01 - this means 1.0f = 1s

        if(timerCount >= 1.0f) { // do something every 1s           
//          if(piecePos[1] < 8) {
//              boardLayout[piecePos[0]][piecePos[1]] = 1;
//              piecePos[1]++;
//              if(piecePos[1] > 0) {
//                  boardLayout[piecePos[0]][piecePos[1] - 1] = 0;
//              }
//          } else {
//              piecePos = new int[] { 1, 0 };
//          }
            timerCount = 0.0f;
        }
    }

    public void paint(Graphics g) {
        //Graphics2D g2 = (Graphics2D) g;

        for(int i = 0; i < boardLayoutToString(boardLayout).split("\n").length; i++) {
            g.drawString(boardLayoutToString(boardLayout).split("\n")[i], 50, 50 + (i * 15));
        }
        repaint();
    }
}

Upvotes: 4

Views: 66

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

That source has some questionable features.

  1. For custom painting in a JComponent, use paintComponent(Graphics) instead of paint(Graphics).
  2. Immediately call super.paintComponent(Graphics) to clear the previous drawing.

Other tips

  1. Add @Override notation to ensure compile time checking.
  2. Don't call repaint() from a paint method, instead call it from an ActionListener controlled by a Swing Timer
  3. Set the frame visible after the components are added.

Upvotes: 4

Related Questions