如何用Java绘制线条

我想知道Java中是否有一个可以从坐标(x1,x2)到(y1,y2)画一条线的函数?

我想要的是做这样的事情:

drawLine(x1, x2, x3, x4); 

我希望能够在代码中随时执行此操作,同时显示多行。

我试过这样做:

 public void paint(Graphics g){ g.drawLine(0, 0, 100, 100); } 

但这让我无法控制何时使用该function,我无法弄清楚如何多次调用它。

希望你明白我的意思!

PS我想创建一个有很多坐标的坐标系。

绘制线条的摆动组件的一个非常简单的例子。 它在内部保留了一个列表,其中包含使用addLine方法添加的行。 每次添加新行时,都会调用重绘以通知图形子系统需要新的绘制。

该类还包括一些使用示例。

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.LinkedList; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class LinesComponent extends JComponent{ private static class Line{ final int x1; final int y1; final int x2; final int y2; final Color color; public Line(int x1, int y1, int x2, int y2, Color color) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.color = color; } } private final LinkedList lines = new LinkedList(); public void addLine(int x1, int x2, int x3, int x4) { addLine(x1, x2, x3, x4, Color.black); } public void addLine(int x1, int x2, int x3, int x4, Color color) { lines.add(new Line(x1,x2,x3,x4, color)); repaint(); } public void clearLines() { lines.clear(); repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (Line line : lines) { g.setColor(line.color); g.drawLine(line.x1, line.y1, line.x2, line.y2); } } public static void main(String[] args) { JFrame testFrame = new JFrame(); testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); final LinesComponent comp = new LinesComponent(); comp.setPreferredSize(new Dimension(320, 200)); testFrame.getContentPane().add(comp, BorderLayout.CENTER); JPanel buttonsPanel = new JPanel(); JButton newLineButton = new JButton("New Line"); JButton clearButton = new JButton("Clear"); buttonsPanel.add(newLineButton); buttonsPanel.add(clearButton); testFrame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH); newLineButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int x1 = (int) (Math.random()*320); int x2 = (int) (Math.random()*320); int y1 = (int) (Math.random()*200); int y2 = (int) (Math.random()*200); Color randomColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random()); comp.addLine(x1, y1, x2, y2, randomColor); } }); clearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { comp.clearLines(); } }); testFrame.pack(); testFrame.setVisible(true); } } 

将行存储在某种类型的列表中。 当需要绘制它们时,迭代列表并绘制每个列表。 像这样的东西。

DrawLines.java

 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.geom.Line2D; import javax.swing.JOptionPane; import javax.swing.JComponent; import javax.swing.SwingUtilities; import java.util.ArrayList; import java.util.Random; class DrawLines { public static void main(String[] args) { Runnable r = new Runnable() { public void run() { LineComponent lineComponent = new LineComponent(400,400); for (int ii=0; ii<30; ii++) { lineComponent.addLine(); } JOptionPane.showMessageDialog(null, lineComponent); } }; SwingUtilities.invokeLater(r); } } class LineComponent extends JComponent { ArrayList lines; Random random; LineComponent(int width, int height) { super(); setPreferredSize(new Dimension(width,height)); lines = new ArrayList(); random = new Random(); } public void addLine() { int width = (int)getPreferredSize().getWidth(); int height = (int)getPreferredSize().getHeight(); Line2D.Double line = new Line2D.Double( random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height) ); lines.add(line); repaint(); } public void paintComponent(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, getWidth(), getHeight()); Dimension d = getPreferredSize(); g.setColor(Color.black); for (Line2D.Double line : lines) { g.drawLine( (int)line.getX1(), (int)line.getY1(), (int)line.getX2(), (int)line.getY2() ); } } } 

截图

在此处输入图像描述

