Java - Necesito ayuda, no encuentro el error!

 
Vista:
sin imagen de perfil

Necesito ayuda, no encuentro el error!

Publicado por hacha65 (1 intervención) el 05/06/2016 06:26:48
Buenas, estoy intentando crear un pequeño programa, en el que quiero mostrar un "Sprite", simulando un personaje moverse. Para ello, he usado la clase Applet, evitándome tener que crear clases para captar eventos, y esas movidas. Solo de prueba. Tengo que decir, que el Applet posee un segundo hilo, en el que se realizan las operaciones para pintar lo deseado. El Applet, posee una instancia de otra clase tambien creada, que es la clase que hace referencia al personaje. Bien, dicho esto, el problema que presento, es el siguiente: Cuando el personaje se mueve, el rastro del pintado anterior no se borra, lo cual me tiene indignado xD. Por la consiguiente, os dejo el código. Ayúdenme porfavor *_*:

Ya de paso, si pueden darme algún consejo, lo agradecería enormemente. Salu2.

//Clase referente al objeto:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class ForeBitmapLoop implements Moveable {

byte STATE = 0;

protected int XVelocity;
protected int YVelocity;

static final int STAND_STATE = 0;
static final int DOWN_MOVEMENT_STATE = 1;

protected Applet parentContex;

protected boolean active;
protected boolean visible;
protected boolean foreground;

protected int x;
protected int y;
protected Image currentImage; // Image represent
protected Image StandImage; // Default image
protected Image[] DownMovement; // Image Fore Represent
protected int DownMovementPosition = 0; // DownMovement Current Position

protected boolean DownMovementState;
protected boolean StandImageState;

public ForeBitmapLoop(int x, int y, Image StandImage, Image[] DownMovement,
Applet parentContext) {
this.x = x;
this.y = y;
this.StandImage = StandImage;
this.DownMovement = DownMovement;
this.parentContex = parentContext;

restore();
foreground = true;
StandImageState = true;
}

public void restore() {
active = true;
visible = true;
}

public void isForegroundState(boolean foregroundState) {
foreground = foregroundState;
}

public void setState(int STATE) {
switch (STATE) {
case 0:
StandImageState = true;
DownMovementState = false;
break;
case 1:
StandImageState = false;
DownMovementState = true;
break;

}
}

public void update() {
if (active && visible) {
if (DownMovementState) {
System.out.print(StandImageState);
currentImage = DownMovement[DownMovementPosition];
if (DownMovementPosition < DownMovement.length) {
DownMovementPosition++;
if (DownMovementPosition == DownMovement.length) {
DownMovementPosition = 0;
}
}
}
updatePosition();
}
}

public void paint(Graphics g) {
if (visible) {
if (StandImageState) {
g.drawImage(StandImage, x, y, parentContex);
} else if (DownMovementState) {
g.drawImage(currentImage, x, y, parentContex);
}
}
}

public void setPosition(int x, int y) {
// Nothing here
}

public void setVelocity(int Xv, int Yv) {
XVelocity = Xv;
YVelocity = Yv;
}

public void updatePosition() {
x += XVelocity;
y += YVelocity;
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Clase referente al Applet simulador:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;

public class GameManager extends Applet implements Runnable {

private ForeBitmapLoop jugador;
private Thread hilo;
Graphics offScreen;
Image image;
private int FPS = 20;

public void init() {
setBackground(Color.BLACK);
image = createImage(bounds().width, bounds().height);
offScreen = image.getGraphics();
initPlayer();
}

public void initPlayer() {
MediaTracker t = new MediaTracker(this);
Image StandImage = Toolkit
.getDefaultToolkit()
.getImage(
"C:/Users/Administrador/Documents/Imagenes_Proyectos/Juego_Roll/24x32/Quieto_Delante.png");
Image DownMovementImage[] = new Image[2];
// Add StandImage;
t.addImage(StandImage, 0);
// Add DownMovementImage;
for (int i = 0; i < DownMovementImage.length; i++) {
DownMovementImage[i] = Toolkit.getDefaultToolkit().getImage(
"C:/Users/Administrador/Documents/Imagenes_Proyectos/16p/Suelo_"
+ (i + 1) + ".png");
t.addImage(DownMovementImage[i], 0);
}
// Loading Images
System.out.println("Loading Images...");
t.checkAll();

if (t.isErrorAny()) {
System.out.println("Error loading");
} else {
System.out.println("Loading Sucefully");
}
jugador = new ForeBitmapLoop(20, 20, StandImage, DownMovementImage,
this);
}

public void start() {
hilo = new Thread(this);
if (hilo != null) {
hilo.start();
}
}

public boolean keyDown(Event e, int key) {
switch (key) {
case Event.DOWN:
jugador.setState(ForeBitmapLoop.DOWN_MOVEMENT_STATE);
jugador.setVelocity(0, 2);
break;
}
return true;
}

public boolean keyUp(Event e, int key) {
if (key == Event.DOWN) {
jugador.setState(0);
jugador.setVelocity(0, 0);
}
return true;
}

public void updatePlayer() {
jugador.update();
}

public void paint(Graphics g) {
super.paint(g);
offScreen.setColor(Color.BLACK);
jugador.paint(offScreen);
g.drawImage(image, 0, 0, this);
}

public void update(Graphics g) {
paint(g);
}

public void run() {
while (true) {
repaint();
updatePlayer();
try {
Thread.sleep(FPS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder