0
public void colortemperatureJSliderStateChanged(ChangeEvent event) {
   fahrenheitdegree = colortemperatureJSlider.getValue(); 
   fahrenheitJPanel.setBounds(100,270, fahrenheitdegree, 20);

   celsiusdegree = (fahrenheitdegree - 32.0)*5.0/9.0;
   celsiusJPanel.setBounds(100,220,celsiusdegree, 20);
}// end of public void colortemperatureJSliderStateChanged..

My professor want both variables (Celsius and Fahrenheit) as a double I set declarations double for celsiusdegree; and fahrenheitdegree;

Somehow, compile identified two error on both row of JPanel.setBounds(xxx,xxx, variable, xx); because it is "incompatible types: possible lossy conversion from double to int."

When I tried to change the variable to int. The error is that the formula for celsiusdegree won't recognize int. So how do I make it work for double variables?

Vogel612
  • 5,620
  • 5
  • 48
  • 73
blacklune
  • 43
  • 1
  • 7

3 Answers3

1

To make this work for double, you should keep the variables as double and tell the compiler to perform the lossy conversation anyways.

so:

celsiusJPanel.setBounds(100, 220, (int) celsiusdegree, 20);

should work just fine.

More information can be found in "Why can int/byte/short/long be converted to float/double without typecasting but vice-versa not possible", as well as the relevant section of the JLS

Community
  • 1
  • 1
Vogel612
  • 5,620
  • 5
  • 48
  • 73
0

JPanel's setBounds sets the top right of the panel to the first and second argument as an x,y coordinate. The third and fourth arguments specify the width and height respectively. You would need to create a label or text field to display your values.

MrPublic
  • 520
  • 5
  • 16
0

I would suggest to keep celsiusdegreeand fahrenheitdegree in Double and in then use setBounds as folows:

celsiusJPanel.setBounds(100, 220, celsiusdegree.intValue(), 20);

Because you cant cast Double to int.

Community
  • 1
  • 1
edasssus
  • 331
  • 4
  • 15
  • But how do you know that OP is using `Double`? If he/she is really using `double`, that won't work, and casting *is* necessary. – TNT Oct 15 '15 at 13:37
  • Yea. You are right, but OP didn't show rest of his code and answer with casting was here so I did that other way assuming that OP used `Double`. – edasssus Oct 15 '15 at 13:43