Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unusual behaviour of subclass of JPanel

 
parvez ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.input_panel background is not set
2.while resizing window sometimes random lines are drawn on the input panel but are not written
either to the database or the file.
3.on starting the paint on the input panel an image of the JMenuBar is created on the input panel.

N.B. output_panel does not show these problems before paintComponent() is overridden, but does oven overriding is done.




thanks for any help
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your overridden paintComponent method should make a call to super.paintComponent. That will fix some of the problems - possibly all of them.
 
parvez ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I tried that too earlier. The thing is, my input panel is supposed to paint a curve through the points
where i drag my mouse. And those curves must persist for the whole session.

You are right about super.paintComponent(), it does remove those problems but my paintings don't
persist then, instead only the last line drawn remains.

Where am i going wrong??
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where am i going wrong??


It's not easy to say since we can't compile and run your code. I suggest that you create a much smaller program that just demonstrates an essential part of your program that is not working right, code that is compilable and runnable by us without need for outside classes or resources (such as a database), and this way we can better try to work with you and help you solve your problem.

Oh, and one other thing, you may wish to create an ArrayList<java.awt.Point>, and use your MouseListener to save points to this ArrayList. Then in the paintComponent method, you would draw your curve by iterating through the ArrayList.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach is to do the drawing "in memory". Create a BufferedImage the same size as the panel, and draw the background on it. Draw the curve into that. Then, in paintComponent, just copy that image onto the panel. That will usually give you a smoother effect than redrawing the whole curve every time.
 
Rob Camick
Ranch Hand
Posts: 2539
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Examples of the two approaches suggested by Pete and Matthew can be found in Custom Painting Approaches.
 
parvez ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the suggestions.
I read through the methods, and though the will solve some of my problems but they add a few too.

As it seems to me, they will add additional overhead.But my application needs to be efficient as the user needs the see the
handwritten characters as he/she writes them and the document can be quite lengthy.
Repainting all the points from the beginning ,is required by both the processes.

The problem is arising due to overriding paintComponent().

Is there any other way in swing,like the one done in awt where overriding update() causes incremental painting??
Somewhere it said if super.paintComponent() is not called,then the application must take care of the functions like
setBackground(), etc. How do I do that??

greatly appreciate all of your time and effort.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parvez ali wrote:
As it seems to me, they will add additional overhead.But my application needs to be efficient as the user needs the see the
handwritten characters as he/she writes them and the document can be quite lengthy.
Repainting all the points from the beginning ,is required by both the processes.

The problem is arising due to overriding paintComponent().

Is there any other way in swing,like the one done in awt where overriding update() causes incremental painting??
Somewhere it said if super.paintComponent() is not called,then the application must take care of the functions like
setBackground(), etc. How do I do that??

greatly appreciate all of your time and effort.


Matthew and Rob both referred to solutions for this -- use a BufferedImage and draw to it. Shoot you can mix the solutions where any background image is placed in a BufferedImage, and any animation in the paintComponent (well, the BufferedImage is painted in the paintComponent too). Also, you will need to look at the overloads for the repaint method to limit the rectangle that requires repainting.
 
parvez ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends, I finally came to the conclusion that there is no other way
than to redraw the whole set of pixel each time, like you all suggested me.

But since i was already using a database,i decided to use the data stored over there
instead of creating arraylists,bufferedimage etc.
The overhead is hardly noticeable.

So,thanks everyboby.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parvez ali wrote:Hello friends, I finally came to the conclusion that there is no other way
than to redraw the whole set of pixel each time, like you all suggested me.

I thought that we were clear that we aren't suggesting this. If any of our posts are confusing, please ask for clarification.

But since i was already using a database,i decided to use the data stored over there
instead of creating arraylists,bufferedimage etc.
The overhead is hardly noticeable.

Just be sure that all database accessing is not done on Swing's event thread, the EDT.
 
parvez ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,I see the difference now.
Better to use BufferedImage.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic