aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes unusual behaviour of subclass of JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "unusual behaviour of subclass of JPanel" Watch "unusual behaviour of subclass of JPanel" New topic
Author

unusual behaviour of subclass of JPanel

parvez ali
Greenhorn

Joined: Oct 08, 2010
Posts: 7
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

Joined: Apr 06, 2010
Posts: 4392
    
    8

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

Joined: Oct 08, 2010
Posts: 7
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Apr 06, 2010
Posts: 4392
    
    8

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

Joined: Jun 13, 2009
Posts: 2183
    
    7
Examples of the two approaches suggested by Pete and Matthew can be found in Custom Painting Approaches.
parvez ali
Greenhorn

Joined: Oct 08, 2010
Posts: 7
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 08, 2010
Posts: 7
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 08, 2010
Posts: 7
yes,I see the difference now.
Better to use BufferedImage.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unusual behaviour of subclass of JPanel