试图根据双打绘制线条,但没有显示?

时间:2023-02-09 15:33:31

Here's the class in which I try to draw the lines

这是我尝试绘制线条的类

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

The Graph.vMap.get(Graph.getEdges()[i].i2).x and Graph.vMap.get(Graph.getEdges()[i].i2).y access the x and y values for the endpoints of the lines and I've tested it and it returned the correct values. However, nothing shows up in my JFrame with this. Trying to draw other lines with set values outside of the for loop actually worked.

Graph.vMap.get(Graph.getEdges()[i] .i2).x和Graph.vMap.get(Graph.getEdges()[i] .i2).y访问端点的x和y值这些线和我测试了它,它返回了正确的值。但是,我的JFrame中没有显示任何内容。试图在for循环之外绘制具有设置值的其他行实际上有效。

1 个解决方案

#1


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

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

These y values are outside the panel. AWT/Swing component visible coordinate space runs from (0, 0) to (width-1, height-1).

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

Check where you are computing the values. If you want (0, 0) to be the center, you need to do some arithmetic or a translation via e.g. Graphics2D#translate(int, int).

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

Furthermore:

public void paintComponent(Graphics2D g)

If you are trying to override paintComponent, you have not done so. paintComponent takes a Graphics, not a Graphics2D:

如果您尝试覆盖paintComponent,则尚未执行此操作。 paintComponent采用Graphics而不是Graphics2D:

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

Always use the @Override annotation when you attempt to override, because it will cause an error and tell you if it's not an override. See https://docs.oracle.com/javase/tutorial/java/IandI/override.html.

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

Possibly you mean to use something like this:

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

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

#1


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

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

These y values are outside the panel. AWT/Swing component visible coordinate space runs from (0, 0) to (width-1, height-1).

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

Check where you are computing the values. If you want (0, 0) to be the center, you need to do some arithmetic or a translation via e.g. Graphics2D#translate(int, int).

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

Furthermore:

public void paintComponent(Graphics2D g)

If you are trying to override paintComponent, you have not done so. paintComponent takes a Graphics, not a Graphics2D:

如果您尝试覆盖paintComponent,则尚未执行此操作。 paintComponent采用Graphics而不是Graphics2D:

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

Always use the @Override annotation when you attempt to override, because it will cause an error and tell you if it's not an override. See https://docs.oracle.com/javase/tutorial/java/IandI/override.html.

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

Possibly you mean to use something like this:

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

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