GeneralPath 不会使用计算的坐标进行绘制

问题描述 投票:0回答:1

我被难住了。我试图根据计算值在图形 2D 环境中画一条线,而 GeneralPath 不会这样做。基本上,我想根据公式 y=sx+b 绘制一条直线,其中 x 是提供的值,s 是角度的正切(以度为单位),b 是 y 截距。求解 y 并绘制到 {x, y}。在测试过程中,我添加了一些虚拟点以使其完全绘制,确实如此,但是当我添加公式时,lineTo 会忽略该坐标对。

这是我的问题代码:

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;

public class Sketch extends JPanel{
    
    public Sketch(String num) {
        super();
    }

    public void paintComponent(Graphics comp){
        super.paintComponent(comp);
        Graphics2D comp2D = (Graphics2D) comp;
        comp2D.setColor(Color.white);//set background color
        Rectangle2D.Double background = new Rectangle2D.Double(0F, 0F, 260F, 220F);//create background
        comp2D.fill(background);
        comp2D.setColor(Color.black);

        Double minX = 74.0;
        Double minY = 62.0;
        Double maxX = 146.0;
        Double maxY = 158.0;
        Double r = 157.5;
        double offsetDir = r + 90;
        double[] offsetLT = new double[2];

        //where X is 0 and Y is 1:
        double offsetX = 15 * Math.cos(Math.toRadians(offsetDir));
        double offsetY = 15 * Math.sin(Math.toRadians(offsetDir));
        offsetLT[0] = 110+offsetX;
        offsetLT[1] = 110+offsetY;
        Double cLT = offsetLT[1] - (Math.tan(Math.toRadians(r))*offsetLT[0]);

        GeneralPath trough = new GeneralPath();
        Double currentY;
        currentY = Math.tan(Math.toRadians(r))*minX+cLT;
        trough.moveTo(minX, minY);
        trough.lineTo(110D, currentY);
        trough.lineTo(maxX-15, maxY);
        comp2D.draw(trough);
    }
}

这是一个更大的包中的类,因此这里故意没有 main 。我还从一个更大的代码块中删除了它,以尝试仅显示相关内容。

*编辑:回答 camickr 的回复,我有一个带有 CardLayout 的 GUI。该草图被称为新草图并添加到该卡中。其代码如下:

Sketch sku = new Sketch(currentStructure);
sketchCard.add(sku, currentStructure);

所述卡位于 JFrame 中,JPanel 包含其他卡(包括 sketchCard)。至于有关大小的问题,我添加了用于创建背景的代码。这似乎可以控制大小。

我尝试从 GeneralPath 更改为 Path2D.Double ,认为问题在于混合 Float 和 Double,但没有改变。谁能解释一下为什么它不会绘制到{110D,currentY}?

谢谢

java swing graphics2d lineto
1个回答
0
投票

你的代码对我有用。下面是添加了一些内容的代码,即创建

JFrame
并向其添加 main 类的实例的
Sketch
方法。

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Sketch extends JPanel{
    
    public Sketch(String num) {
        super();
    }

    public void paintComponent(Graphics comp){
        super.paintComponent(comp);
        Graphics2D comp2D = (Graphics2D) comp;
        comp2D.setColor(Color.white);//set background color
        Rectangle2D.Double background = new Rectangle2D.Double(0F, 0F, 260F, 220F);//create background
        comp2D.fill(background);
        comp2D.setColor(Color.black);

        Double minX = 74.0;
        Double minY = 62.0;
        Double maxX = 146.0;
        Double maxY = 158.0;
        Double r = 157.5;
        double offsetDir = r + 90;
        double[] offsetLT = new double[2];

        //where X is 0 and Y is 1:
        double offsetX = 15 * Math.cos(Math.toRadians(offsetDir));
        double offsetY = 15 * Math.sin(Math.toRadians(offsetDir));
        offsetLT[0] = 110+offsetX;
        offsetLT[1] = 110+offsetY;
        Double cLT = offsetLT[1] - (Math.tan(Math.toRadians(r))*offsetLT[0]);

        GeneralPath trough = new GeneralPath();
        Double currentY;
        currentY = Math.tan(Math.toRadians(r))*minX+cLT;
        trough.moveTo(minX, minY);
        trough.lineTo(110D, currentY);
        trough.lineTo(maxX-15, maxY);
        comp2D.draw(trough);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Sketch sketch = new Sketch("0");
            frame.add(sketch);
            frame.setSize(450, 300);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }
}

这是我运行上述代码时得到的窗口。

请注意,我在 Windows 11 上使用 Eclipse 2023-12,因此上述代码是使用 Eclipse IDE 构建器编译的,并使用 Eclipse JRE(即 Java 17)运行。

© www.soinside.com 2019 - 2024. All rights reserved.