Problem: solve the following Source of SHORTEST DISTANCE path of A,B,C,D,E,F,G.

I already did link the strings of dots of ABCDEFG and made a polygon which is the other objectives, my only problem now is calculating the source of the shortest distance part of A,B,C,D,E,F,G.

Am using net beans. Guys can you please help me? am very much confused.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author User
*/
public class paths{
}
import java.awt.Color;
import java.awt.Graphics;
import java.swing.*;

public class paths
{
private static void main{({String args[]}
{
myFrame g=newFame ()
coor = new int[][]{{300,400},{200,120},{170,50}{80,76}{143,100},{92,130},{55,50},{300,140}};
g.repaint();
}
static class myFrame extends JFrame
{
public myFrame()
{
setDefaultOperation(JFrame.EXIT_ON_CLOSE);
setSize(450,200);
setVisible(true);
}
public void paint(Grapics.g)
{
char a[]=new char[]{'a','b','c','d','e','f','g'};
g.setColor(Color.blue);
for(int i=0;<7;i++)
g.drawLine(coor[o],coor[1],coor[i+1][o],coor[i+1][1]);

for(int x=0;x<8;x++){
g.setColor(Color.RED);
g.fillOval(coor[x][o]-5,coor[x][1]-5,10,10);
g.setColor(Color.BLACK);ra
g.drawCharts(a,x,1 coor[x][o]-5,coor[x][1]-5);
}


Any help would be appreciated much! thank you!

Dani AI

Generated

There are two possible meanings of "shortest distance" here. One is the straight-line (Euclidean) distance between two points. The other — which matches the thread context and the lines already drawn — is the shortest path constrained to the segments already drawn (a weighted graph problem). Treat the dots as graph nodes and each drawn segment as an edge whose weight is the Euclidean length; then run a shortest-path algorithm (Dijkstra) from node A to node G.

Recommended steps

  • Map labels A..G to indices 0..6 (A=0, B=1, …, G=6).
  • Build a connection matrix (or adjacency lists) that marks which pairs of points are actually connected by a drawn segment.
  • Convert each connected pair to a weight = sqrt((dx)^2 + (dy)^2).
  • Run Dijkstra to get the shortest distance and the list of nodes in the shortest path.

Example Dijkstra helper (adapt and drop into a utility class):

import java.util.*;

public class ShortestPath {
    static double euclid(int[] a, int[] b){
        return Math.hypot(a[0]-b[0], a[1]-b[1]);
    }

    static double[][] buildWeights(int[][] coords, boolean[][] conn){
        int n = coords.length;
        double[][] w = new double[n][n];
        for(int i=0;i<n;i++){
            Arrays.fill(w[i], Double.POSITIVE_INFINITY);
            for(int j=0;j<n;j++){
                if(conn[i][j]) w[i][j] = euclid(coords[i], coords[j]);
            }
        }
        return w;
    }

    static List<Integer> dijkstra(double[][] w, int src, int target){
        int n = w.length;
        double[] dist = new double[n];
        int[] prev = new int[n];
        boolean[] seen = new boolean[n];
        Arrays.fill(dist, Double.POSITIVE_INFINITY);
        Arrays.fill(prev, -1);
        dist[src] = 0;
        class Node implements Comparable<Node>{
            int v; double d;
            Node(int v,double d){this.v=v;this.d=d;}
            public int compareTo(Node o){return Double.compare(d,o.d);}
        }
        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(new Node(src,0));
        while(!pq.isEmpty()){
            Node cur = pq.poll();
            int u = cur.v;
            if(seen[u]) continue;
            seen[u]=true;
            if(u==target) break;
            for(int v=0; v<n; v++){
                double wt = w[u][v];
                if(wt < Double.POSITIVE_INFINITY && dist[u] + wt < dist[v]){
                    dist[v] = dist[u] + wt;
                    prev[v] = u;
                    pq.add(new Node(v, dist[v]));
                }
            }
        }
        List<Integer> path = new ArrayList<>();
        if(prev[target]==-1 && src!=target) return path;
        for(int at=target; at!=-1; at=prev[at]) path.add(0, at);
        return path;
    }
}

Practical notes and fixes (drawn from earlier replies)

  • Address the syntax errors flagged: use public static void main(String[] args) and correct typos like coor[x][0] (zero) not letter o.
  • As hinted, store connectivity separately (boolean[][] or adjacency lists) rather than assuming every pair connects.
  • For Swing: do heavy calculations outside paint; override JPanel.paintComponent(Graphics) and call super.paintComponent(g); start the GUI on the Event Dispatch Thread (SwingUtilities.invokeLater). Compute the path once, then paint the path in a distinct color.

Recommended Answers

All 7 Replies

calculating the source of the shortest distance part of A,B,C,D,E,F,G.

Can you define how a distance is measured?
What do the letters A,B,C,D,E,F,G represent?

Please edit your code and wrap it in code tags. Use the [code] icon above the input box.

Your posted code is FULL of syntax errors.
Please correct it.

TPAM - ohhh sorry about that. Wait up. xD Just a newbie here.
Will edit the first post. Any type of measurement would be fine.

It just confuses me, the ABCDEFG is the points on which I used
to link the strings. This has been a pain in the neck. Thanks for the effort of replying sir.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author User
*/
public class paths{
}
import java.awt.Color;
import java.awt.Graphics;
import java.swing.*;

public class paths
{
private static void main{({String args[]}
{
myFrame g=newFame ()
coor = new int[][]{{300,400},{200,120},{170,50}{80,76}{143,100},{92,130},{55,50},{300,140}};
g.repaint();
}
static class myFrame extends JFrame
{
public myFrame()
{
setDefaultOperation(JFrame.EXIT_ON_CLOSE);
setSize(450,200);
setVisible(true);
}
public void paint(Grapics.g)
{
char a[]=new char[]{'a','b','c','d','e','f','g'};
g.setColor(Color.blue);
for(int i=0;<7;i++)
g.drawLine(coor[i][o],coor[i][1],coor[i+1][o],coor[i+1][1]);

for(int x=0;x<8;x++){
g.setColor(Color.RED);
g.fillOval(coor[x][o]-5,coor[x][1]-5,10,10);
g.setColor(Color.BLACK);ra
g.drawCharts(a,x,1 coor[x][o]-5,coor[x][1]-5);
}

The code you just posted looks to have the same error as the first post.

Does this code that you posted compile?

Your posted code is absolute junk!!!!

Sir I didn't mention that those were the fixed codes.
Please keep calm. I am a newbie. Still fixing the codes and thank you for replying.

to start with, you need to know the distances between your points and to keep track of the distances, you can keep them using the adjacency matrix. however remember that the adjacency matrix consumes memory as it also contains 0 where the is no direct route between points. i don't want to go much in details cause it will confuse you. try to search on how to use an adjacency matrix. it is an algorithm concept of graphs which focus much on vertices(nodes) and edges(lines from one nodes to the other) but i think you need it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.