【问题标题】:How can I display large coordinates within the coordinatesystem?如何在坐标系中显示大坐标?
【发布时间】:2018-07-14 09:57:43
【问题描述】:

我想从给定的 wkt 文件中绘制一些点,但由于它们的接近性,我只能显示重叠的椭圆堆。

这些点仅在小数位上有所不同:

  POINT (3346349.958 5642197.806)
  POINT (3346349.313 5642199.622)
  POINT (3346349.237 5642201.918)
  POINT (3346349.734 5642204.058)
  POINT (3346351.746 5642205.777)
  POINT (3346351.636 5642210.304)
  POINT (3346349.335 5642216.518)
  POINT (3346347.326 5642221.15)
  POINT (3346347.365 5642223.671)
  POINT (3346351.577 5642195.711)
  etc...

首先我尝试绘制点,但正如我所提到的,所有点似乎都显示在同一个地方。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import com.vividsolutions.jts.io.ParseException;

public class Display extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int width;
    private int height;
    private WKTGrabsteine p = new WKTGrabsteine();

    public Display() {
        setLayout(null);
        width = 0;
        height = 0;
        repaint();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.WHITE);

        Graphics2D g2 = (Graphics2D) g;

        g2.translate(height, width);

        try {
            for (int point = 0; point < p.geoCoordinates().size(); point++) {
                Ellipse2D shape = new Ellipse2D.Double(p.geoCoordinates().get(point).getX() / 1000000 + 400,
                        p.geoCoordinates().get(point).getY() / 1000000 + 100, 5, 5);

                g2.draw(shape);
            }

        } catch (IOException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

但是我认为解决方案可能在于重新调整坐标系,我尝试通过 Affinetransform 和 .scale() 进行转换

 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Ellipse2D;
 import java.awt.geom.Line2D;
 import java.awt.geom.Path2D;
 import java.awt.geom.Point2D;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import javax.swing.JPanel;
 import com.vividsolutions.jts.io.ParseException;

 public class Display extends JPanel {
 /**
 *
 */
 private static final long serialVersionUID = 1L;
 private int width;
 private int height;
 private WKTGrabsteine p = new WKTGrabsteine();

 public Display() {
 setLayout(null);
 width = 0;
 height = 0;
 repaint();
 }

 public void paintComponent(Graphics g) {
 super.paintComponent(g);
 setBackground(Color.WHITE);

 Graphics2D g2 = (Graphics2D) g;
 AffineTransform at = new AffineTransform();
 g2.translate(height, width);
 at.scale(6000000, 6000000);

 try {
 for (int point = 0; point < p.geoCoordinates().size(); point++) {
 Ellipse2D shape = new Ellipse2D.Double(p.geoCoordinates().get(point).getX(),
 p.geoCoordinates().get(point).getY(), 10, 10);

 g2.transform(at);
 g2.draw(shape);
 }

 } catch (IOException | ParseException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }



 }

 }

事实上,我对这个话题很陌生,不知道如何让所有点都可见。如果有人可以帮助我,那就太好了。 非常感谢

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。硬编码一些示例坐标以消除对外部文件和第 3 方 API 的需求。 2)setLayout(null);自定义绘画时这是不必要的,否则是有害的。
  • @Andrew Thompson 现在可以完美运行非常感谢您的帮助!我会在以后的帖子中考虑您的提示

标签: java swing coordinate-transformation affinetransform wkt


【解决方案1】:

结合使用缩放和平移仿射变换。

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class PointPlotter {

    Point2D.Double[] points = {
        new Point2D.Double(3346349.958, 5642197.806),
        new Point2D.Double(3346349.313, 5642199.622),
        new Point2D.Double(3346349.237, 5642201.918),
        new Point2D.Double(3346349.734, 5642204.058),
        new Point2D.Double(3346351.746, 5642205.777),
        new Point2D.Double(3346351.636, 5642210.304),
        new Point2D.Double(3346349.335, 5642216.518),
        new Point2D.Double(3346347.326, 5642221.15),
        new Point2D.Double(3346347.365, 5642223.671),
        new Point2D.Double(3346351.577, 5642195.711)
    };
    public static int SZ = 400;
    BufferedImage image = new BufferedImage(SZ, SZ, BufferedImage.TYPE_INT_RGB);

    private JComponent ui = null;

    PointPlotter() {
        initUI();
    }

    private void drawImage() {
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, SZ, SZ);

        Area area = new Area();
        double r = 0.3;
        for (Point2D.Double point : points) {
            Ellipse2D.Double e = new Ellipse2D.Double(
                    point.getX() - r, point.getY() - r, 2*r, 2*r);
            area.add(new Area(e));
        }
        Rectangle2D rect = area.getBounds2D();
        double w = rect.getWidth();
        double h = rect.getHeight();
        double max = w>h ? w : h;
        double s = SZ/max;
        AffineTransform scale = AffineTransform.getScaleInstance(s, s);
        double tX = -rect.getMinX();
        double tY = -rect.getMinY();
        AffineTransform translate = AffineTransform.getTranslateInstance(tX, tY);

        AffineTransform transform = scale;
        transform.concatenate(translate);

        area = new Area(transform.createTransformedShape(area));

        g.setColor(Color.RED);
        g.draw(area);

        g.dispose();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }
        drawImage();

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        ui.add(new JLabel(new ImageIcon(image)));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                PointPlotter o = new PointPlotter();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
相关资源
最近更新 更多