Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of the java.awt.Dimension class

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?

like image 429
uetoyo Avatar asked Dec 11 '22 13:12

uetoyo


2 Answers

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.

like image 162
Oswald Avatar answered Dec 30 '22 15:12

Oswald


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.

like image 27
jlordo Avatar answered Dec 30 '22 17:12

jlordo