• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
matt dowmann
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
matt dowmann
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2971
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic