×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Apr 2003
    Location
    Bangalore
    Posts
    117

    Problem with Translate mathod

    Hello...

    I am try to develop some game where I need to move the background Image Please help me
    I have written this code but this is not working properly...
    I mean I am not getting any errors but the backgroud Image is not moving as I want it to move.

    please help me....

    Thankz in Advance
    Chandu



    ==============================================
    import javax.microedition.lcdui.*;



    public class MyCanvas extends javax.microedition.lcdui.Canvas implements Runnable {
    Image offImage,bgImage; // An Image for double buffering.
    Graphics offGraphics; // A global variable for graphics.
    int w,h; //Screen-width and height
    Thread thread; // The Thread
    int blackCol= 0x00000000;
    int whiteCol= 0x00ffffff;
    boolean forceTerminate=false;
    static int X1,Y1;

    public MyCanvas() {
    w = 120;
    h = 120;
    offImage = Image.createImage(150,150); // Prepare image for double buffer.
    offGraphics = offImage.getGraphics(); // Prepare graphics for double buffer.
    offGraphics.setClip(0, 0, w, h); // Set clipping area.

    }

    protected void paint(Graphics g) {
    g.drawImage(offImage,0,0,20);
    g.translate(X1,Y1);
    }

    public void doStart() {
    thread = new Thread(this);
    // Prepare the thraed...
    thread.start();
    // Go...
    }


    public void run(){
    loadImagies();
    while(!forceTerminate) {
    //Repeat for ever
    offGraphics.drawImage(bgImage,0,0,20);
    repaint();
    serviceRepaints();
    //System.out.println("XXX");
    }




    }
    void loadImagies(){
    try{
    bgImage=Image.createImage("/bgImage.png");
    }catch(Exception ex){System.out.println("Error while loading the Image"+ex);}
    }

    ///////////////////////////////////////////////////////////////////////
    public void keyPressed(int keyCode)
    {
    int action = getGameAction(keyCode); // Store the value for the key pressed.
    if(action==LEFT || keyCode==KEY_NUM4) //Left Arrow ( move curser to left )
    {
    X1=X1-50;
    // offGraphics.translate(X1,Y1);
    System.out.println("X1 --->"+X1);
    }
    else if(action==RIGHT || keyCode==KEY_NUM6)
    //Right Arrow ( move curser to right )
    {
    X1=X1+50;
    //offGraphics.translate(X1,Y1);
    System.out.println("X1 --->"+X1);
    }
    else if(action==UP || keyCode==KEY_NUM2)
    //Up Arrow ( move curser to Up)
    {
    Y1=Y1-50;
    // offGraphics.translate(X1,Y1);
    System.out.println("Y1 --->"+Y1);
    }
    else if(action==DOWN || keyCode==KEY_NUM8)
    //Down Arrow ( move curser to Down )
    {
    Y1=Y1+50;
    //offGraphics.translate(X1,Y1);
    System.out.println("Y1 --->"+Y1);
    }
    repaint();
    serviceRepaints();
    }



    }

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Hello,

    as I understand, the method Graphics.translate(...) only translates the coordinate-system of the Graphics-Context, not the content of the screen itself.

    To do this, you have to move the anchorpoint of the image relative to the origin of the Graphics-Context.

    In your example this would look like this:

    Code:
    protected void paint(Graphics g) {
    g.drawImage(offImage,anchorX,anchorY,20);
    g.translate(X1,Y1);
    }
    
    public void run(){
    loadImagies(); 
    while(!forceTerminate) {
    // NEW: move the anchorpoints in the desired direction
    // HERE: scrolling diagonal from topleft to downright
    anchorX++; anchorY++;
    //Repeat for ever
    repaint();
    serviceRepaints();
    //System.out.println("XXX");
    }
    I recommend the Helicopter-Example from the Nokia-Documents-Section, its a good start.


    I hope I could help,

    StingerAJ

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    When you start the paint() method you get a fresh graphics context with the coordinate origin at (0,0) every time. You're drawing the image there (at 0,0), and then translating the origin. But the next time paint() is called the origin is back at (0,0). If you want to use translate like this you should do the translating before you draw the image (and each time by a different value).

    shmoove

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Thx for the info, shmoove.

    Greetings,

    StingerAJ

Posting Permissions

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