java - 以前的字符实例不会被删除
问题描述
这就像如果你在一个镜子迷宫中,你可能很难找到自己的一个特定反射。所以你会对自己有多重反映,当你试图找到对自己的直接反映时,你会感到困惑。
可悲的是我无法发布图片............所以......
字符是“C”
它从
C
至
抄送
当我试图向右移动时。
package WASD;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class WASD extends JFrame implements KeyListener, ActionListener{
private int playerX = 400;
private int playerY = 400;
int C = 1;
private int RESTART = 0; // Three ENTER = Restart
private Timer timer;
JFrame j = new JFrame();
private int previousX = -50;
private int previousY = -50;
public WASD() {
JPanel panel = new JPanel();
setTitle("Not supposed to be a painting program.");
setSize(2000, 2000);
timer = new Timer(8, this);
this.getContentPane().add(panel);
addKeyListener(this);
setVisible(true);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){
WASD W = new WASD();
}
public void paint(Graphics g) { //TODO paint is only working once, then failing forever.
//////////////////////////
g.setColor(Color.BLACK);
g.drawRect(0, 0, 3000, 3000); // Background
if(C == 1) g.setColor(Color.RED);
else if (C == 2) g.setColor(Color.BLUE);
else if (C == 3) g.setColor(Color.GREEN);
else if (C == 4) g.setColor(Color.YELLOW);
else if(C>4){
g.setColor(Color.GRAY);
try {
Thread.sleep(1000); //Gray pauses for a second I guess...
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
C = 1;
}
g.fillRect(playerX, playerY, 50, 50); // Player/
g.setColor(Color.BLACK);
g.fillRect(previousX, previousY, 50, 50);
g.dispose(); //Last thing
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
repaint();
}
@Override
public void keyPressed(KeyEvent K) {
// TODO Auto-generated method stub
int e = K.getKeyCode();
repaint(); //timer.start(); does not affect this. g.dispose() is unavailable.
/*
If this repaint is used, then the previous locations
that the player was at will be painted as well.
Basically, instead of moving, all instances
made will not be deleted. #PaintingApplication
However, if it is not used, then the CHARACTER will not move at all.
It seems that the problem is not about
playerX, or playerY,
instead, it is about the program not refreshing.
*/
if((e == KeyEvent.VK_RIGHT || e == KeyEvent.VK_D) && playerX<2000){
playerX += 200;
}
else if((e == KeyEvent.VK_LEFT || e == KeyEvent.VK_A) && playerX>0){
playerX -= 200;
}
else if((e == KeyEvent.VK_UP || e == KeyEvent.VK_W) && playerY>0){
playerY -= 200;
}
else if(e == KeyEvent.VK_DOWN || e == KeyEvent.VK_S && playerY<2000){ // Easter egg bug 1
playerY += 200;
}
else if (e == KeyEvent.VK_C){
C++;
}
else if(e == KeyEvent.VK_ENTER){
if(RESTART == 3){
playerX = 400;
playerY = 400;
RESTART = 0; //TODO
repaint();
}
else{
RESTART++;
}
}
else{
RESTART = 0;
}
}
@Override public void keyTyped(KeyEvent arg0) {}
@Override public void keyReleased(KeyEvent arg0) {}
}
解决方案
我为犯了这么简单的错误而道歉。问题出在“paint”函数中。线
g.drawRect(0, 0, 3000, 3000); //Outline of Rectangle
应该
g.fillRect(0, 0, 3000, 3000); //Actually filling the background.
推荐阅读
- c# - 在 Firebase REST API 上放置 JSON 数据时出现 BadRequest
- python - 从 2 个自动编码器中提取特征并将它们输入 MLP
- angular - 如何从Angular中的HttpClient发出的get请求中返回数据对象?
- javascript - Onclick 单个列表项图标的切换类 Angular 4 5 Todo 应用程序
- server - 路径中的 Plesk 变音符号不起作用
- go - 如何将 []*type 传递给函数?
- android-studio - 构建失败:无法处理 jetified-Android Jetpack
- ios - Swift 4 - 特定视图控制器状态栏样式不变
- boot - 信任链和tpm?
- c - 如何找到scanf没有任何输入