You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

继承java.awt.Rectangle时,如何在构造方法中使用float类型宽高?

How to Use Float Width/Height with a Rectangle (Instead of java.awt.Rectangle's Ints)

Great question! The core issue here is that java.awt.Rectangle is designed to use integer coordinates and dimensions under the hood—so you can't directly pass float values to its constructor or modify its internal storage to use floats. But there are two solid approaches to get the float-based width/height behavior you need:

Java provides the java.awt.geom.Rectangle2D.Float class in the Java2D API, which natively supports float values for x, y, width, and height. This is the cleanest solution because you don't have to work around the limitations of java.awt.Rectangle.

Here's how you can adapt your class to use it:

import java.awt.geom.Rectangle2D;

public class Rectangle {
    private Position position;
    private Rectangle2D.Float internalRect;

    // Copy constructor
    public Rectangle(Rectangle toCopy) {
        this.position = toCopy.position;
        this.internalRect = new Rectangle2D.Float(
            toCopy.internalRect.x,
            toCopy.internalRect.y,
            toCopy.internalRect.width,
            toCopy.internalRect.height
        );
    }

    // Constructor with float dimensions
    public Rectangle(Position position, float width, float height) {
        this.position = position;
        // Adjust x/y if your Position class holds coordinates—this example uses 0,0 as default
        this.internalRect = new Rectangle2D.Float(0, 0, width, height);
    }

    // Getters for float dimensions
    public float getWidth() {
        return internalRect.width;
    }

    public float getHeight() {
        return internalRect.height;
    }

    // Optional: Add a method to convert to java.awt.Rectangle if you need to interact with legacy code
    public java.awt.Rectangle toAwtRectangle() {
        return new java.awt.Rectangle(
            (int) Math.round(internalRect.x),
            (int) Math.round(internalRect.y),
            (int) Math.round(internalRect.width),
            (int) Math.round(internalRect.height)
        );
    }
}

Pros:

  • No precision loss (native float support)
  • Uses standard Java API components
  • Avoids messy workarounds with inherited integer fields

If you absolutely must extend java.awt.Rectangle (e.g., for compatibility with existing code), you can store your own float width/height values and override the parent class's methods to avoid confusion. Note that this approach has tradeoffs, since the parent class will still use integer values internally.

Here's an example implementation:

public class Rectangle extends java.awt.Rectangle {
    private Position position;
    private float floatWidth;
    private float floatHeight;

    // Copy constructor
    public Rectangle(Rectangle toCopy) {
        super(toCopy);
        this.position = toCopy.position;
        this.floatWidth = toCopy.floatWidth;
        this.floatHeight = toCopy.floatHeight;
    }

    // Constructor with float dimensions
    public Rectangle(Position position, float width, float height) {
        // Convert floats to ints for the parent class (choose rounding/truncation based on your needs)
        super(Math.round(width), Math.round(height));
        this.position = position;
        this.floatWidth = width;
        this.floatHeight = height;
    }

    // Override parent getters to avoid returning misleading integer values
    @Override
    public int getWidth() {
        throw new UnsupportedOperationException("Use getFloatWidth() for float precision");
    }

    @Override
    public int getHeight() {
        throw new UnsupportedOperationException("Use getFloatHeight() for float precision");
    }

    // Custom getters for float dimensions
    public float getFloatWidth() {
        return floatWidth;
    }

    public float getFloatHeight() {
        return floatHeight;
    }

    // Override setters to keep float and integer values in sync
    @Override
    public void setSize(int width, int height) {
        super.setSize(width, height);
        this.floatWidth = width;
        this.floatHeight = height;
    }

    public void setSize(float width, float height) {
        super.setSize(Math.round(width), Math.round(height));
        this.floatWidth = width;
        this.floatHeight = height;
    }
}

Cons:

  • Risk of confusion between the parent class's integer values and your custom float values
  • Potential precision loss when converting floats to ints for the parent class
  • Clunky implementation compared to using Rectangle2D.Float

内容的提问来源于stack exchange,提问作者MDS18

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:42:02