Im trying to make a rectangle move on certain Input. Although the Input works, the rectangle is not being redrawn in the way I want it to. The former rectangles are not being erased.
`
public class MiniGame_Panel extends JPanel implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
Thread gameThread;
public KeyHandler2 keyH = new KeyHandler2();
public MGPlayer spieler = new MGPlayer(keyH, this);
public MiniGame_Panel() {
setBackground(new Color(222, 228, 128, 0));
setBounds(0, 0, 16*16*3 + 300 + 3, 16*13*3);
setVisible(true);
setFocusable(true);
requestFocus();
this.addKeyListener(keyH);
}
public void startGameThread() {
gameThread = new Thread((Runnable) this);
gameThread.start();
}
public void run() {
double drawInterval = 1000000000/60;
double delta = 0;
long lastTime = System.nanoTime();
long currentTime;
// GAME LOOP
while(gameThread != null) {
currentTime = System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
lastTime = currentTime;
if(delta >= 1) {
update();
repaint();
delta--;
}
}
}
public void update() {
spieler.update();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
spieler.draw(g2);
g2.dispose();
}
}
public class MGPlayer {
private int x, y;
private Rectangle rect;
private int width, height;
private int leben;
private KeyHandler2 keyH;
private MiniGame_Panel mGp;
public MGPlayer(KeyHandler2 keyH, MiniGame_Panel miniGame_Panel) {
x = 10;
y = 100;
width = 50;
height = 100;
leben = 3;
this.keyH = keyH;
this.mGp = miniGame_Panel;
}
boolean allowJump;
public void update() {
if(keyH.stats) {
if(!allowJump) {
this.y -= 8;
}
//System.out.println("Leertaste");
}
if(keyH.left == true) {
this.x--;
//System.out.println("links");
}
if(keyH.right == true) {
this.x++;
//System.out.println("rechts");
}
if(y <= 200) {
this.y++;
allowJump = true;
}else {
allowJump = false;
}
}
public void draw(Graphics2D g2) {
g2.setColor(Color.BLACK);
g2.fillRect(x, y, width, height);
}
}
I just want to draw a rectangle that moves on certain input. This works but instead of redrawing the rectangle, they all stay in the panel.
