PrintJob has been giving me a couple of problems lately, one of them was proper placement of the objects on page. This post discusses the fix

NOTE: I have not been able to confirm the behavior described here, may be it was just something i was doing wrong. However i am posting it here so that if it’s really a problem, this fix might help

Say, there’s a Sprite/MovieClip of a standard page size (say A4), say it’s called ‘p’, naturally you would print it as:

var pj:PrintJob = new PrintJob();
if(pj.start())
{
pj.addPage(p); // p is the Sprite/MovieClip of full page size
pj.send();
}

Say you had an rectangle (the only object in the Sprite) right in the middle of the sprite, since the sprite’s size equals the page size, you’ll expect the rectangle to be printed in the middle of the page, right? Surprise! it gets printed in the top left corner.

What happened here is that actionscript aligned the first drawn pixel (the top left corner of the rectangle) to the top left corner of the page. To workaround, you may draw an invisible rectangle of the full page size to the Sprite so that everything gets printed neatly. here’s what you would do:

var pj:PrintJob = new PrintJob();
if(pj.start())
{
p.graphics.lineStyle(1, 0xFFFFFF, 0);
p.graphics.drawRect(0, 0, PageWidth, PageHeight);

pj.addPage(p); // p is the Sprite/MovieClip of full page size
pj.send();
}

I am working on an ActionScript 3.0 module in my day job, and although I am new to ActionScript, I rarely touch the documentation (yeah yeah, I know I should do it more often =) ), most of the time it works, may be because I was on Java before.

Last day, I needed to find out the stage’s height somewhere, so I just typed

stage.h

and thanks to intelli-sense, it quickly told me that it was

stage.height

i was looking for. But something strange happened and i was not getting the value i expected. After a couple of hours of hair pulling, it dawned on me that I should be using

stage.stageHeight

instead of

stage.height

to get stage’s height.

I can’t say whether Adobe/Macromedia’s decision to use

stage.stageHeight

and

stage.stageWidth

instead of

stage.height

and

stage.width

was right, but the property name is not abundantly clear in what it is going to return.

Technorati Tags: , ,

Currently I am working on a project that has been under development for almost 6 years now, so it contains a lot code written by a lot of people.

A couple of days ago, I was modifying a module in which We had a function like:

public ResponseObject perfromTask(/* params */)  {

  ResponseObject response;

  try {
  // — My Changes Start Here –

  //….

  //some statements

  //….

  // all done, return the object

  return response; // this response object is perfectly valid

  // — My Changes End Here –

  }

  catch(SomeException ex)  {  // handle exception

  }  finally {

  // some resource cleaning

  return response; // BOOM!!!!! I didn’t notice this statement at first

  }

}

and there was another method that was calling the above method like this:

public void callerMethod(/* params */) {

  // irrelevant code

  ResponseObject response = performTask( /* params */); // the response object received here was always null

  // irrelevant code

}

But strangely, I was always getting NullPointerException when the callerMethod tried to get response from perfromTask. So I placed a breakpoint at the line where I was returning the response in the performTask (the line that has a “this response is perfectly valid” comment) and another just after the line where performTask is called in the callerMethod. And when it interrupted the execution inside perfromTask, I could see that the response object was the one that should have been expected, so I just “resume” to jump to the next break point and OUCH! the response object received in the caller is indeed null.

After single stepping through performTask, I noticed that finally block had a return statement itself, I don’t know why it was returning null (since it had the same reference as the try block) but it, while at the same time being surprising, was quiet annoying because the code ran into an error when in fact there was no error (in the changes I made I mean). Anyway, removing return statement from finally block and making sure that try block was returning in every case it was supposed to return solved the problem.

I never thought of returning inside finally block before, but now that I have seen it, I know it is something mysterious and might get you into problems. Don’t know what java specs have to say about it.

Close
E-mail It