如何向每个坐标点添加文本标签

时间:2018-07-19 13:53:56

标签: java swing visualization graphics2d

谁能告诉我如何添加文本字符串     每一点?几何代表墓碑,位于     在犹太墓地上并从wktFile中提取。

我尝试过     g.drawString()并将x / y坐标设置为参数,但是我有     不知道我怎么能把它们变成正确的形状。不分     它们,它们是不可见的,而使其变小则使其出现     在同一位置,导致缩放错误。顺便说一句     卡在这个项目中。我有一个坐标为102的wktFile     墓碑和其他信息,例如日期/人/史诗     文本等。

目标是在     Java为了对给定的信息进行一些分析,并且     可视化一些查询。启动程序后,用户     应该将所有墓碑视为重点。之后,用户应     能够过滤(按按钮和搜索字段)。

  • 例如突出显示所有 女后裔
  • 例如突出显示公元1223年之前的所有墓碑。
  • 例如用“ Halevi”等名称突出显示所有后代。

有没有     Java中实现此项目的更好方法?

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

public class PointPlotter2 {

    PointLoader pl = new PointLoader();

    public List<Point2D> points = pl.getPoints();
    public static int SZ = 400;

    BufferedImage image = new BufferedImage(SZ, SZ, 
    BufferedImage.TYPE_INT_RGB);

    private JComponent ui = null;

    PointPlotter2() {

        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.2;

        for (Point2D point : points) {

            Ellipse2D.Double e = new Ellipse2D.Double(point.getX() - r, 
            point.getY() - r, 2 * r, 2 * r);

            g.drawString("kicher", (float) e.getCenterX() / 100000, (float) 
            e.getCenterY() / 100000);

            g.setColor(Color.BLUE);
            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.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) {

                }

                PointPlotter2 o = new PointPlotter2();

                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);

    }

}

//PointLoader
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTFileReader;
import com.vividsolutions.jts.io.WKTReader;

public class PointLoader {

    List<Point2D> getPoints() {

        File file = new 
File("C:\\Users\\s7a38865\\Desktop\\geoCoordinates.wkt");

        WKTReader r = new WKTReader();

        WKTFileReader fr = new WKTFileReader(file, r);

        List<Geometry> geometries = null;

        try {

            geometries = fr.read();

        } catch (IOException | ParseException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        }

        List<Point2D> pointList = new ArrayList<>();

        for (Geometry g : geometries) {

            Coordinate[] c = g.getGeometryN(0).getCoordinates();

            CoordinateArraySequence s = new CoordinateArraySequence(c);

            for (int point = 0; point < g.getNumGeometries(); point++) {

                double posX = s.getOrdinate(point, 0);

                double posY = s.getOrdinate(point, 1);

                Point2D tmpPoint = new Point2D.Double(posX, posY);

                pointList.add(tmpPoint);
            }
        }
        return pointList;
    }
}

0 个答案:

没有答案