如何在Java 2D中画线?
以下代码段显示了如何使用Graphics2D.draw()method绘制一条简单的线。此方法采用实现java.awt.Shape接口的参数。
要画一条线,我们可以使用Line2D.Doublestatic-inner类。该类的构造函数采用四个整数值,分别代表线的起点(x1,y1)和终点(x2,y2)。
package org.nhooo.example.geom; import javax.swing.*; import java.awt.*; import java.awt.geom.Line2D; public class DrawLine extends JComponent { @Override public void paint(Graphics g) { //使用Graphics2Ddraw()方法绘制一条简单的线。 Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(2f)); g2.setColor(Color.RED); g2.draw(new Line2D.Double(50, 150, 250, 350)); g2.setColor(Color.GREEN); g2.draw(new Line2D.Double(250, 350, 350, 250)); g2.setColor(Color.BLUE); g2.draw(new Line2D.Double(350, 250, 150, 50)); g2.setColor(Color.YELLOW); g2.draw(new Line2D.Double(150, 50, 50, 150)); g2.setColor(Color.BLACK); g2.draw(new Line2D.Double(0, 0, 400, 400)); } public static void main(String[] args) { JFrame frame = new JFrame("Draw Line"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawLine()); frame.pack(); frame.setSize(new Dimension(420, 440)); frame.setVisible(true); } }
运行代码