gpt4 book ai didi

Java - 如何绘制虚线和波浪线?

转载 作者:行者123 更新时间:2023-12-01 12:42:33 27 4
gpt4 key购买 nike

我想在java中绘制虚线和波浪线。我可以使用 Graphics 和 g.drawLine() 方法绘制普通线条。有没有一种简单的方法可以在Graphics2D或类似的东西中绘制虚线和波浪线?

现在,我使用 MouseListener 的坐标绘制线条。所以它有点像 MS Paint。

最佳答案

虚线,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();
}

您可以像这样创建虚线。

波浪线,as presented by Tiger:

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

<强> Docs.oracle to learn more about Swing

关于Java - 如何绘制虚线和波浪线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971437/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com