Java Cross Hatching Texture

Java Cross Hatching Texture

一人独醉 发布于 2021-11-30 字数 820 浏览 822 回复 1 原文

Any know how to recreate a cross hashing texture in Java? The C# code belows shows how to accomplish this for the .NET framework. The Java snippet is close, but I've been unable to correctly rotate the lines by 45 degrees.

C#

HatchBrush crossHatch =
        new HatchBrush(HatchStyle.Cross, somecolor, somecolor);

Java

BufferedImage bufferedImage =
        new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = bufferedImage.createGraphics();

g2.setColor(Color.BLUE);
g2.fillRect(0, 0, 5, 5);
g2.setColor(pinColor);
g2.fillOval(0, 0, 5, 5);

// paint with the texturing brush
Rectangle2D rect = new Rectangle2D.Double(0, 0, 5, 5);
g2d.setPaint(new TexturePaint(bufferedImage, rect));
g2d.fill(shape);

Thanks in advance.

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2022-06-07 1 楼

Here's one that should cross-hatch at 5-pixel intervals:

BufferedImage bufferedImage =
        new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = bufferedImage.createGraphics();
g2.setColor(backColor);
g2.fillRect(0, 0, 5, 5);
g2.setColor(stripeColor);
g2.drawLine(0, 0, 5, 5); // 
g2.drawLine(0, 5, 5, 0); // /

// paint with the texturing brush
Rectangle2D rect = new Rectangle2D.Double(0, 0, 5, 5);
g2d.setPaint(new TexturePaint(bufferedImage, rect));
g2d.fill(shape);