Question

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!!

Was it helpful?

Solution

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.

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