0

Not much to explain here, take a look at the screenshot of me resizing my frame from the top left corner to the top left of the screen (happens at all orientations). I was just wondering why this happens and whether it can be avoided / improved / optimised or not. Thanks.

enter image description here

rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • 3
    It's hard to tell *why* when there's no code, but my guess is a `repaint` request needs to be issued. Either that, or some long-running task is preventing the window from finishing its repainting. – mre Jan 10 '12 at 15:40
  • That's very true, and I understand that. I think it is all my colored rectangles being repainted on component resize. I was just wondering if there was a way to make it look nicer? Might have to take a look at optimizing my paint code.. – rtheunissen Jan 10 '12 at 15:47
  • 4
    I don't see why repainting your rectangles would have a problem. I've never seen this with Swing components so you must have a problem with your custom painting. Do you invoke `super.paintComponent(...)`? – camickr Jan 10 '12 at 16:18
  • Also, make sure you aren't performing any time-consuming tasks on the EDT. – fireshadow52 Jan 10 '12 at 17:41
  • Be certain to honor the [opacity](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props) property. – trashgod Jan 10 '12 at 18:39
  • @trashgod do you mean if I have components that are translucent I should set their opacity? Though that seems obvious, I'm not sure how I'd get translucent panels without doing so anyway. That is what you are referring to right? – rtheunissen Jan 11 '12 at 00:48
  • @camickr Let's say I have 500 rectangles, would it be more efficient to use `JLabels` instead of `JPanels` for that? Because as soon as there are many (100+) components, it starts to lag (painting being EDT). – rtheunissen Jan 11 '12 at 00:50
  • 1
    Your question is too general. I don't know what kind of custom painting you are doing with the JPanels. Also you image only show about 25 components so I don't understand where the 100+ comes from. Post an SSCCE demonstrating the problem and maybe someone will have a suggestion. – camickr Jan 11 '12 at 01:38

2 Answers2

2

Be certain to honor the opacity property. With setOpaque(true), "The component agrees to paint all of the bits contained within its rectangular bounds." Failing to do so can leave rendering artifacts similar to those shown in your question. In contrast, this example uses setOpaque(false) to indicate that not all pixels will be drawn.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

One option would be to not allow the default repaint and have an alternate representation while the window is resizing. For example, you could only show the window outline while the window is being resized and then, once the operation is complete, fill in the additional space according to your normal repaint.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
  • Any clues on how to achieve that? Could spend some time figuring it out if not. This would be ideal if it's possible. Thanks – rtheunissen Jan 10 '12 at 15:52
  • @paranoid-android You could override the `repaint` method to do something different. – fireshadow52 Jan 10 '12 at 17:35
  • @paranoid-android don't clutch straws (aka: apply random hacks) - instead try to get a clear understanding of what's happening. Most probably something is wrong with your custom code. Debugging and/or a profiler will help – kleopatra Jan 15 '12 at 11:51