java2d破折号模式

时间:2010-12-19 12:52:02

标签: java-2d

我需要使用java2d制作动画选择工具的解决方案。

我知道BasicStroke和Rectagnle2D API,但我不知道制作黑白破折号

并激活了这个。有人知道这项工作吗?

alt text

感谢

1 个答案:

答案 0 :(得分:0)

void paint(Graphics2D g) {

    //define these constants yourself
    float dashWidth;
    float offset;

    //draw solid, i.e. background
    g.setColor(Color.WHITE);
    g.setStroke(new BasicStroke(width, cap, join, miter, null));
    g.drawLine(x1, y1, x2, y2);

    //draw the pattern on top
    float[] pattern = new float[] {dashWidth, dashWidth*2}
    g.setColor(Color.BLACK);
    g.setStroke(new BasicStroke(width, cap, join, miter, pattern, offset));
    g.drawLine(x1, y1, x2, y2);
}

这适用于任何形状,因此如果您需要,请将drawLine替换为drawRect。要设置动画,请切换颜色并重新绘制。