簡體   English   中英

Java-如何繪制虛線和波浪線?

[英]Java - How to draw dashed and wavy lines?

我想在Java中繪制虛線和波浪線。 我可以使用Graphicsg.drawLine()方法繪制法線。 有沒有一種簡單的方法可以在Graphics2D或類似Graphics2D繪制虛線和波浪線?

現在,我使用MouseListener的坐標繪制線條。 因此它類似於MS Paint。

由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了解有關Swing 波浪線的 更多信息

曲線線

Docs.oracle以了解有關Swing的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM