You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fig: The representation of the pixel coordinates space and points coordinates space using coordinate mappers
418
418
419
+
### Usage of Rectangle.OfFloat & Point.OfFloat
420
+
421
+
Since both the SWT API and the Windows API predominantly use integer values for positions, sizes, and bounds, rounding is generally involved when values are converted to or from the operating system.
422
+
If API return values (for example, `Control.getLocation`) are used in calculations and later passed to other API methods (such as `Control.setBounds`), multiple destructive rounding steps can occur. These accumulated roundings may result in final pixel values that are larger or smaller than expected.
423
+
To mitigate this effect, most API methods in Windows return internal subclasses `Rectangle.OfFloat` or `Point.OfFloat`, which preserve fractional residuals so that a correct rounding can be applied at a later stage.
424
+
To maintain this behavior, it is important to keep and reuse the returned `Point`/`Rectangle` instances instead of replacing them with new ones. The static utility methods provided by `Rectangle` and `Point` help retain as much precision as possible throughout these operations.
425
+
426
+
**Example**
427
+
428
+
In the following example, the parent control is resized based on the current size of a child. One possible implementation is to use the `Control.setBounds(int, int, int, int)` API, which converts any potential `Point.OfFloat` inputs and immediately applies rounded integer values.
429
+
430
+
```java
431
+
void resize(Composite parent, Control, child) {
432
+
Point childSize = child.getSize();
433
+
parent.setBounds(5, 5, childSize.x, childSize.y);
434
+
}
435
+
```
436
+
437
+
A better alternative is to use the `Control.setBounds(Rectangle)` API and create the bounds using `Rectangle.of(Point, Point)`. On Windows, this will automatically create a `Rectangle.OfFloat` if any of the provided points is a `Point.OfFloat`, preserving fractional values and improving the precision of subsequent conversions.
As a rule of thumb, avoid API methods that take raw integer values when an equivalent overload accepting `Rectangle` or `Point` is available, as these retain precision and reduce cumulative rounding errors.
448
+
449
+
Be aware that `Rectangle.OfFloat` or `Point.OfFloat` are internal classes which you should not instantiate outside of SWT. There are static utility methods `Rectangle.of` and `Point.of` that will create precise version if necessary as well as implementations of `Rectangle.clone` and `Point.clone` to create precise copies if the original one was precise.
450
+
419
451
### Process & Thread DPI Awareness
420
452
421
453
Windows differentiates between process and thread DPI awareness.
0 commit comments