您需要创建一个扩展Component的类。 在那里你可以覆盖paint方法并将你的绘画代码放在:

 package blah.whatever; import java.awt.Component; import java.awt.Graphics; public class TestAWT extends Component { /** @see java.awt.Component#paint(java.awt.Graphics) */ @Override public void paint(Graphics g) { super.paint(g); g.drawLine(0,0,100,100); g.drawLine(10, 10, 20, 300); // more drawing code here... } } 

将此组件放入应用程序的GUI中。 如果您正在使用Swing,则需要扩展JComponent并覆盖paintComponent。

正如Helios所提到的,绘画代码实际上告诉系统你的组件是什么样的。 系统会在认为需要(重新)绘制时询问此信息(调用您的绘画代码),例如,如果窗口移动到组件前面。

在你的class上你应该:

 public void paint(Graphics g){ g.drawLine(x1, y1, x2, y2); } 

然后在代码中如果需要你将改变x1,y1,x2,y2并调用repaint();

我知道你正在使用Java AWT API进行绘图。 当控件需要重新绘制时,将调用paint方法。 而且我很确定它在Graphics参数中提供了哪个矩形是需要重新绘制的矩形(以避免重绘所有)。

因此,如果您要呈现固定图像,您只需在该方法中绘制所需的任何内容。

如果你是动画我假设你可以使某些区域无效,并且将自动调用paint方法。 所以你可以修改状态,调用invalidate,然后再次调用它。

您可以使用要绘制的组件的getGraphics方法。 这反过来应该允许您绘制线条并通过Graphics类创建其他可用的东西

给你一些想法:

 public void paint(Graphics g) { drawCoordinates(g); } private void drawCoordinates(Graphics g) { // get width & height here (w,h) // define grid width (dh, dv) for (int x = 0; i < w; i += dh) { g.drawLine(x, 0, x, h); } for (int y = 0; j < h; j += dv) { g.drawLine(0, y, w, y); } } 

我建立了一整套方法来绘制点,线,矩形,圆形等。我设计它将窗口视为一张方格纸,其中原点不必位于左上角,y值增加当你上去。 这是我绘制线条的方式:

 public static void drawLine (double x1, double y1, double x2, double y2) { ((Graphics2D)g).draw(new Line2D.Double(x0+x1*scale, y0-y1*scale, x0+x2*scale, y0-y2*scale)); } 

在上面的示例中, (x0, y0)表示屏幕坐标中的原点,而scale是比例因子。 输入参数将作为图形坐标提供,而不是屏幕坐标。 没有调用repaint() 。 您可以保存,直到您绘制了所需的所有行。

在我看来,有人可能不想用方格纸来思考:

  ((Graphics2D)g).draw(new Line2D.Double(x1, y1, x2, y2)); 

请注意Graphics2D的使用。 这允许我们使用双精度而不是整数来绘制Line2D对象。 除了其他形状,我的class级还支持3D透视绘图和几种便利方法(比如在给定半径的某一点绘制一个圆心。)如果有人感兴趣,我很乐意分享更多这个类。

 a simple line , after that you can see also a doted line import java.awt.*; import javax.swing.*; import java.awt.Graphics.*; import java.awt.Graphics2D.*; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.BasicStroke; import java.awt.Event.*; import java.awt.Component.*; import javax.swing.SwingUtilities; /** * * @author junaid */ public class JunaidLine extends JPanel{ //private Graphics Graphics; private void doDrawing(Graphics g){ Graphics2D g2d=(Graphics2D) g; float[] dash1 = {2f,0f,2f}; g2d.drawLine(20, 40, 250, 40); BasicStroke bs1 = new BasicStroke(1,BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND,1.0f,dash1,2f); g2d.setStroke(bs1); g2d.drawLine(20, 80, 250, 80); } @Override public void paintComponent(Graphics g){ super.paintComponent( g); doDrawing(g); } } class BasicStrokes extends JFrame{ public BasicStrokes(){ initUI(); } private void initUI(){ setTitle("line"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); add(new JunaidLine()); setSize(280,270); setLocationRelativeTo(null); } /** * @param args the command line arguments */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ @Override public void run(){ BasicStrokes bs = new BasicStrokes(); bs.setVisible(true); } }); } } 

要回答你的原始问题,它是(x1, y1)(x2, y2)

例如,

这是画一条水平线:

 g.drawLine( 10, 30, 90, 30 ); 

VS

这是绘制一条垂直线:

 g.drawLine( 10, 30, 10, 90 ); 

我希望它有所帮助。