我想在java中绘制虚线和波浪线。
我可以使用Graphics
和g.drawLine()
方法绘制法线。
有没有一种简单的方法可以在Graphics2D
或类似的东西中绘制虚线和波浪线?
现在我使用MouseListener
的坐标绘制线条。它就像MS Paint一样。
答案 0 :(得分:1)
虚线,as presented by Kevin Workman:
public void drawDashedLine(Graphics g, int x1, int y1, int x2, int y2){
//creates a copy of the Graphics instance
Graphics2D g2d = (Graphics2D) g.create();
Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
g2d.setStroke(dashed);
g2d.drawLine(x1, y1, x2, y2);
//gets rid of the copy
g2d.dispose();
}
您可以创建虚线,就像使用它一样。
import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;
public class CurveDraw extends JFrame {
public static void main(String[] args) {
CurveDraw frame = new CurveDraw();
frame.setVisible(true);
}
public CurveDraw() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
}
public void paint(Graphics g) {
QuadCurve2D.Double curve = new QuadCurve2D.Double(50,100,100,170,150,100);
((Graphics2D)g).draw(curve);
}
}
Docs.oracle to learn more about Swing 的 wavy line 强>
<强> Curvy line 强>