简体   繁体   English

用Java绘制点

[英]Plotting points in java

Hey I am supposed to take in a list of cities and x and y coordinates and plot them. 嘿,我应该输入一个城市列表以及x和y坐标并将其绘制出来。 I have all the cities in as Vertexes with x and y coordinates. 我将所有城市作为具有x和y坐标的顶点。 Now I am trying to plot them but I can't seem to see what I am doing wrong and I am not getting an error. 现在,我正在尝试绘制它们,但似乎看不到自己在做错什么,也没有收到错误。 This is my first time using GUI so it could be a dumb mistake. 这是我第一次使用GUI,因此可能是一个愚蠢的错误。

 import javax.swing.JTextField;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import java.awt.geom.*;
 import java.util.Arrays;


 public class GraphMaker{


public GraphMaker(Vertex[] a )
  {
    JFrame frame = new JFrame();
    String start = "Start";
    int columns=20;
    String end = "End";
    JTextField startCity = new JTextField(start,columns);
    JTextField endCity = new JTextField(end,columns);
    JButton button = new JButton("Find Path");
    //button.addActionListener(button);

    int length = a.length;
    Vertex current = a[0];
    CityComponent cityPanel = new CityComponent(current);

    /*for(int i=0; i < length; i++){
        Vertex current = a[i];
        g2.draw(new Line2D.Double(x,y,x,y));
    }*/

    JPanel panel = new JPanel();

    panel.setLayout(new FlowLayout());

    panel.add(startCity);
    panel.add(endCity);
    panel.add(button);

    frame.setLayout(new BorderLayout());
    frame.add(cityPanel,BorderLayout.CENTER);
    frame.add(panel,BorderLayout.SOUTH);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);


}

 public void actionPerformed(ActionEvent e) {
        return;
 }
}

import javax.swing.*;
import java.awt.*;

public class CityComponent extends JComponent {

private Vertex m;
private int x = 0;
private int y = 0;

public CityComponent(Vertex m) {
   this.m = m;
}

public void paintComponent(Graphics g) {

    Graphics2D g2 = (Graphics2D)g;
    m.draw(g2);

}

} 


import java.awt.*;
import java.awt.geom.*;
import java.util.*;

public class Vertex{

public String element;
public Double x;
public Double y;


public Vertex(String city, String a, String b){
    this.element = city;
    this.x = Double.parseDouble(a);
    this.y = Double.parseDouble(b);

}

public void draw(Graphics2D g2){



Point2D.Double r1 = new Point2D.Double(x/10, y/10);
Line2D.Double line = new Line2D.Double(r1,r1);

  g2.draw(line);
}
}

You're trying to use a Graphics object that doesn't exist. 您正在尝试使用一个不存在的Graphics对象。 As I see it you have one of two choices to make here: 正如我所看到的,您可以在此处进行以下两种选择之一:

  • You could draw in the paintComponent(Graphics g) method of a class that extends JPanel, using a for loop to iterate through the vertices, and drawing with the Graphics object supplied by the JVM... 您可以绘制一个扩展JPanel的类的paintComponent(Graphics g)方法,使用for循环遍历顶点,并使用JVM提供的Graphics对象进行绘制...
  • Or you could draw in a BufferedImage, using the Graphics object you obtain by calling getGraphics() on the BufferedImage. 或者,您可以使用通过在BufferedImage上调用getGraphics()获得的Graphics对象来绘制BufferedImage。 You could then place the image into an ImageIcon, and then the Icon into a JLabel. 然后可以将图像放入ImageIcon,然后将图标放入JLabel。
  • Or you could do a combintation of the above by drawing the BufferedImage created above in a JPanel's paintComponent method. 或者,您可以通过在JPanel的paintComponent方法中绘制上面创建的BufferedImage来对上述内容进行组合。
  • Whatever you do, don't use a Graphics object obtained by calling getGraphics() on a Swing component. 无论做什么, 都不要使用通过在Swing组件上调用getGraphics()获得的Graphics对象。 You've been warned. 您已被警告。

Edit 编辑
I'm now seeing your CityComponent class which extends JComponent, and now see that you should be drawing with this. 我现在看到的是扩展JComponent的CityComponent类,现在应该使用此类进行绘制。 The key will be to pass the correct Vertex into it, something that I don't know if you're doing correctly, since we don't see how you construct your GraphMaker class. 关键是将正确的顶点传递到其中,这是我不知道您是否做得正确的事情,因为我们看不到如何构造GraphMaker类。

You might wish to tell us which code is yours, which was given to you, and also give us your exact requirements. 您可能希望告诉我们您的代码是什么,是给您的,还给我们您的确切要求。 Some of your code still seems a bit off. 您的某些代码似乎仍然有些偏离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM