如何在Java 2D中创建虚线笔划?
package org.nhooo.example.geom;import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
public class DrawDashedStroke extends JComponent {
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
float[] dash = {10.0f, 5.0f, 3.0f};
// 创建一个虚线
Stroke dashed = new BasicStroke(2.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
g2.setStroke(dashed);
g2.setPaint(Color.RED);
g2.draw(new RoundRectangle2D.Double(50, 50, 300, 100, 10, 10));
}
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Dashed Stroke Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new DrawDashedStroke());
frame.pack();
frame.setSize(new Dimension(420, 250));
frame.setVisible(true);
}
}
此代码段产生以下输出:
以上是 如何在Java 2D中创建虚线笔划? 的全部内容, 来源链接: utcz.com/z/356619.html