Kieran France Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He spends his time working on the the JPedal library and our internal test suite..

Java’s Graphics2D object does not verify any input – this is your job!

1 min read

This blog post was inspired by a problem I looked at this week. A customer was reporting issues with some code which turned out to be due to some errors in what they were inputting on the Graphics2D settings. The code was doing exactly what it was told to do. Software is like that – it does not do what you want it to do, only what you tell it to…

“On two occasions I have been asked, “Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?” … I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.”
—Charles Babbage, 1864, Passages from the Life of a Philosopher

 

Many times it can be all to easy to make assumptions about code and not think any further about it. This can often lead to incorrect inputs (Garbage In) and incorrect output (Garbage Out). In my opinion this is often one of the most common causes of reported “issues” when using software libraries, especially amongst new comers to a library or project. The important thing to remember is that the code is stupid. It does exactly what it is told. This isn’t even a new concept either, it can be dated back to the 1864 and maybe even earlier.

For quite some time we have had a method that will draw pdf pages straight on to a Graphics2d object to be displayed elsewhere. This method will draw the designated page on to the graphics2d object passed in at the specified scaling value.

It should be noted about now that the renderToG2 method is stupid, it does exactly what it is told. The method will attempt to draw the pdf page at the given scaling regardless of the size of the graphics2D object. This means if the graphics2d object has a smaller drawable area than the page would cover you will only get a partial page drawn.

This also applies for pages that are rotated within the pdf. When a page is rotated to 90 or 270 degrees the pages height and width values also need to be swapped to take this into account (as the pdf height and width are the pages height and width before any rotation).

So if using these values to determine the size of the graphics2D you need to remember these values will need to be swapped on rotated pages otherwise you will find sections of the page could be missing.

Kieran France Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He spends his time working on the the JPedal library and our internal test suite..

Pros and Cons of Bitbucket Pipelines

Recently I have been looking at our current test suite looking for ways to improve our own tests. As we use Bitbucket we have...
Kieran France
3 min read

WTF: What’s The Future and Why It’s Up To…

If you want to know what is going to happen in the future, the person to ask is Tim O’Reilly. As well as being...
Bethan Palmer
2 min read

Leave a Reply

Your email address will not be published. Required fields are marked *