Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中绘制线条_Java_Coordinates_Coordinate Systems - Fatal编程技术网

如何在Java中绘制线条

如何在Java中绘制线条,java,coordinates,coordinate-systems,Java,Coordinates,Coordinate Systems,我想知道Java中是否有一个函数可以从坐标(x1,x2)到(y1,y2)画一条线 我想做的是这样的事情: drawLine(x1, x2, x3, x4); 我希望能够在代码中的任何时候这样做,同时显示几行 我尝试过这样做: public void paint(Graphics g){ g.drawLine(0, 0, 100, 100); } 但这让我无法控制何时使用该函数,我也不知道如何多次调用它 希望你明白我的意思 另外,我想创建一个包含许多坐标的坐标系。在你的课堂上,你应该有:

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

我想做的是这样的事情:

drawLine(x1, x2, x3, x4);
我希望能够在代码中的任何时候这样做,同时显示几行

我尝试过这样做:

public void paint(Graphics g){
   g.drawLine(0, 0, 100, 100);
}
但这让我无法控制何时使用该函数,我也不知道如何多次调用它

希望你明白我的意思


另外,我想创建一个包含许多坐标的坐标系。

在你的课堂上,你应该有:

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

然后在代码中,如果需要,您将更改x1、y1、x2、y2并调用
repaint()

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

所以,如果你展示的是一个固定的图像,你只需要在这个方法中画出你需要的任何东西

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

您可以使用要在其上绘制的组件的方法。这反过来应该可以让你画线,并通过课堂提供其他东西,给你一些想法:

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);
  }
}
public void绘制(图形g){
坐标(g);
}
专用坐标(图形g){
//在此处获取宽度和高度(w,h)
//定义栅格宽度(dh、dv)
对于(int x=0;i
您需要创建一个扩展组件的类。在那里,您可以覆盖绘制方法并将绘制代码放入:

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提到的,绘制代码实际上告诉系统组件的外观。当系统认为需要(重新)绘制时,系统会询问此信息(调用绘制代码),例如,如果窗口在组件前面移动。

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

该类还包括一些用法示例

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<Line> lines = new LinkedList<Line>();

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);
}

}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.LinkedList;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类LinesComponent扩展JCComponent{
私有静态类行{
最终int-x1;
最终int y1;
最终int x2;
最终int y2;
最终颜色;
公用线路(int x1、int y1、int x2、int y2、彩色){
这是1.x1=x1;
这是1.y1=y1;
这是0.x2=x2;
这1.y2=y2;
这个颜色=颜色;
}               
}
私有最终LinkedList行=新LinkedList();
公共无效添加行(int x1、int x2、int x3、int x4){
地址行(x1、x2、x3、x4,颜色为黑色);
}
公共无效添加线(int-x1、int-x2、int-x3、int-x4、彩色){
添加(新行(x1、x2、x3、x4、颜色));
重新油漆();
}
公共无效清除线(){
行。清除();
重新油漆();
}
@凌驾
受保护组件(图形g){
超级组件(g);
用于(行:行){
g、 setColor(line.color);
g、 抽绳(线x1、线y1、线x2、线y2);
}
}
公共静态void main(字符串[]args){
JFrame testFrame=新JFrame();
testFrame.setDefaultCloseOperation(JFrame.DISPOSE\u ON\u CLOSE);
最终LinesComponent组件=新的LinesComponent();
组件设置首选尺寸(新尺寸(320200));
testFrame.getContentPane().add(comp,BorderLayout.CENTER);
JPanel buttonPanel=新的JPanel();
JButton newLineButton=新JButton(“新行”);
JButton clearButton=新JButton(“清除”);
按钮面板添加(newLineButton);
按钮面板添加(clearButton);
testFrame.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
newLineButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
int x1=(int)(Math.random()*320);
int x2=(int)(Math.random()*320);
inty1=(int)(Math.random()*200);
int y2=(int)(Math.random()*200);
Color randomColor=新颜色((float)Math.random(),(float)Math.random(),(float)Math.random(),(float)Math.random());
复合添加线(x1,y1,x2,y2,随机颜色);
}
});
clearButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
comp.clearLines();
}
});
testFrame.pack();
testFrame.setVisible(true);
}
}

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

屏幕截图

抽绳

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<Line2D.Double> lines;
    Random random;

    LineComponent(int width, int height) {
        super();
        setPreferredSize(new Dimension(width,height));
        lines = new ArrayList<Line2D.Double>();
        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) {
        super.paintComponent(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()
                );
        }
    }
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.geom.Line2D;
导入javax.swing.JOptionPane;
导入javax.swing.JComponent;
导入javax.swing.SwingUtilities;
导入java.util.ArrayList;
导入java.util.Random;
类抽绳{
公共静态void main(字符串[]args){
Runnable r=新的Runnable(){
公开募捐{
LineComponent LineComponent=新的LineComponent(400400);

对于(intii=0;iiI),我构建了一个完整的方法类来
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));
}
    ((Graphics2D)g).draw(new Line2D.Double(x1, y1, x2, y2));
g.drawLine( 10, 30, 90, 30 );
g.drawLine( 10, 30, 10, 90 );
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);

}                

});

}


}