• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

unusual behaviour of subclass of JPanel

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,I see the difference now.
Better to use BufferedImage.
Thanks
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic