aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Having issue with Graphics and Graphics2d, program is running multiple times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Having issue with Graphics and Graphics2d, program is running multiple times " Watch "Having issue with Graphics and Graphics2d, program is running multiple times " New topic
Author

Having issue with Graphics and Graphics2d, program is running multiple times

matt dowmann
Greenhorn

Joined: Nov 20, 2012
Posts: 3
hello...
i am making a program in which a user inputs 2 strings and depending on user input, either a line is drawn or a circle is drawn.......but problem is that the program is executing multiple times.......i can see the multiple executions in console....i am new to graphics so maybe i am writting its methods wrongly..


PS that i also have to do future operations on the programe(this is just simple overview of problem).

thanks in advance
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Hello matt and welcome to the Ranch! Why, in this day and age, are you using AWT components? Swing replaced AWT as the preferred GUI toolkit more than 10 years ago! To port your application to Swing, go through the tutorial on Creating a GUI With JFC/Swing. Two parts you will need to know and understand correctly are Concurrency in Swing and Performing Custom Painting.

As far as your 'problem' is concerned: the programmer has absolutely no control over how often and how many times any painting method is called. That doesn't change whether you're using AWT, Swing or any other GUI toolkit, Java or otherwise.

For your future posts, please do remove inane IDE-generated doc comments that just add clutter, and any other irrelevant comments in your code. Thank you.


luck, db
There are no new questions, but there may be new answers.
matt dowmann
Greenhorn

Joined: Nov 20, 2012
Posts: 3
Darryl Burke wrote:
As far as your 'problem' is concerned: the programmer has absolutely no control over how often and how many times any painting method is called.


what!!! noooooooo!!!
thats just unfortunate to me.........isnt there any trick available to counter this? or any other way for drawing shapes and stuff?

and i will try to use Swing in future programes......and sorry for those IDE comments, i will try not put them in future posts...........and thanks for your help
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

No, there are no tricks, nor are any needed. Just make sure that painting methods restrict themselves to painting and painting only; no business logic, no change of state of any GUI component.
matt dowmann
Greenhorn

Joined: Nov 20, 2012
Posts: 3
Darryl Burke wrote:No, there are no tricks, nor are any needed. Just make sure that painting methods restrict themselves to painting and painting only; no business logic, no change of state of any GUI component.

yep that business logic thing does make sense........but wierdly as some time passed, the problem got fixed magically(i didnt do anything with the paint method)...............anyways thanks for your help mate.......

also i am new to the site so should I put this thread as resolved? cause if you look closely, we didnt resolved the matter, we just made our way around the problem.......but thanks none the less..........you were a big help............atleast i now know the limitation of a thing which i wanted to use
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
.but wierdly as some time passed, the problem got fixed magically(i didnt do anything with the paint method).

As Darryl said you have no control over the the number of time paint is called. Whether or not paint() is called multiple times is down to lots of things outside of your programs control, try dragging another window over your applications window and you will see your paint method is suddenly called lots of times. This is not a fault, it's just the way it works hence Darryl's advice about business logic.

also i am new to the site so should I put this thread as resolved? cause if you look closely, we didnt resolved the matter, we just made our way around the problem

Actually there was no problem is solve other than you did not understanding the way the calls to paint() worked which means your code was essentially correct, so well done
I would, however, recommend you do as Darryl suggested and convert your code to use Swing.

I'll mark the thread as resolved for you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having issue with Graphics and Graphics2d, program is running multiple times