在Java中用破折号画线结果是实线

问题描述 投票:0回答:0

我在 Java 中用 [2/3f, 1/3f] 破折号画一条线,但结果是一条实线。

我还尝试了其他破折号,这是结果:

[2/3f, 1/3f]: a solid line
[0.5f, 0.25f]: a good dash line
[0.6f, 0.3f]: a dash line with small difference
[0.66f, 0.33f]: a bad dash line with big difference
[2, 1]: a good dash line

代码:

//try width with 1/3f, 0.25f, 0.3f, 0.33f, 1f, different results get.
float width = 1/3f;
float[] dash = new float[] {2*width, 1*width};

BufferedImage image = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB);

Graphics2D g = image.createGraphics();
g.setColor(Color.RED);
BasicStroke basicStroke = new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 
        10, dash, 0);
g.setStroke(basicStroke);
g.drawLine(0, 300, 600, 300);
g.dispose();

ImageIO.write(image, "PNG", new File("test.png"));
java awt
© www.soinside.com 2019 - 2024. All rights reserved.