• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

a baffling problem

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a Paint program that i am improving. it used a custom class called StandardColorSelector which used a JComboBox to select a color. i changed it to use a JColorSelector instead. the problem is that now when i change either the background color or the foreground color, it fails to store(or redraw) the previously drawn shapes like it used to . it is pretty long so i will try to select relevant parts that might be the problem. please excuse the indentation.
old

new


this is pretty much all i changed
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, that isn't enough information. There is nothing there about 'drawn shapes' or 'saving' so we would have to speculate on everything relevant to the problem. Make a SSCCE (http://sscce.org/) that shows the problem. Make it as small as possible, but make it show the problem...
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was afraid of that. best i can do is post all the code, even though i only changed Paint.java




 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a little further information. say i set the background to a pink color and the foreground to say a green color and draw something. if i then try to change the background color, but change my mind and choose cancel, it eliminates what i drew and changes the background to the default of white. as i mentioned, the program works fine using the StandardColorSelector(JComboBox).
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:a little further information. say i set the background to a pink color and the foreground to say a green color and draw something. if i then try to change the background color, but change my mind and choose cancel, it eliminates what i drew and changes the background to the default of white.


That is as it should be. When you cancel, the default value returned is Color.WHITE because that is what:

Says to do. If you want it to revert to last-selected-background you would substitute Color.WHITE to the last backColor:



as i mentioned, the program works fine using the StandardColorSelector(JComboBox).


Your first two conditions in the actionPerformed are if{}. Then you do a long line of if {} else if {} else if {} ... else {}. When you do the backButton, for example, it will execute the first if, find out it is true and so execute that code. Then it will do the second if, find out it is false and skip it. Then it will execute the third if, find it is false and fall to the else if, find it is false and fall to the next else if, etc... until it gets to else {}. Since there is no condition on the else, and since the previous if was false, the code inside the else{} gets executed. What does that code do?

This is why you have different actionListeners for each button. Makes inappropriate button interaction less likely.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Steve!
that is obvious now.
when i added the two new buttons to actionPerformed, i started them both with if!!!
so i have if, if ,if, else if
when i should have if, else if, else if, else if
as for seperate listeners, i have been chided for that before
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is much better. i am surprised i did'nt see it myself
i just have one problem now.
i choose background and foreground and draw. i change foreground and draw again. everything is fine.
but if i change background, the background color doesn't change until i draw something else. i am thinking i need to call repaint() when i change background. with the old program, the background color changed as soon as i chose a new one, now it waits untill i draw something new. then it does change
 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did the old program run on an older JVM? I'm guessing but, maybe the need to call repaint() directly was an added feature in newer JVMs?

 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or possibly, a repaint() was called automatically as a side effect of the combobox selection or something.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think Steve is correct here. i think the JVM used to call it for me(because of the JComboBox), but now i have to call it myself.
one or two final points for anyone interested. the API can be your best friend. it told me that if the user chooses cancel or closes the dialog from the title bar it will return null. apparently the color you pass to the constructor is only for if the user makes no choice and clicks OK.

it works fine now but i am still a bit baffled. i have no idea how white and black are the default background and foreground colors. to be safe i am going to assign those values when i declare the variables. also for some reason the foreground works ok the way it was but i needed a few lines of extra code for the background. again i am going to err on the side of safety by making them the same.


the only thing left to do is add a tab to the JColorChooser so the user can use the old "JComboBox" to choose. the tutorial is pretty clear on how to do that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic