【问题标题】:How to show dragging animation when drawing boxes on canvas in Java?用Java在画布上绘制框时如何显示拖动动画?
【发布时间】:2018-11-30 08:37:41
【问题描述】:

我有一个画布,并且正在使用鼠标事件侦听器来绘制框。但是,这些框只会出现在 mouseUp 上,但我想要的是在绘制时显示这些框,以便用户可以看到他们正在绘制的框的大小。

这是我的 mouseDown 和 mouseUp 代码:

public Canvas(int width, int height) {

element = getElement();

element.addEventListener("mousedown", event -> {  // Retrieve Starting Position on MouseDown

            Element boundingBoxResult = ElementFactory.createDiv();
            element.appendChild(boundingBoxResult);

            JsonObject evtData = event.getEventData();

            double xBox = evtData.getNumber("event.x");
            double yBox = evtData.getNumber("event.y");
            boundingBoxResult.setAttribute("data-x", String.format("%f", xBox));
            boundingBoxResult.setAttribute("data-y", String.format("%f", yBox));

            BoundingBox newBox = new BoundingBox(0, xBox, yBox, 0.0, 0.0);
            arrayBoxes.add(newBox);

            isDrawing = true;

            mouseIsDown=true;


        }).addEventData("event.x").addEventData("event.y");


        element.addEventListener("mouseup", event -> {  // Draw Box on MouseUp

            Element boundingBoxResult2 = ElementFactory.createDiv();
            element.appendChild(boundingBoxResult2);

            JsonObject evtData2 = event.getEventData();

            endX = evtData2.getNumber("event.x");
            endY = evtData2.getNumber("event.y");
            boundingBoxResult2.setAttribute("end-x", String.format("%f", endX));
            boundingBoxResult2.setAttribute("end-y", String.format("%f", endY));

            double xcoordi = 0;
            double ycoordi = 0;
            double boxWidth = 0;
            double boxHeight = 0;

            for (int i = 0; i < arrayBoxes.size(); i++) {
                arrayBoxes.get(i).setName(i + 1);
                arrayBoxes.get(i).setWidth(endX, arrayBoxes.get(i).xcoordi);
                arrayBoxes.get(i).setHeight(endY, arrayBoxes.get(i).ycoordi);
                xcoordi = arrayBoxes.get(i).getXcoordi();
                ycoordi = arrayBoxes.get(i).getYcoordi();
                boxWidth = arrayBoxes.get(i).getWidth();
                boxHeight = arrayBoxes.get(i).getHeight();

            }

                mouseIsDown=false;
                context.beginPath();
                context.setFillStyle("limegreen");
                context.setLineWidth(2);
                context.strokeRect(xcoordi, ycoordi, boxWidth, boxHeight);
                context.fill();

            System.out.println(arrayBoxes.toString());

        }).addEventData("event.x").addEventData("event.y");

}

非常感谢任何帮助,谢谢!

【问题讨论】:

    标签: java canvas drawing mouseevent vaadin


    【解决方案1】:

    您可以在 mousedown 事件中创建 mousemove-eventlistener ,然后在 mouseup 事件中再次删除该侦听器。这样做是为了在不需要鼠标移动事件侦听器时并不总是处于活动状态。

    在 mousemove 事件监听器中,用鼠标按下位置的 x1/y1 和当前鼠标位置的 x2/y2 绘制一个矩形的轮廓。始终从最后一个 mousemove 事件中删除/重用矩形轮廓,因此始终只存在一个这样的矩形轮廓。

    我不知道如何在画布中制作矩形轮廓,但看看你是如何使用它一段时间的(请参阅你的其他问题 :D)我假设你知道如何做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-13
      • 2014-07-19
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 2021-10-26
      • 2022-10-07
      • 2019-04-12
      相关资源
      最近更新 更多