简体   繁体   中英

java instant double to int

I have looked around but all conversions have used more than one line and variables. I am trying to paint objects in a certain co-ordinates times a double that changes when you change the size of the frame.

Width = getWidth();
Height = getHeight();
cWidth = 1900/Width;
cHeight = 1030/Height;

Inside the paint class,

g.fillOval (PlayerX/cWidth, PlayerY/cHeight, 50/cWidth, 50/cHeight);

but I get the error:

The method fillOval(int, int, int, int) in the type Graphics is not applicable for the arguments (double, double, double, double).

Would I have to make a separate variable for all 4 for every object painted or is there an easier way?

您必须将它们转换为int。

g.fillOval ((int)(PlayerX/cWidth), (int)(PlayerY/cHeight), (int)(50/cWidth), (int)(50/cHeight);

If your dont have a method like:

fillOval(double a, double b, double c, double d) 

you can not do

g.fillOval (PlayerX/cWidth, PlayerY/cHeight, 50/cWidth, 50/cHeight);

you need to pass integers not doubles, if loosing the presision is not a problem then try casting

g.fillOval ((int)(PlayerX/cWidth), (int)(PlayerY/cHeight), (int)(50/cWidth), (int)(50/cHeight));

Note:

you need to cast carefully grouping the result. the reason is:

PlayerX is a double, same as cWidth , so doing :

(int)PlayerX/cWidth

will not work, since the result is the same as int/double → double


 PlayerX/(int)cWidth

will not work either, since the result is the same as doublet/int → double


the option that will work is

(int)(PlayerX/cWidth)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM