简体   繁体   English

Java自定义Path2D

[英]Java custom Path2D

I have created a custom Path2D class to draw an H-shaped "calliper" on screen, for a project I am doing. 我创建了一个自定义Path2D类,以便为我正在做的项目在屏幕上绘制一个H形的“卡尺”。 I want to drag and eventually resize the calliper on screen. 我想拖动并最终在屏幕上调整游标卡尺的大小。 I have managed to get the Path2D set up so I can draw the calliper, and the code looks like this: 我已经成功设置了Path2D,因此可以绘制游标卡尺,代码如下所示:

  1. Declaration and Constructor: 声明和构造函数:

     public class Calliper extends Path2D.Double { // X and Y coordinates of all six points on Calliper double cX1, cX2, cX3, cX4, cX5, cX6; double cY1, cY2, cY3, cY4, cY5, cY6; // Width and Height double cWidth; double cHeight; public Calliper(double x, double y, double w, double h) { cWidth = w; cHeight = h; cX1 = x; cY1 = y; cX2 = x; cY2 = y + (h/2); cX3 = x; cY3 = y + h; cX4 = x + w; cY4 = y; cX5 = cX4; cY5 = cY4 + (h /2); cX6 = cX4; cY6 = cY4 + h; build(); } 
  2. build() method (used to draw the path) and setCalliper() method, used to redefine the coordinates, or width, height: build()方法(用于绘制路径)和setCalliper()方法,用于重新定义坐标或宽度,高度:

     private void build() { // Draw the path for the calliper moveTo(cX1, cY1); lineTo(cX2, cY2); lineTo(cX3, cY3); moveTo(cX2, cY2); lineTo(cX5, cY5); moveTo(cX4, cY4); lineTo(cX6, cY6); } public void setCalliper(double x, double y, double w, double h) { // Rebuild the calliper using different x,y coordinates, or // different width/height cWidth = w; cHeight = h; cX1 = x; cY1 = y; cX2 = x; cY2 = y + (h/2); cX3 = x; cY3 = y + h; cX4 = x + w; cY4 = y; cX5 = cX4; cY5 = cY4 + (h /2); cX6 = cX4; cY6 = cY4 + h; build(); } 

I have created a class to draw this calliper on the screen, which it will do, however if I try to drag the calliper around the screen, it doesn't erase the original shape as I drag, so I get a long trail of shapes left behind. 我创建了一个类来在屏幕上绘制此游标卡,它会这样做,但是,如果我尝试在屏幕上拖动游标卡尺,则拖动时它不会擦除原始形状,因此得到的形状很长被留下来。 I thought I had omitted super.paintComponent(g) from my paintComponent(Graphics g) method, but even with it in there the code still does not work. 我以为我已经从我的paintComponent(Graphics g)方法中省略了super.paintComponent(g) ,但是即使在那里,代码仍然无法正常工作。

My drag method looks like this: 我的拖动方法如下所示:

@Override
public void mouseDragged(MouseEvent ev) 
{

    double mx = ev.getX();
    double my = ev.getY();

    if (dragging)
    {
        calX = mx - offsetX;
        calY = my - offsetY;

        cal = setCalliper(calX, calY, calW, calH);
        repaint();
    }

}

If I change the line cal = setCalliper(calX, calY, calW, calH); 如果我更改行cal = setCalliper(calX, calY, calW, calH); above to read cal = new Calliper(calX, calY, calW, calH); 上面读取cal = new Calliper(calX, calY, calW, calH); then it works, but I have been told I shouldn't do it this way. 然后它起作用了,但是有人告诉我不应该这样。

Any ideas why it doesn't work as expected? 任何想法为什么它不能按预期工作?

The setCalliper() directly calls the build method, a method which appends new points to all the previous points added to the Path2D - so each time mouseDragged is called more points are added to the Path. setCalliper()直接调用build方法,该方法将新点添加到添加到Path2D所有先前点上-因此,每次调用mouseDragged都会将更多点添加到Path中。 Try calling reset() before calling build() (or call reset in the build method before the moveTo/lineTo calls). 尝试在调用build() reset()之前调用reset() (或在moveTo / lineTo调用之前在build方法中调用reset)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM