0

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.

Alex Popov
  • 65
  • 7
  • Without seeing your code how can we tell you what is wrong with it? – takendarkk Mar 13 '18 at 17:58
  • 3
    We cannot give suggestions about what you have done wrong nor how to fix them until you show us what you have actually done. Please provide a [mcve] and we can help from there. – Code-Apprentice Mar 13 '18 at 17:58
  • There is very large code base, should I cut everyting down, or is it simpler to provide the package (6-7 java files) "as is"? – Alex Popov Mar 13 '18 at 18:03
  • If the code is large, the OP should write a much smaller version of the code, say exactly one test case, that still reproduces the problem. If that can be done we might be able to locate the problem. – markspace Mar 13 '18 at 18:11
  • Do look at `https://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint` – Oleg Cherednik Mar 13 '18 at 18:11
  • You could write an example of your ui, that just uses a buffered image. That should be much less than the full webbrowser code base. – matt Mar 13 '18 at 18:13
  • It sounds like you just don't understand how GUIs work in general. This isn't even really a Java specific problem, really (maybe literally some of the classes you are using are, but the problem is larger than Java). I think you should spend your time reading about rendering threads (Java's is called the EventDispatchThread). Then the thing I would tell you to do is to start reading the documentation on the Components you're using. Rule of thumb: if it isnt drawing to the screen, do it out of the EDT. If it is drawing things, it must be done in the EDT (not just a rule of thumb, it is a rule). – searchengine27 Mar 13 '18 at 18:14
  • >I think you should spend your time reading about rendering threads (Java's is called the EventDispatchThread). Yes, I poorly understand the mechanics of the UI in Java or anywhere else. But what seems very annoying for me is that I have very, very little control of what is happening on the screen of my application. I came to Java from web and JS, and there things are so much simpler when you are drawing something... "What you write in code is what you get". – Alex Popov Mar 13 '18 at 18:22
  • What you write in code is what you get in Swing too, but you're probably doing something wrong, like you've overridden `paintComponent` incorrectly, even given the fact that you're ostensibly just painting an image in that method. Also, if you're doing a lot of custom painting, you should read this document: http://www.oracle.com/technetwork/java/painting-140037.html. – Radiodef Mar 13 '18 at 18:25
  • Just sit, try to make a small example using a bufferedimage that reproduces the problems you're seeing. If you can do that, then start a new question. Nobody will be able to look at this question in it's current state and derive anything useful from it. – matt Mar 13 '18 at 18:30
  • >What you write in code is what you get in Swing too A white background color of the container with no children displayed after changing dimensions of its parent container and itself, or after a window minimize is also "What you write in code is what you get"? :D – Alex Popov Mar 13 '18 at 18:30
  • What you're seeing is what your code does. If it does that, then there's a problem with it. You're probably doing something like painting conditionally, or you just have a bug which is making your images white. I and the other Swing users here have written a lot of stuff like this, and we just don't really know what to tell you until you can show us a code example which reproduces the issue so we can look through it. – Radiodef Mar 13 '18 at 18:38
  • 1
    We'd have to see your code before we could answer that. Right now you could be making any mistake at all, including a simple programming error not related to Swing. – markspace Mar 13 '18 at 18:38

1 Answers1

0

Oh, sorry for your time, I found the cause of at least 2 of 3 errors. It's really not Swing related (though it was hidden by Swing until I minimized the window). I had calls to removeAll() in many places in my code, and it was the reason why all my children were being deleted. Sorry again, it's just lack of attention from me.

Alex Popov
  • 65
  • 7