简体   繁体   中英

Plotting points in java

Hey I am supposed to take in a list of cities and x and y coordinates and plot them. I have all the cities in as Vertexes with x and y coordinates. 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.

 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. 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...
  • Or you could draw in a BufferedImage, using the Graphics object you obtain by calling getGraphics() on the BufferedImage. You could then place the image into an ImageIcon, and then the Icon into a JLabel.
  • Or you could do a combintation of the above by drawing the BufferedImage created above in a JPanel's paintComponent method.
  • Whatever you do, don't use a Graphics object obtained by calling getGraphics() on a Swing component. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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