Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

See JavaDoc:

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

So, if you need a Dimension class holding width and height in floating point precision, you can't use java.awt.Dimension. Looking at the Methods java.awt.Dimension provides it should be fairly easy to provide an own implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top