Rendering too resource-intensive

I am using Swing to render a graph in a window; however rendering is taking too long and is too resource-intensive. What changes can I make to help with performance? Here is the class responsible for rendering my graph:
public class GraphPanel extends JPanel {
private Graph graph;
private PathfindAlgorithm algo;


public GraphPanel(Graph graph, PathfindAlgorithm pathfindAlgorithm) {
this.setBackground(Color.black);
setGraph(graph);
setPathAlgo(pathfindAlgorithm);
}

public void setPathAlgo(PathfindAlgorithm algo) {
this.algo = algo;
}

public void setGraph(Graph graph) {
this.graph = graph;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
renderConnectedEdges(g);
renderExploredEdges(g);
renderPath(g);
renderStartEndNodes(g);
}

private void renderStartEndNodes(Graphics g) {
if (algo.getStartNode() != null && algo.getEndNode() != null) {
int width = 10, height = 10;

Point2D p1 = calculatePixelPos(new Point2D.Double(algo.getStartNode().latitude, algo.getStartNode().longitude));
Point2D p2 = calculatePixelPos(new Point2D.Double(algo.getEndNode().latitude, algo.getEndNode().longitude));

g.setColor(Color.GREEN);
g.fillOval((int) p1.getX() - width / 2, (int) p1.getY() - height / 2, width, height);

g.setColor(Color.BLUE);
g.fillOval((int) p2.getX()- width / 2, (int) p2.getY() - height / 2, width, height);
}
}

private void renderConnectedEdges(Graphics g) {
g.setColor(Color.YELLOW);
graph.nodes.values().forEach(node -> {
node.adjacentEdges.forEach(edge -> {
Point2D p1 = calculatePixelPos(new Point2D.Double(edge.nodeA.latitude, edge.nodeA.longitude));
Point2D p2 = calculatePixelPos(new Point2D.Double(edge.nodeB.latitude, edge.nodeB.longitude));

g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
});
});
}


private void renderExploredEdges(Graphics g) {}
private void renderPath(Graphics g) {}

private Point2D calculatePixelPos(Point2D point) {
int panelWidth = getWidth();
int panelHeight = getHeight();

double minLat = graph.nodes.values().stream().mapToDouble(n -> n.latitude).min().orElse(0);
double maxLat = graph.nodes.values().stream().mapToDouble(n -> n.latitude).max().orElse(1);
double minLon = graph.nodes.values().stream().mapToDouble(n -> n.longitude).min().orElse(0);
double maxLon = graph.nodes.values().stream().mapToDouble(n -> n.longitude).max().orElse(1);

double x = ((point.getY() - minLon) / (maxLon - minLon)) * (panelWidth - 40) + 20;
double y = (1 - (point.getX() - minLat) / (maxLat - minLat)) * (panelHeight - 40) + 20;

return new Point2D.Double(x, y);
}

public void updateGraphView() {
repaint();
}
}
public class GraphPanel extends JPanel {
private Graph graph;
private PathfindAlgorithm algo;


public GraphPanel(Graph graph, PathfindAlgorithm pathfindAlgorithm) {
this.setBackground(Color.black);
setGraph(graph);
setPathAlgo(pathfindAlgorithm);
}

public void setPathAlgo(PathfindAlgorithm algo) {
this.algo = algo;
}

public void setGraph(Graph graph) {
this.graph = graph;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
renderConnectedEdges(g);
renderExploredEdges(g);
renderPath(g);
renderStartEndNodes(g);
}

private void renderStartEndNodes(Graphics g) {
if (algo.getStartNode() != null && algo.getEndNode() != null) {
int width = 10, height = 10;

Point2D p1 = calculatePixelPos(new Point2D.Double(algo.getStartNode().latitude, algo.getStartNode().longitude));
Point2D p2 = calculatePixelPos(new Point2D.Double(algo.getEndNode().latitude, algo.getEndNode().longitude));

g.setColor(Color.GREEN);
g.fillOval((int) p1.getX() - width / 2, (int) p1.getY() - height / 2, width, height);

g.setColor(Color.BLUE);
g.fillOval((int) p2.getX()- width / 2, (int) p2.getY() - height / 2, width, height);
}
}

private void renderConnectedEdges(Graphics g) {
g.setColor(Color.YELLOW);
graph.nodes.values().forEach(node -> {
node.adjacentEdges.forEach(edge -> {
Point2D p1 = calculatePixelPos(new Point2D.Double(edge.nodeA.latitude, edge.nodeA.longitude));
Point2D p2 = calculatePixelPos(new Point2D.Double(edge.nodeB.latitude, edge.nodeB.longitude));

g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
});
});
}


private void renderExploredEdges(Graphics g) {}
private void renderPath(Graphics g) {}

private Point2D calculatePixelPos(Point2D point) {
int panelWidth = getWidth();
int panelHeight = getHeight();

double minLat = graph.nodes.values().stream().mapToDouble(n -> n.latitude).min().orElse(0);
double maxLat = graph.nodes.values().stream().mapToDouble(n -> n.latitude).max().orElse(1);
double minLon = graph.nodes.values().stream().mapToDouble(n -> n.longitude).min().orElse(0);
double maxLon = graph.nodes.values().stream().mapToDouble(n -> n.longitude).max().orElse(1);

double x = ((point.getY() - minLon) / (maxLon - minLon)) * (panelWidth - 40) + 20;
double y = (1 - (point.getX() - minLat) / (maxLat - minLat)) * (panelHeight - 40) + 20;

return new Point2D.Double(x, y);
}

public void updateGraphView() {
repaint();
}
}
No description
10 Replies
JavaBot
JavaBot2d ago
This post has been reserved for your question.
Hey @Danish Pirate! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
danishpirate
danishpirateOP2d ago
updateGraphView() is called whenever the nodes in my graph are connected upon initalization and whenever a node is visited.
dan1st
dan1st2d ago
How exactly do you see it taking too long?
danishpirate
danishpirateOP2d ago
I need to wait a couple of seconds for anything to be rendered at first. Then my window freezes I optimized it a bit by caching minLat, maxLat, minLon, maxLon instead of calculating them every view update.
dan1st
dan1st2d ago
so the rendercomponent takes too much time? I didn't know you'd need to iterate over all nodes there
danishpirate
danishpirateOPthis hour
I need to redraw all the edges between the nodes every view update.
dan1st
dan1stthis hour
can you check what actually takes too long?
danishpirate
danishpirateOPthis hour
Seems to be the iterating over the nodes
No description
dan1st
dan1st23h ago
So you are iterating over all nodes/edges and computing min/max for lat/lon fir each of them Why? just compute these once per rendering
JavaBot
JavaBot21h ago
Post Closed
This post has been closed by <@173447330719072258>.

Did you find this page helpful?