Question

I am using a JGraphModelAdapter to convert a Jgraph to a JGrapht. Its working fine and my vertices are displayed on the gui as they should.

I have been given some base code to work with when editing the positions of the vertices on the GUI. (Seen Below)

private void positionVertices() {
    for (MapLocation aVertex : this.graphContents.vertexSet()) {
        double xPostion = (?);
        double yPostion = (?);
        this.positionAVertex(aVertex, xPostion, yPostion);
    }
}

private void positionAVertex(Object vertex, double x, double y) {
    DefaultGraphCell vertexDisplayCell = this.theModelAdapter
            .getVertexCell(vertex);
    AttributeMap cellAttributes = vertexDisplayCell.getAttributes();

    Rectangle2D bounds = GraphConstants.getBounds(cellAttributes);
    Rectangle2D newBounds = new Rectangle2D.Double(x, y, bounds.getWidth(),
            bounds.getHeight());

    GraphConstants.setBounds(cellAttributes, newBounds);

    HashMap<DefaultGraphCell, AttributeMap> cellAttributeMap = new HashMap<>();
    cellAttributeMap.put(vertexDisplayCell, cellAttributes);
    this.theModelAdapter.edit(cellAttributeMap, null, null, null);
}

Each MapLocation(Vertex) object represents a Zip code of a place in America, it has access to an accurate latitude and longitude getter. Using them I should be able to position the vertices at proportionally realistic places on my gui. However I am having difficulty determining what formula (Denoted by (?)) would work well in this situation.

The frame is set to MAXIMIZED_BOTH but must work on any monitor size.

Thanks for any assistance in advance.

Était-ce utile?

La solution 2

This is the code that work

 double xPostion = (124 - Math.abs(aVertex.getLongitude()));
        xPostion = xPostion / 57;           
        xPostion = xPostion * this.myWidth;
 double yPostion = 49 - aVertex.getLatitude();
        yPostion = yPostion / 25;
        yPostion = yPostion * this.myHeight ;

57 is difference in longitude from eastern most continental US and western most.

25 is the difference in latitude from the norther most continental US and the southern most

124 is the longitude of the western most continental US.

49 is the latitude of the norther most continental US.

Autres conseils

In light of your comments, I'll make the following assumptions.

  1. You're only going to display the 48 contiguous states, that is, no Alaska or Hawaii.
  2. You don't need the accuracy that would come with spherical trigonometry - treating North America as a flat rectangle is adequate.
  3. Your latitudes are given as a positive number of degrees north.
  4. Your longitudes are given as a positive number of degrees west (you may want to check this - I would expect this for data that was limited to USA, although common international usage is to have positive degrees for east and negative for west).
  5. The borders of the region that you want to display are 125 degrees and 65 degrees west, and 50 degrees and 25 degrees north.
  6. The area on your screen where the map is drawn goes from (left, top) to (left + width, top + height)

Given those assumptions, here's how to convert longitude and latitude to (x,y).

x = left + width * ( 125 - longitude ) / 60;
y = top + height * ( 50 - latitude ) / 25;

Note that if your longitudes are in fact negative for degrees west, you can just replace - with + in the expression for x.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top