重绘并不总是有效

Repaint Not Always Working

每次移动时,我都会重新绘制角色的位置和之前的位置。这在大多数情况下都有效。偶尔并且看似随机地,角色的图像会被留下,并且不会重新绘制,即使在继续移动之后也是如此。

Class 处理图形和角色移动:

public class GameState extends JFrame implements KeyListener {
Container contentPane=this.getContentPane();
Graphics bufferGraphics;

int characterX=463;
int characterY=486;
int oldCharacterX=463;
int oldCharacterY=486;
int xAxis;
int yAxis;
Image characterNorth = CustomImages.createImageIcon("Images/characterNorth.jpg").getImage();
Image characterEast = CustomImages.createImageIcon("Images/characterEast.jpg").getImage();
Image characterSouth = CustomImages.createImageIcon("Images/characterSouth.jpg").getImage();
Image characterWest = CustomImages.createImageIcon("Images/characterWest.jpg").getImage();
Image brickWall = CustomImages.createImageIcon("Images/brickWall.jpg").getImage();
Image brickFloor = CustomImages.createImageIcon("Images/brickFloor.jpg").getImage();
Image character=characterNorth;
boolean pressed=false;

ArrayList<RoomState> map = new ArrayList<RoomState>();
RoomState currentRoom = new RoomState();
RoomState currentRoomState=new RoomState();

GameState() {
    this.setBounds(0, 0, 1680, 1050);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(this);
    setFocusable(true);
    requestFocusInWindow();
}

public void move(int x, int y) { //Check Move
    currentRoomState=currentRoomState.MoveToNextRoom(true, false, false, false);
    currentRoomState=currentRoomState.MoveToNextRoom(false, true, false, false);
    currentRoomState=currentRoomState.MoveToNextRoom(false, false, true, false);
    currentRoomState=currentRoomState.MoveToNextRoom(false, false, false, true);
}

public void paint(Graphics g) { //Graphics
    for(xAxis=58;xAxis<=858;xAxis=xAxis+50) {
        for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
            g.drawImage(brickFloor,xAxis,yAxis,null);
        }
        yAxis=31;
    }
    for(xAxis=8;xAxis<958;xAxis=xAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    yAxis=931;
    for(xAxis=8;xAxis<=908;xAxis=xAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    xAxis=8;
    for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    xAxis=908;
    for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }

    if(currentRoom.northDoor) {
        g.drawImage(brickFloor,458,31,null);
    }
    if(currentRoom.eastDoor) {
        g.drawImage(brickFloor,908,481,null);
    }
    if(currentRoom.southDoor) {
        g.drawImage(brickFloor,458,931,null);
    }
    if(currentRoom.westDoor) {
        g.drawImage(brickFloor,8,481,null);
    }

    g.drawImage(character,characterX,characterY,null);
}

@Override
public void keyPressed(KeyEvent arg0) { //Character Rotation/Movement.
    if(pressed==false) {
        pressed=true;
        if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_UP) {
            if(character==characterNorth) {
                if(characterY>86 && characterX>13 && characterX<913) {
                    characterY=characterY-50;
                }else if(currentRoom.northDoor && characterX==463) {
                    oldCharacterY=characterY;
                    characterY=characterY-50;
                    if(characterY==-14) {
                        RoomState nextRoom = new RoomState();
                        nextRoom.southDoor = true;
                        nextRoom.rs_SouthDoor = currentRoom;
                        currentRoom.rs_NorthDoor = nextRoom;
                        map.add(nextRoom);
                        currentRoom = nextRoom;
                        nextRoom = null;
                        characterX=463;
                        characterY=936;
                        repaint();
                    }
                }
            }else {
                character=characterNorth;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_A || arg0.getKeyCode() == KeyEvent.VK_LEFT) {
            if(character==characterWest && characterY>36 && characterY<926) {
                if(characterX>63) {
                    oldCharacterX=characterX;
                    characterX=characterX-50;
                }else if(currentRoom.westDoor && characterY==486) {
                    oldCharacterX=characterX;
                    characterX=characterX-50;
                    if(characterX==-37) {
                        RoomState nextRoom = new RoomState();
                        nextRoom.eastDoor = true;
                        nextRoom.rs_EastDoor = currentRoom;
                        currentRoom.rs_WestDoor = nextRoom;
                        map.add(nextRoom);
                        currentRoom = nextRoom;
                        nextRoom = null;
                        characterX=913;
                        characterY=486;
                        repaint();
                    }
                }
            }else {
                character=characterWest;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_S || arg0.getKeyCode() == KeyEvent.VK_DOWN) {
            if(character==characterSouth) {
                if(characterY<871 && characterX>13 && characterX<913) {
                    oldCharacterY=characterY;
                    characterY=characterY+50;
                }else if(currentRoom.southDoor && characterX==463) {
                    oldCharacterY=characterY;
                    characterY=characterY+50;
                    if(characterY==986) {
                        RoomState nextRoom = new RoomState();
                        nextRoom.northDoor = true;
                        nextRoom.rs_NorthDoor = currentRoom;
                        currentRoom.rs_SouthDoor = nextRoom;
                        map.add(nextRoom);
                        currentRoom = nextRoom;
                        nextRoom = null;
                        characterX=463;
                        characterY=36;
                        repaint();
                    }
                }
            }else {
                character=characterSouth;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
            if(character==characterEast && characterY>36 && characterY<926) {
                if(characterX<848) {
                    oldCharacterX=characterX;
                    characterX=characterX+50;
                }else if(currentRoom.eastDoor && characterY==486) {
                    oldCharacterX=characterX;
                    characterX=characterX+50;
                    if(characterX==963) {
                        RoomState nextRoom = new RoomState();
                        nextRoom.westDoor = true;
                        nextRoom.rs_WestDoor = currentRoom;
                        currentRoom.rs_EastDoor = nextRoom;
                        map.add(nextRoom);
                        currentRoom = nextRoom;
                        nextRoom = null;
                        characterX=13;
                        characterY=486;
                        repaint();
                    }
                }
            }else {
                 character=characterEast;
            }
        }
        repaint(oldCharacterX,oldCharacterY,40,40);
        repaint(characterX,characterY,40,40);
        }
    }

@Override
public void keyReleased(KeyEvent arg0) { //Prevents Holding Down Keys.
    if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_UP) {
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_A || arg0.getKeyCode() == KeyEvent.VK_LEFT) {
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_S || arg0.getKeyCode() == KeyEvent.VK_DOWN) {
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
        pressed=false;
    }
}

@Override
public void keyTyped(KeyEvent arg0) {
}

}

您可以在 keyPressed() 方法中设置 oldCharacterXoldCharacterY,具体取决于按下的是哪个键。然而,有时您会同时设置 X 和 Y 坐标!看起来其中一组应该是空操作,但为什么要冒险?

相反,您应该无条件地将两者都设置在函数的顶部。然后,您可以在每个按键情况下删除 oldCharacterX/Y 的冗余设置。

public void keyPressed(KeyEvent arg0) {
    if(pressed==false) {
        pressed=true;
        oldCharacterX = characterX;
        oldCharacterY = characterY;

        //... keypress handling ...

        if (oldCharacterX != characterX  ||  oldCharacterY != characterY) {
            // Only need to repaint old position if it is different from the new
            repaint(oldCharacterX, oldCharacterY, 40, 40);
        }
        repaint(characterX, characterY, 40, 40);
    }
}