放大时如何实现resize元素

时间:2017-02-10 18:00:33

标签: java

我已经使用Java Swing实现了一个JPanel来可视化地图。地图包含节点和链接。我想在缩放时调整节点/链接的大小。

public class MainJPanel extends javax.swing.JPanel {

private List<Station> stations;
private List<Node> micro_nodes;
private List<Link> micro_links;

private double scale = 1.0;
private double translateX;
private double translateY;
private int referenceX;
private int referenceY;

public MainJPanel() {
    initComponents();  
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
    Graphics2D g2d = (Graphics2D) g;
    /*Zooming*/
    zooming(g2d);

    AffineTransform saveTransform = g2d.getTransform();
    AffineTransform at = new AffineTransform(saveTransform);


    /*The panning transformation*/
    at.translate(translateX, translateY);
    g2d.setTransform(at);


    drawLayer(g2d, stations, null, Color.BLUE, null, at);
    drawLayer(g2d, micro_nodes, micro_links, Color.GREEN, Color.GRAY, at);

    g2d.setTransform(saveTransform);

}

private void zooming(Graphics2D g2d) {
    int w = getWidth();
    int h = getHeight();

    g2d.translate(w / 2, h / 2);
    g2d.scale(scale, scale);
    g2d.translate(-w / 2, -h / 2);
}

private void drawLayer(Graphics2D g2d,  List nodes, List<Link> edges, Color nodeColor, Color edgeColor, AffineTransform at) {

        if (edges != null) {
            for (Link edge : edges) {
                g2d.setColor(edgeColor);
                drawEdge(g2d, edge.getCoord(), at);
            }
        }
        for (Object node : nodes) {
            g2d.setColor(nodeColor);
            drawNode(g2d, node, at);
        }
}

private void drawEdge(Graphics2D g2d, double coord[], AffineTransform at) {

    Point2D startCoord = CoordinateConvertor.convertLatLongToCoord(new Point2D.Double(coord[0], coord[1]), getWidth(), getHeight());
    Point2D endCoord = CoordinateConvertor.convertLatLongToCoord(new Point2D.Double(coord[2], coord[3]), getWidth(), getHeight());

    Line2D.Double l = new Line2D.Double(startCoord.getX(), startCoord.getY(), endCoord.getX(), endCoord.getY());
    Shape transformedShape = at.createTransformedShape(l);
    g2d.draw(transformedShape);

}

private void drawNode(Graphics2D g2d, Object node, AffineTransform at) {
    Point2D p2d = null;
    if (node instanceof Station) {
        p2d = CoordinateConvertor.convertLatLongToCoord(((Station) node).getCoordinates(), getWidth(), getHeight());
    }
    if (node instanceof Node) {
        p2d = CoordinateConvertor.convertLatLongToCoord(((Node) node).getCoordinates(), getWidth(), getHeight());
    }
    if (p2d != null) {
        Ellipse2D.Double e = new Ellipse2D.Double(p2d.getX() - 2, p2d.getY() - 2, 4, 4);
        Shape transformedShape = at.createTransformedShape(e);
        g2d.draw(transformedShape);

    }
}

public static Point2D convertLatLongToCoord(Point2D coordinate, int width, int height) {
    double xPixel = Math.abs((MAX_RIGHT - coordinate.getX()) / (MAX_RIGHT - MAX_LEFT) * width - width);
    double yPixel = Math.abs(((MAX_DOWN - coordinate.getY())) / (MAX_DOWN - MAX_TOP) * height - height);
    return new Point2D.Double(xPixel, yPixel);
}

这是原始地图:

Map

这是放大后的地图:

Zoom Map

如何在缩放时调整节点/链接的大小?这就是我想要的:

What I want

0 个答案:

没有答案