覆盖 Scala 面板坐标

发布于 2024-12-06 05:18:59 字数 131 浏览 1 评论 0原文

我正在尝试覆盖 scala.swing.Panel 的坐标系。我只想在笛卡尔坐标系的第一象限中工作。因此,我希望将面板的左下角标记为 (0,0),因此当鼠标在面板上移动时,将仅记录为正 x 和 y 值。有谁知道这是否可能?

谢谢!!

I am attempting to override the coordinate system of a scala.swing.Panel. I want to work only in the first quadrant on the Cartesian coordinate system. Thus, I want the lower left hand corner of the panel to be noted as (0,0), and therefore the mouse will register as only positive x and y values when moving across the panel. Does anyone know if this is possible??

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2024-12-13 05:18:59

如果您只想平移坐标,使左下角是原点而不是右上角,请执行以下操作:

  case e: MouseClicked => {
    val newPoint = new Point(e.point.x, size.height - e.point.y)
    // do stuff using newPoint instead of e.point
  }

我认为您真的不想覆盖整个坐标系,因为它可能会对绘画和对齐组件造成严重破坏。

您可以做的另一件事是覆盖底层 JPanel 的 getMousePosition 方法:

  val p = new Panel { 
    override lazy val peer = new javax.swing.JPanel with SuperMixin {
      override def getMousePosition = {
        val p: Point = super.getMousePosition
        new Point(p.x, getHeight - p.y)
      }
    }
  ...

然后

  case e: MouseClicked => {
    val newPoint: Point = peer.getMousePosition
    // do stuff
  }

,覆盖 getMousePosition 不会影响 MouseClicked< 上报告的位置/code> 事件,因此与第一种方式相比并没有获得太多好处。

If you just want to translate the co-ordinates so that the bottom-left is the origin rather than the top-right, do this:

  case e: MouseClicked => {
    val newPoint = new Point(e.point.x, size.height - e.point.y)
    // do stuff using newPoint instead of e.point
  }

I don't think you really want to override the whole co-ordinate system, as it would likely play havoc with painting and alligning the component.

Another thing you could do would be to override the getMousePosition method of the underlying JPanel:

  val p = new Panel { 
    override lazy val peer = new javax.swing.JPanel with SuperMixin {
      override def getMousePosition = {
        val p: Point = super.getMousePosition
        new Point(p.x, getHeight - p.y)
      }
    }
  ...

Then

  case e: MouseClicked => {
    val newPoint: Point = peer.getMousePosition
    // do stuff
  }

However, overriding getMousePosition doesn't affect the reported position on the MouseClicked event, so doesn't gain much over the first way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文