×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    4

    Loss of fraction on Graphics.drawImage()

    Hi there!
    I try to move a game object like this:
    Code:
    public void Move(double speed) {
            double dy = speed*Math.cos(facing*Math.PI/180);
            double dx = speed*Math.sin(facing*Math.PI/180);
            PosX += dx;
            PosY += dy;
        }
    but when i try to draw it on a canvas I must cast the coordinates to int so i lose a fraction:
    Code:
    g.drawImage(frames.getImage(count), (int)PosX, (int)PosY, Graphics.TOP | Graphics.LEFT);
    And the game object does not behave normally.

    Can someone give an advice on that? Is there any workaround?

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: Loss of fraction on Graphics.drawImage()

    This piece of code is very typical for decades, and it usually does not cause any problems in itself.
    What kind of abnormal behavior do you experience?
    If speed is small, the animation becomes a little jumpy, but avoiding that would require (emulating) sub-pixel accuracy, which is not really feasible for Java ME.

  3. #3
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    685

    Re: Loss of fraction on Graphics.drawImage()

    Ivan, you need to provide us with more information and code if you really want help.

    Screen coordinate is done in pixels there is no fraction in pixel, either a pixel is lid with a color or its not.
    i suggest you might print to the console the PosX and PosY values before your drawImage call and see what they are.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Posts
    4

    Re: Loss of fraction on Graphics.drawImage()

    I asked around for a solution and this is what people say:
    I have to implement my own coordinate system, where i would be able to make continuous moves that can take less then 1 pixel. Then during the frame paint project it on a real matrix of LCD.
    Considering complexity of this stuff and the resources of the platform I'd rather give up on this and let the units to move UP|DOWN|LEFT|RIGHT and dioganally UP_LEFT etc.

  5. #5
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    685

    Re: Loss of fraction on Graphics.drawImage()

    you already have your own "coordinate system" because you keep your object position in PosX, PosY which are doubles (at least so it seems to me with out seeing the entire code)
    which is why i told you before that if you want good help you have to share more code, do more debugging, log printing etc.

  6. #6
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: Loss of fraction on Graphics.drawImage()

    It may also help describing the actual problem with visuals/animation. Too slow? Too fast? Jumps too many pixels a time?

Similar Threads

  1. Graphics.drawImage() vs DirectGraphics.drawImage()
    By owcs in forum Mobile Java Media (Graphics & Sounds)
    Replies: 11
    Last Post: 2003-11-18, 14:44
  2. Graphics.drawImage and DirectGraphics.drawPixels
    By mrgasjs in forum Mobile Java General
    Replies: 0
    Last Post: 2002-10-23, 01:26
  3. Graphics.drawImage and DirectGraphics.drawPixels
    By mrgasjs in forum Mobile Java General
    Replies: 1
    Last Post: 2002-09-25, 08:49
  4. Replies: 0
    Last Post: 2002-08-31, 01:02
  5. bug in Series 40 emulator for Graphics.drawImage?
    By Kiigan in forum Mobile Java Tools & SDKs
    Replies: 2
    Last Post: 2002-07-23, 06:18

Posting Permissions

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