试图根据双打画线,但没有显示? [英] Trying to draw lines based on doubles but nothing shows?

查看:78
本文介绍了试图根据双打画线,但没有显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试画线的课程

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循环之外的设置值绘制其他行实际上是可行的.

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.

推荐答案

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

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

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

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

检查在哪里计算值.如果您想以(0, 0)为中心,则需要执行一些算术运算或翻译,例如 Graphics2D#translate(int, int) .

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

此外:

public void paintComponent(Graphics2D g)

如果您尝试覆盖paintComponent,则尚未这样做. paintComponent使用Graphics,而不是Graphics2D:

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

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

在尝试覆盖时始终使用@Override批注,因为它将引起错误并告诉您是否不是覆盖.请参见 https://docs.oracle.com/javase/tutorial/java/IandI/override.html .

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.

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

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

这篇关于试图根据双打画线,但没有显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