Repaint i j2me - kan ikke slette gamel grafik
Hej AlleVi har følgende problem:
Vi vil gerne tegne en cursor, string="X", og dernæst flytte denne. Men vi kan sagtens tegne og flytte, dvs tegne en mere, men kan ikke slette den gamle cursor.
Vores kode ser i korte træk sådan ud:
package BackG;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.Image;
import java.io.IOException;
public class Board extends Canvas implements CommandListener
{
//Die die1, die2;
boolean GameOver;
boolean NewGame;
private Piece hvid;
private Piece sort;
Graphics g;
public Image image;
//2-D koord til kursoren
int cursor_x;
int cursor_y;
private static final int WHITE = 0x00FFFFFF;
private static final int RED = 0x00FF0000;
private void CursorMove(int vandret, int lodret)
{
int old_posX= cursor_x;
int old_posY= cursor_y;
cursor_x = cursor_x + 12*vandret;
cursor_y = cursor_y+10*lodret;
//update(g);
repaint();
}
public void paint(Graphics g)
{
//clear(g); må ikke kaldes her da repaint skal bruges
// istedet bruges paintGame()
if(gamePlay.GameOver)
{
//paint the Game Over screen
}
else
{
paintGame(g);
}
}
void paintGame(Graphics g)
{
if(NewGame==true)
{
clear(g);
drawBoard(g);
NewGame=false;
cursor_x=3;
cursor_y=72;
}
drawCursor(g, cursor_x, cursor_y);
}
void drawCursor(Graphics g, int x_pos, int y_pos )
{
g.setColor(255,255,255);
g.drawString("x", x_pos, y_pos, 0);
}
void update(Graphics g)
{
paint(g);
}
protected void keyPressed(int keyCode)
{
int action = getGameAction(keyCode);
switch(action)
{
case FIRE:
//Selected ();
break;
case RIGHT:
CursorMove(1,0);
break;
case LEFT:
CursorMove(-1,0);
break;
case UP:
CursorMove(0,1);
break;
case DOWN:
CursorMove(0,-1);
break;
}
}
}