Use of the java.awt.Dimension class

uetoyo picture uetoyo · Jun 30, 2013 · Viewed 13.7k times · Source

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

Constructor:

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

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

Answer

Oswald picture Oswald · Jun 30, 2013

Don't use java.awt.Dimension just because it happens to provide an interface that looks similar to the one you need. As the introduction says in the documentation:

The Dimension class encapsulates the width and height of a component ... in a single object.

By component, a Java AWT Component is meant. It seems weird to use a java.awt.Dimension for anything else.

If you provide your own implementation of java.awt.Dimension, it must adhere to the same interface as the one provided by the Java Runtime environment. So you do not gain anything. BTW, there is no need to provide your own implementation of java.awt.Dimension, the Java Runtime environment already provides a perfectly suitable implementation.

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