尝试根据双打画线但没有任何显示?

Trying to draw lines based on doubles but nothing shows?

这是我尝试画线的 class

package gps;
import java.awt.*;
import java.awt.geom.Line2D;
import java.util.*;

import javax.swing.*;

public class RoadMap extends JPanel {

    public void paintComponent(Graphics2D g)
    {
        super.paintComponent(g);
        g.setColor(Color.blue);

        for(int i = 0; i < Graph.getEdges().length; i++)
        {   
            Shape s = new Line2D.Double(Graph.vMap.get(Graph.getEdges()[i].i1).x,
                    Graph.vMap.get(Graph.getEdges()[i].i1).y,
                    Graph.vMap.get(Graph.getEdges()[i].i2).x,
                    Graph.vMap.get(Graph.getEdges()[i].i2).y);

            g.draw(s);
        }   
    }   
}

Graph.vMap.get(Graph.getEdges()[i].i2).xGraph.vMap.get(Graph.getEdges()[i].i2).y 访问线端点的 xy 值,我已经对其进行了测试并返回了正确的值。但是,我的 JFrame 中没有显示任何内容。尝试使用 for 循环之外的设置值绘制其他线条实际上有效。

x1 = 43.12929, x2 = 43.12976, y1 = -77.626956, y2 = -77.62679

这些 y 值在面板之外。 AWT/Swing 组件可见坐标 space 从 (0, 0)(width-1, height-1)

检查您计算值的位置。如果你想让 (0, 0) 成为中心,你需要做一些算术或翻译,例如Graphics2D#translate(int, int).

此外:

public void paintComponent(Graphics2D g)

如果您试图覆盖 paintComponent,那么您还没有这样做。 paintComponent 需要 Graphics,而不是 Graphics2D:

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

当您尝试覆盖时始终使用 @Override 注释,因为它会导致错误并告诉您它是否不是覆盖。参见 https://docs.oracle.com/javase/tutorial/java/IandI/override.html

可能你的意思是使用这样的东西:

public class RoadMap extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g.create();
        g2.translate(getWidth() / 2, getHeight() / 2);

        g2.setColor(Color.blue);

        for(int i = 0; i < Graph.getEdges().length; i++) {   
            Shape s = new Line2D.Double(
                Graph.vMap.get(Graph.getEdges()[i].i1).x,
                Graph.vMap.get(Graph.getEdges()[i].i1).y,
                Graph.vMap.get(Graph.getEdges()[i].i2).x,
                Graph.vMap.get(Graph.getEdges()[i].i2).y);

            g2.draw(s);
        }

        g2.dispose();
    }
}