When I try to add the panel into my frame, I get no results (hence I've tried making the panel's background to be red). Also, whenever I call on my linkedList in my Vertex class, I get an error saying: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. My main issue right now is with the panel.
Draw Panel class
@SuppressWarnings("serial")
public class DrawPanel extends JPanel {
Project4Test test = new Project4Test();
Graph graph;
Vertex v;
public DrawPanel() {
setBackground(Color.red);
setPreferredSize(new Dimension(500,500));
}
public void paintComponent(Graphics page) {
Graphics2D page2 = (Graphics2D) page;
page2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
page2.setColor(Color.black);
Iterator iterator = test.hashEdge.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry dualEntry = (Map.Entry)iterator.next();
Edge e = (Edge) dualEntry.getValue();
drawEdge(page2, e.vertex1, e.vertex2);
}
ListIterator<Vertex> listIterator = v.linkedList.listIterator();
while(listIterator.hasNext()) {
drawEdge(page2, v, listIterator.next());
}
}
public void drawEdge(Graphics2D page2, Vertex vertex1, Vertex vertex2) {
//x1, y1, x2, y2
page2.setColor(Color.black);
page2.draw(new Line2D.Double(vertex1.latitude, vertex1.longitude, vertex2.latitude, vertex2.longitude));
}
}
Test Class
@SuppressWarnings("serial")
public class Project4Test {
static int numVertices = 0;
static Map<String, Vertex> hashVertex = new HashMap<String, Vertex>();
static Map<String, Edge> hashEdge = new HashMap<String, Edge>();
//Map with String intersectionID as the key, and Vertexes as the value
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) {
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
DrawPanel drawpanel = new DrawPanel();
frame.setSize(660, 630);
frame.add(drawpanel);
//frame.setExtendedState(Frame.MAXIMIZED_BOTH);
File inFile = new File(args[0]);
//frame.setExtendedState(Frame.MAXIMIZED_BOTH);
//DrawPanel drawpanel;
//frame.setVisible(true);
try {
Scanner scan = new Scanner(inFile);
Graph newGraph = new Graph(0, false);
while(scan.hasNext()) {
String temp = scan.nextLine();
String[] info = temp.split("\\t", 4);
String interRoad = info[0];
//If it is an intersection
if(info[0].equalsIgnoreCase("i")) {
String vertexID = info[1];
double x = Double.parseDouble(info[2]);
double y = Double.parseDouble(info[3]);
//System.out.println(interRoad + " " + vertexID + " " + x + " " + y);
//Insert to vertex here
Vertex tempVertex = new Vertex(vertexID, numVertices, x , y);
hashVertex.put(vertexID, tempVertex);
numVertices++;
}//end of intersection
//Update graph to have the number of vertices
newGraph = new Graph(numVertices, false);
//If it is a road
if(info[0].equalsIgnoreCase("r")) {
String roadID = info[1];
String vertexID1 = info[2];
String vertexID2 = info[3];
Vertex vertex1 = hashVertex.get(vertexID1);
Vertex vertex2 = hashVertex.get(vertexID2);
//System.out.println(interRoad + " " + roadID + " " + vertexID1 + " " + vertexID2);
Edge newEdge = new Edge(roadID, vertex1, vertex2);
hashEdge.put(roadID, newEdge);
//Essentially saying linkedList(v1).add(v2) because if it's a road, it's inherently connected
vertex1.linkedList.add(vertex2);
vertex2.linkedList.add(vertex1);
//drawpanel = new DrawPanel(newGraph);
frame.add(drawpanel);
}
}//end of while loop
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} //end of main
}