gpt4 book ai didi

java - 为什么我的字符串没有在我的绘画方法中更新?

转载 作者:搜寻专家 更新时间:2023-11-01 02:41:04 24 4
gpt4 key购买 nike

我正在尝试更改我的网格系统中位置 (0, 1) 处的文本,但由于某种原因,它之前的数字会保留在那里,所以如果我将它从 0 更改为 1,它是0 与 1 重叠。导致此问题的代码有什么问题?

主类:

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();
}
}

游戏类:

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();
}
}

最佳答案

该来源有一些有问题的特征。

  1. 对于 JComponent 中的自定义绘画,请使用 paintComponent(Graphics) 而不是 paint(Graphics)
  2. 立即调用super.paintComponent(Graphics)清除之前的绘制。

其他提示

  1. 添加@Override 符号以确保编译时检查。
  2. 不要从绘画方法调用 repaint(),而是从由 Swing Timer 控制的 ActionListener 调用它
  3. 在添加组件后设置框架可见。

关于java - 为什么我的字符串没有在我的绘画方法中更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33097181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com