×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Translating graphics

    Hey everybody

    Im creating a back-gammon game and my problem is that I would like to move a piece on the board.
    I can draw a piece somewhere else than the original position but how do I delete the old drawing at the old position ????

    Ive looked at the translate function but it seems to me that this function translate the origon for the entire graphics content - that the hole board.

    Likewise Ive read about a function called "repaint" but my compiler dosnt support this function - is this general ???

    Any help is appreciated

    CVP

  2. #2
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    repaint() is a call to the paint() method of a Canvas class. So it is not called within the paint method, but rather outside it,
    ie. A player makes a move, and you update the positions of the pieces and call repaint() which will in turn call paint().

    Consider using sprites or TiledLayers. You can very easily move stuff around using move() or setPosition().

    Gluck.

    [edit]
    http://developers.sun.com/techtopics...articles/game/
    [/edit]

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    16
    I dont know must about TileLayers but managed to get the repaint() to draw a new figure but I cant delete the old figure.
    The thing is I want to move a cursor and Ive implemented a pressedkey and a movecorsur. It moved the the cursor but the old cursor remains........

    Likewise ive looked at GameCanvas but how do i parse args i the GameCanvas const

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    the code

    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
    {
    private Game the_game;
    private final GamePlay gamePlay;

    //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;

    int up=0;
    int down=0;

    private Command BACK = new Command("Back",Command.BACK,0);
    private Command Roll = new Command("Roll",Command.OK,1);
    private Display display;
    private Displayable currentDisplayable;

    private static final int WHITE = 0x00FFFFFF;
    private static final int RED = 0x00FF0000;

    public Board(Game the_game)
    {
    // There must be a call to super because this is the Canvas.class no arg constructor
    //super();

    addCommand(BACK);
    //addCommand(EXIT);
    addCommand(Roll);
    setCommandListener(this);
    // This lines parses the instance of Game to the var the_game
    // The instance of Game, instance_Game, is parsed to Board() - no arg constr - in Game.startApp()
    this.the_game= the_game;
    //die1= new Die(6);
    //die2= new Die(6);
    gamePlay = new GamePlay();

    try
    {
    image = Image.createImage("/BG_board.png");
    }
    catch (IOException ioe)
    {
    System.out.println(ioe.getMessage());
    ioe.printStackTrace();
    }

    NewGame = true;
    }

    public void commandAction(Command c, Displayable d)
    {

    if(c==BACK)
    {
    the_game.display.setCurrent(the_game.playMenu);
    }

    else if(c==Roll)
    {
    // start the game med thread
    init();
    //die1.roll();
    //die2.roll();

    }
    }

    private void init()
    {
    gamePlay.init();
    NewGame= true;
    // Ligeledes bestemme hvem der starter

    }

    private void CursorMove(int vandret, int lodret)// vars senere
    {
    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(cursor_x,cursor_y,12,12);


    }

    private void SelectPiece(/* arg til brik og nuværende pos*/)
    {
    /*brik.selected++;
    */

    }

    private void doPlayerMove()
    {
    //int maxX = die1.getValue()+die2.getValue();

    }

    // this func is used to clear the Canvas
    public void clear (Graphics g)
    {
    int clipX = g.getClipX();
    int clipY = g.getClipY();
    int clipH = g.getClipHeight();
    int clipW = g.getClipWidth();

    int color= g.getColor();
    g.setColor(WHITE);
    g.fillRect(clipX,clipY,clipW,clipH);

    g.setColor(color);
    }

    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;

    //initial cursor pos
    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);
    }

    void drawBoard(Graphics g)
    {
    g.drawImage(image,0,0,Graphics.TOP|Graphics.LEFT);

    hvid = new Piece(0,0,1,g);
    sort = new Piece(0,0,0,g);

    down=0;
    up=174;

    // De sorte brikker
    g.setColor(0);
    for(int i=0;i<5;i++)
    {
    down = i*10;
    sort.drawPiece(0,down);
    }
    down =0;

    for (int i=0;i<3;i++)
    {
    up = up - 10;
    sort.drawPiece(48,up);
    }
    up=174;

    for (int i=0;i<5;i++)
    {
    up=up-10;
    sort.drawPiece(84,up);

    }
    up=174;

    for(int i=0;i<2;i++)
    {
    down=i*9;
    sort.drawPiece(144,down);
    }

    // De hvide brikker
    g.setColor(255,255,255);
    for (int i=0;i<5;i++)
    {
    up = up - 10;
    hvid.drawPiece(0, up);
    }
    up=174;

    for (int i=0;i<3;i++)
    {
    down = i*9;
    hvid.drawPiece(48,down);
    }
    down=0;

    for(int i=0;i<5;i++)
    {
    down=i*10;
    hvid.drawPiece(84,down);
    }
    down=0;

    for(int i=0;i<2;i++)
    {
    up= up-10;
    hvid.drawPiece(144, up);
    }

    }

    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;
    }
    repaint();
    }


    }

  5. #5
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    When you rely solely on the graphics paint without the use of layers, there is no way to erase whatever is underneath because it is all considered as one big layer. You will either have to paint over the old position before painting the piece's new position, or paint the entire board again.

    Though that will work, many games use layers. Sprite and TiledLayer are layer classes, meaning you will not have to repaint the entire area over. (ie. one layer for your board and another for your pieces) Both have animation capabilities.

    If you use Sprites for your pieces, you can use methods move() or setPostion() to move them without having to bother about where they last were.

    TiledLayers can also be moved around with the methods above. TiledLayers is one way to minimize code size. Say for example a checkers board, you only load one image of a black tile and a red tile into your game and use a tiled layer to paint your entire board using the two images.

    If you want to find out more, check out the link i gave you in previous post, or do a search.

    ps: do use the [code] tags next time you include code. Makes reading easier.

    Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×