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

a baffling problem

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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


SCJP
Visit my download page
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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...


Steve
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 152

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

Or possibly, a repaint() was called automatically as a side effect of the combobox selection or something.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a baffling problem