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

Drawings in JPanel disappears after scrolling the scrollpane  RSS feed

 
Sebastian Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problems trying to get my jPanel to display my drawings properly after scrolling. (My drawings is done using by overriding the paint() method as the paintComponent() method does not work in my case.) My jPanel is component of my JScrollpane, and my JScrollpane is part of my jFrame.

The organization of components is: JFrame <-- JScrollPane <-- JPanel ( <-- means added to).

Situation: My drawings did appear when the frame becomes start, but when I start scrolling the scrollbar in any direction, it starts to eat up my drawing leaving only part of drawing which is not affected by scrolling.

I want my jpanel to display the drawing properly even after scrolling in both vertical and horizontal directions. I did use revalidate and repaint methods in my method for start of the JFrame.

Some Ways which I have tried:
1) I have tried adding this.revalidate() and this.repaint() method to the paint() method which did retains the image, but the jPanel keeps blinking on my screen which is trying attempt to update the drawing in a recursive manner.

2) I have tried panel.repaint() and panel.revalidate() in my paint() method, but it did not display my drawing.

3) I have read from other sources that I needed a listener to do it, but I am not sure how it works (I might have done it wrongly. Please show me the correct solution if this is the correct way).



I would appreciate and thanks for any help given in advance.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Wong wrote:My drawings is done using by overriding the paint() method as the paintComponent() method does not work in my case.


You should use the paintComponent() method and if overriding the paintComponent() method does not work then you are doing something wrong but of course without seeing the faulty paintComponent() method it is impossible to say what.
 
Sebastian Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:
Sebastian Wong wrote:My drawings is done using by overriding the paint() method as the paintComponent() method does not work in my case.


You should use the paintComponent() method and if overriding the paintComponent() method does not work then you are doing something wrong but of course without seeing the faulty paintComponent() method it is impossible to say what.


I followed your advice and used paintComponent() method and also segment out my JPanel code and jFrame code which is actually together. I managed to see my drawn objects properly now when I start to scroll the jScrollPane, but it is not displayed at the start of the frame (I have to scroll before my drawings appear).

However, my JLabels with picture icons inside my jPanel begins to distort the images (like small boxes flying everywhere, somehow repainting not done properly) when I start to scroll my jScrollPane.

I actually have a method which adds the my JLabels picture icons into position, I am not sure it is the correct, but it did displayed properly and work initially.

Is there a way to solve these problems? I might have a problem pasting my code here because it is very long, kind of difficult to segment out the portion that is affected as all of them is interlink.

 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need any listener to do scrolling. Scrolling happens automatically. Make sure you override the getPreferredSize() method of the panel where you do custom painting.

Post a SSCCE that demonstrates your problem, if you need more help.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of what you are describing I cannot help with without seeing the detail of your paintComponent() method and how the JPanel is managed within the JScrollPane. Other contributors to this forum may be better able to help.

It seems to me that you are trying to add JLabels to the JPanel you are drawing into but you might do better to use a renderer approach where the images are not added to the JPanel but just painted in the paintComponent() method. I do this on some genealogy diagrams using a JLabel as the renderer. I keep a collection of the information and the (x,y) position of the point at which I want to display the information then use a single JLabel to render each item in the collection each time the paintComponent() method is invoked. Essentially the same as is done with JTable renderers.
 
Sebastian Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted my code here as requested. This code would display my Jlabels at start of the JFrame, but not the lines drawn by PaintComponent. The point when I start scrolling my Jpanel on my Jscrollpane, it would distort my jLabel images (Like bits and pieces of Image run all over the place), but the lines drawn would appear nicely and not distorted.

I have done the methods in this way because it looks like a easy way to do it to me, but I am not sure whether this is the correct way to do it.

I have tried to shorten the code I pasted because it is really very long, and I have a hard time making it concise in nice and short format. I Hope it is not too long for a single post.

Am I doing it the right way? I totally have no idea why my JLabel pictures are distorted.



JFrame File which make my (MapPanel) Jpanel Appear. (Found this method of doing from other websites)
 
Sebastian Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:You don't need any listener to do scrolling. Scrolling happens automatically. Make sure you override the getPreferredSize() method of the panel where you do custom painting.

Post a SSCCE that demonstrates your problem, if you need more help.


I have tried out your advice, but I am not sure whether am I doing it correctly.

I have overrided the getPreferredSize() method of my panel.

However, the panel keeps blinking non-stop if override the method in this way. (I tried it in this way, just to see whether dynamic size for panel is working as I need it later.)


And If I fixed the width and height for the getPreferredSize() Method, I would get back the same trailing image distortions (boxes/Images flying around).



 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't posted a SSCCE so I can't help. The code you posted is not compileable. The idea of a SSCCE is to demonstrate your problem. Your problem is with painting, so you need to isolate the painting part an post a complete program that shows the problem. Most time while you try to isolate the code you will find the problem.

For more help on custom painting read the Swing tutorial on Custom Painting for working examples. The idea is to start with something simple that works and then change the painting to be more complicated until it stops working. Then you know what you have changed which will give you a better idea on what to fix.
 
Sebastian Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that. I do not really know how to post my code in SSCCE format. I totally have no idea how SSCCE format looks like and what it is.

I manage to solve the problem using the (Graphics) g.clearRect() method.

The problem is caused by when I scroll my jpanel on my jScrollpane it does not clear the image distortion caused by the repaint() method.

It seems that when I start scrolling my scrollpane, it would keep on repainting and redrawing without really clearing the panel and keeps on drawing on the panel.

I used the g.clearRect() to clear my entire panel of the drawing.

Thank you for your time and the help given.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Wong wrote: I do not really know how to post my code in SSCCE format.


It is not a 'format'; please follow --> SSCCE
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!