Using the java.awt.Dimension Class

I would like to use the Dimension class ( java.awt.Dimension ), but it only supports integers. I want to make Rectangle and Square such classes:

Constructor:

 public Rectangle(Dimension dim(double A, double B)) { // constructor code } 

Is it better to write your own implementation of the Dimension class?

+4
source share
3 answers

Do not use java.awt.Dimension just because it provides an interface similar to the one you need. As the documentation says:

The Dimensions class encapsulates the width and height of a component ... into a single object.

Component implies Java AWT Component . It seems strange to use java.awt.Dimension for anything else.

If you provide your own implementation of java.awt.Dimension , it should adhere to the same interface as the Java Runtime environment. This way you get nothing. BTW, there is no need to provide your own implementation of java.awt.Dimension , the Java Runtime environment already provides a perfect implementation.

Write your own class that can handle float values ​​as width and height.

+6
source

See JavaDoc :

The Dimension class encapsulates the width and height of a component ( in whole precision ) in a single object.

So, if you need a dimension class that contains the width and height exactly as a floating point, you cannot use java.awt.Dimension . Looking at java.awt.Dimension Methods, it should be fairly easy to provide your own implementation.

+3
source

Source: https://habr.com/ru/post/1488946/


All Articles