Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change a parameter type of a API class?

 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay what I mean is I am trying to fill/draw in my paint method using Floats, but it only accepts integers..



Basically

Anything with a 1 after it i.e., y1 is an int, and the 'x' is a float.

Normally everything is a float, but I had to convert it using x1 = (int) x;

to get the fillRect/drawRect to work... Now I rather not have to change these, so is there a way I can keep them as a float and use the g2 class? Do I just override the fillRect/drawRect methods to accept floats instead, or is there another way.


Also

What exactly is it doing here? It seems that we are setting g2 to Graphics g, because g2 is a subclass of g. I also tried
 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can cast them as you make the call, like:



But why do you want things that measure pixels to be floats?
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:You can cast them as you make the call, like:



But why do you want things that measure pixels to be floats?


That's casting them to an int tho... I want to keep them as a float..

I'm not sure why exactly, I'm importing something using floats, I'm going to meet someone now and ask... thanks.
 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Orsaw wrote:
That's casting them to an int tho...


That's true. In that case, the answer is no. There is no way to pass floats as parameters to a method that declares those parameters to be ints.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:
Jay Orsaw wrote:
That's casting them to an int tho...


That's true. In that case, the answer is no. There is no way to pass floats as parameters to a method that declares those parameters to be ints.


Without implementing the class and overriding right?

So figured out they can be ints and what I needed to do, for the floats....



SO now I'm just curious about the Graphics initiation...

Oh and what's special about an abstract class?
 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Orsaw wrote:
Without implementing the class and overriding right?


Subclassing, you mean? No, if you override a method in a subclass, it has to have the same signature (i.e., parameter types) as in the parent class. You can overload a method to give it a different signature, but that's really a different method.

Jay Orsaw wrote:
Oh and what's special about an abstract class?


An abstract class cannot be directly instantiated. It must be subclassed by a concrete class for an instance to be created.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic