drawText issue?

I have been trying to use a numeric field in the Y and X coordinates and this seems to work fine in the simulator, but seems to not work and any of the edge units.  Has anyone else tried this and if so were you able to get it to work?

Thanks

Ronnie

  •  If you are trying to display a Number, add .toString() to it.

  •        calcheightdec = height * .025;

            calcheightgo = Math.floor(calcheightdec);

            calcwidthdec = width * .175; 

            calcwidthgo = Math.floor(calcwidthdec);

            dc.drawText((width / 2) + calcwidthgo.toString(), (height / 2) - calcheightgo.toString(), Gfx.FONT_TINY, "F", Gfx.TEXT_JUSTIFY_LEFT);

    I am getting a IQ error on the simulator.

    Ronnie       

  • Why are you subtracting a string from a number for x,y?

    x,y need to be numeric. 

    Text needs to be string. 

  •         calcheightdec = height * .025;

            calcheightgo = Math.floor(calcheightdec);

            calcheight = (height / 2) - calcheightgo;

            calcheight = calcheight.format(valueFormat).toNumber();

            calcwidthdec = width * .175; 

            calcwidthgo = Math.floor(calcwidthdec);

             calcwidth = (width / 2) + calcwidthgo;

            calcwidth = calcwidth.format(valueFormat).toNumber();

            dc.drawText(calcwidth, calcheight, Gfx.FONT_TINY, "F", Gfx.TEXT_JUSTIFY_LEFT);

    The above code works in the simulator, but not on my 830.  If I put .toStirng() on the dc.drawtext it gives an IQ error on the simulator.

    Also if I just set calcwidth and calcheight to a numeric value it works in the simulator, but not on the 830.  If I just put the value on the dc.drawtext inplace of the field name it works in the simulator and on the 830.  Is this a bug?

    Ronnie 

  • I pasted your code into a test project and tested it in the simulator. It fails, just like I'd expect. Here is my onUpdate function:

    function onUpdate(dc) {
        dc.setColor(Graphics.COLOR_RED, Graphics.COLOR_YELLOW);
        dc.clear();
            
        var height = dc.getHeight();
        var width = dc.getWidth();
    
    	var calcheightdec = height * .025;
        var calcheightgo = Math.floor(calcheightdec);
        var calcwidthdec = width * .175; 
        var calcwidthgo = Math.floor(calcwidthdec);
    
        dc.setColor(Graphics.COLOR_GREEN, Graphics.COLOR_TRANSPARENT);
        dc.drawText((width / 2) + calcwidthgo.toString(), (height / 2) - calcheightgo.toString(), Gfx.FONT_TINY, "F", Gfx.TEXT_JUSTIFY_LEFT);
    }

    In the simulator, I get this error message when I run it:

    Error: Unhandled Exception
    Exception: UnexpectedTypeException: Expected Number/Float/Long/Double, given Number/String
    Stack:
      - onUpdate() at /Users/vitek/Desktop/eclipse-3.2.0/eclipse-workspace/Bitmap/source/BitmapApp.mc:68 0x100001a2

    This is happening because he expression (width / 2) + calcwidthgo.toString() is trying to add a Number (width / 2) and a String (calcwidthgo.toString()). This is not legal.

    If you remove the unnecessary conversions to String, it works:

        dc.drawText((width / 2) + calcwidthgo, (height / 2) - calcheightgo, Gfx.FONT_TINY, "F", Gfx.TEXT_JUSTIFY_LEFT);
    

    The last bit of code you posted should also work, but it is a really roundabout way of doing this. Comments follow:

        var height = dc.getHeight();
        var width = dc.getWidth();
    
        // `height` is a Number (a 32-bit integral value). multiplying with a
        // Float (.025 is a 32-bit floating point value) gives you a Float
    	var calcheightdec = height * .025;
    	
    	// this converts the Float back to a Number. it could be done more
    	// simply by writing `calcheightdec.toNumber()`, but that shouldn't
    	// be necessary
        var calcheightgo = Math.floor(calcheightdec);
    
        // `height` is a Number, `2` is a Number, and `calcheightgo` is a
        // Number. Number / Number => Number, and Number - Number => Number
        // so you are going to get a Number.
        var calcheight = (height / 2) - calcheightgo;
        
        // `calcheight.format("%d")` converts the Number to a String and
        // `result.toNumber()` converts that String back to a Number. why?
        // this is completely unnecessary.
        calcheight = calcheight.format("%d").toNumber();
    

  • Yes my second post didn't have the toString and it worked in the simulator.  My issue is on the device the x and y values do not have the correct results.

  • Are you using dc.getHeight()/dc.getWidth() or are you using System.getDeviceSettings().screenHeight/System.getDeviceSettings().screenWidth?

    on touch screen edge devices, they are different.  Using the dc calls gives you the size of the dc, while the System calls, the full screen, and on some devices, there is a "button bar" on the screen, so the dc isn't as tall.