Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java2D drawRect()  RSS feed

 
Tian Zhang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I am just getting started with Java2D (more accurately, "restarted" after an 8 year break from Java ) and have hit an interesting behaviour when playing with Java2D. Here is the code in question:



The code above should draw an rectangle when the Render is added to a JFrame, however ends up drawing a square with with the bottom edge misaligned (see attachment).

The issue goes away if I force BasicStroke(1.1f), however feel that this is a bit of a hack.

Why does the miss-alignment happen and what is the "best practise fix" for this?


Thanks!
Tian
Screenshot.jpg
[Thumbnail for Screenshot.jpg]
Screenshot
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Tian,

I do not see any mis-alignment on my screen. Of course, 10 by 10 is a bit small for my eyes to see
it sharp, but as said, I see nothing wrong. And when I enlarge the rectangle (say 100 x 100 or
200 x 200), and/or use a BasicStroke(7), I see nothing wrong.

If you enlarge your rectangle as well, do you still see this effect?

Greetz,
Piet
 
Tian Zhang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Piet,

The size of the rectangle seems to be irrelevant, and the misalignment does not happen with stroke width > 1. Furthermore the misalignment goes away after resizing the JFrame too...

I've distilled the code down to the following:



Strangely enough, the code renders fine with jdk1.7.0_67 (see attachment) and the misalignment only seems to happen with jdk1.8.0_20 (see attachment).

(You may need to zoom into the images to see the pixel that's sticking out)

Tian


jdk1.7.0.png
[Thumbnail for jdk1.7.0.png]
Using 1.7.0_67
jdk1.8.0.png
[Thumbnail for jdk1.8.0.png]
Using 1.8.0_20
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Tian,

indeed, I tested it on jdk1.7.0_25.
On my pc, I have Win XP 32bit, so at the moment I cannot test it on java8.
I'll have a look later this evening, using my wife's laptop.

Greetz,
Piet
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Tian,

well, I tried it on 1.8_20, but I still see nothing strange here. I ran a loop,
creating lots of rectangles, but they were all perfect. I also tried several
StrokeWidths, again nothing abnormal.

Greetz,
Piet

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!