简体   繁体   English

将html响应图像放入JFrame

[英]Put html response image in JFrame

I have the following html that i would like to use to display a google streetview. 我想使用以下HTML来显示Google Streetview。 The HTML was been tested and works to return a streetview however I am not sure how to set the %%lat%% and %%long%% wildcard values and I am not sure how to retrieve the streetview and put it in a JFrame or JOptionPane . HTML已经过测试,可以返回街景,但是我不确定如何设置%% lat %%和%% long %%通配符值,也不确定如何检索街景并将其放入JFrameJOptionPane HTML as follows: HTML如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
    function initialize() {
        var markerPosition = new google.maps.LatLng(%%lat%%, %%lon%%);
        var panoramaOptions = {
            position: markerPosition,
            pov: {
                heading: 165,
                pitch: 0,
                zoom: 1
            }
        };
        var myPano = new google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);
        myPano.setVisible(false);
        new google.maps.Marker({map: myPano, position: markerPosition, title: 'Feature'});

        var changeImage = function(){
            var image = "https://maps.googleapis.com/maps/api/streetview?size=600x300&location=44.414382,11.013988&heading=151.78&pitch=-0.76";
            document.getElementById("pano").innerHTML = "<img src='" + image + "' style='width:100%;height:100%'>"; 
        }
        // add a variable that gets set when the position_changed handler gets fired off
        var positionDidChange = false;
        var newPov = {};
        var listenerHandle = google.maps.event.addListener(myPano, 'position_changed', function () {
            positionDidChange = true;
            google.maps.event.removeListener(listenerHandle);
            newPov.heading = google.maps.geometry.spherical.computeHeading(myPano.getPosition(), markerPosition);
            newPov.pitch = 0;
            newPov.zoom = 1;
            myPano.setPov(newPov); myPano.setVisible(true);
        });

        // add a function that gets fired off to see if the position did change so that the user does not wait forever
        setTimeout(function () {
            if (!positionDidChange) {
                changeImage();
            }
        }, 5000);
    }
</script>
</head>
<body onload="initialize()">
<div id="pano" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0;      text-align: center"> LOADING STREET VIEW...</div>
</body>
</html>

that I would like to get the response from when I am in the following method: 我想从以下方法获得响应:

    @Override
protected void onMouseDown(MouseButton button,
                           KeyModifier keyModifier,
                           int mouseX,
                           int mouseY) {
    try {
        if (MouseButton.LEFT.equals(button)) {

            FeatureWrapper closestStationOrSpanFw = SelectionUtil.getClosestSpanOrStation(toMapPoint(mouseX, mouseY));
            if (closestStationOrSpanFw != null) {
                IGeometry shape = closestStationOrSpanFw.getIFeature().getShapeCopy();
                IPoint point = null;
                if (shape instanceof IPoint) {
                    point = (IPoint) shape;
                    double lat = point.getY();
                    double lon = point.getX();
                    ....

I don't think you'll have much success getting your HTML to work in Swing, it's HTML/web support is pretty dated. 我认为让HTML在Swing中运行不会取得很大的成功,因为HTML / Web支持已经过时。 A solution might be to use the WebView from Java-FX. 一个解决方案可能是使用Java-FX中的WebView

However, if all you want to do is display the image, you can do something like... 但是,如果您只想显示图像,则可以执行以下操作:

街景

    try {
        URL url = new URL("https://maps.googleapis.com/maps/api/streetview?size=400x400&location=40.720032,-73.988354&fov=90&heading=235&pitch=10");
        try (InputStream is = url.openStream()) {
            BufferedImage img = ImageIO.read(is);
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));
        }
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

Now, I might be temptered to write a method which takes the parameters and generates the URL 现在,我可能很想写一个采用参数并生成URL

protected static final String BASE_URL = "https://maps.googleapis.com/maps/api/streetview?size=400x400&";

public BufferedImage getStreetView(double lat, double log, double fov, double heading, double pitch) throws IOException {

    StringJoiner sj = new StringJoiner("&");
    sj.add(BASE_URL);
    sj.add("location=" + Double.toString(lat) + "," + Double.toString(log));
    sj.add("fov=" + Double.toString(fov));
    sj.add("heading=" + Double.toString(heading));
    sj.add("pitch=" + Double.toString(pitch));

    BufferedImage img = null;
    URL url = new URL(sj.toString());
    try (InputStream is = url.openStream()) {
        img = ImageIO.read(is);
    }

    return img;
}

Or better, some kind of builder 或者更好的是某种建造者

Then you could just call it using something like... 然后,您可以使用类似...的名称来调用它

BufferedImage img = getStreetView(40.720032, -73.988354, 90d, 235d, 10d);
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM