java如何画虚线
发布网友
发布时间:2022-04-21 05:24
我来回答
共1个回答
热心网友
时间:2022-06-18 05:20
可以使用 Graphics2D 和 BasicStroke 画虚线,如下:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class App extends JFrame {
public App() {
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 10, 10 }, 0);
g2d.setStroke(stroke);
g2d.drawLine(50, 100, 300, 100);
}
public static void main(String[] argv) {
new App().setVisible(true);
}
}
运行结果: