I'm trying to write a renderer for a web browser using Swing and Java. Probably not a good idea, but I decided to give it a try.
Now I came to a "tricky" scheme: in order to lower CPU consumption (which was very high when I did all the rendering in paintComponent
method) I moved the rendering logic to another function and started to render to BufferedImage
. The paintComponent
now takes data from this image and paints it to the screen.
However, I have many strange bugs now, that I don't know how to fix.
1) I resize my JFrame, some handlers are triggered, written by me, that recalculate the positions and sizes. But when it's done, I see a white blank screen with nothing on it in my viewport (my "root" element, ancestor of JPanel, becomes white, maybe it is because of that scheme, I don't know).
2) If a click somewhere outside a "viewport" but inside a JFrame, then everything is OK. But when I minimize and then restore the frame - the viewport is white again. Seems like some bug, but why is it visible only after a full frame repaint?
3) Text selection in blocks (custom, implemented by me) is working fine in the "model", but to display changes in the "view" I again need to minimize and then restore the frame. I found no way to make it show immediately (it is implemented using the backgrounds of JLabels, that are in my structure). It worked just fine before that switch to BufferedImage
, but now it's buggy too.
So the question is: what have I done wrong and how to do it the right way? Please, don't advise to use some C/C++ library for manipulating graphics, I'd like to do it in Java.
UPD1: I found an important detail. The problem with "blanking" after a minimize and restore is reproducable only in one of the 3 examples. I looked for the differences, and I think I see the main one. The problem example has nested custom elements (i.e. elements that paint themselves from Buffered Image
are members of other such elements, as all of these elements extend JPanel). Maybe it was the wrong idea, and I should have stored elements only in Vectors, but do not make them all JPanels (thought, I need to draw them on screen, anyway, so I should have extended one of Swing components).
UPD2: I read here about the system repaint triggers, but I still can't figure out why the repaint that is done from the raster in memory is done OK when I move the window, partially or even fully hide it with another window, but fails after I minimize and then restore it! How is it possible?..
UPD3: Removed the packages that are not related to the topic and uploaded the code as a zip archive: http://dropmefiles.com/CLGs7. The problem example is LayoutTests
, it's a runnable class. The main class is Drawable
, this is the core. And WebDocument
is a JFrame ancestor, it does some work on frame resize and also stores some useful properties.