0

我有这个曲线形状,我希望宽度和高度相对于我的 JFrame 的大小相应地调整大小,例如我的 JFrame 大小是 setSize(440, 300); - 然后,如果我最大化我的 JFrame,我希望曲线形状也可以调整大小,以便形状保持其实际形状。任何帮助,提前感谢。谢谢,

这是我的代码:

float offset = (float) Math.sin(Math.PI);

x1 = offset;
y1 = (height/4.0f) - 4.0f;

x1ctl = ((width/4) - 140) + 90.0f;
y1ctl = ((height/4) - 100) + 20.0f;

x2ctl = ((width/4) - 10.0f) + 60.0f;
y2ctl = ((height/4) - 8.0f) + 1.0f;

x2 = (width/2.0f) - 20.0f;
y2 = offset - 4.0f;

curve = new CubicCurve2D.Float(
        x1,y1,
        x1ctl,y1ctl,
        x2ctl,y2ctl,
        x2,y2);

g2d.draw(curve);
4

1 回答 1

1

您可以覆盖paintComponent和使用组件的尺寸。这是基于问题中的论点的示例:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.CubicCurve2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class CubicCurveComponentTest extends JComponent {

    public void paintComponent(Graphics g) {

        float offset = (float) Math.sin(Math.PI);

        float x1 = offset;
        float y1 = (getHeight()/4.0f) - 4.0f;

        float x1ctl = ((getWidth()/4) - 140) + 90.0f;
        float y1ctl = ((getHeight()/4) - 100) + 20.0f;

        float x2ctl = ((getWidth()/4) - 10.0f) + 60.0f;
        float y2ctl = ((getHeight()/4) - 8.0f) + 1.0f;

        float x2 = (getWidth()/2.0f) - 20.0f;
        float y2 = offset - 4.0f;

        CubicCurve2D curve = new CubicCurve2D.Float(
                x1,y1,
                x1ctl,y1ctl,
                x2ctl,y2ctl,
                x2,y2);

        Graphics2D g2 = (Graphics2D) g;
        g2.draw(curve);
    }

    private static void createAndShowGUI() {    
        JFrame f = new JFrame("Cubic Curve Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(440, 300);
        f.add(new CubicCurveComponentTest());
        f.setVisible(true);
    }

    public static void main(String args[]) {
        Runnable doCreateAndShowGUI = new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        };
        SwingUtilities.invokeLater(doCreateAndShowGUI);
    }
}

编辑:坐标示例

以下是从容器的给定初始尺寸 (440, 300) 和原始计算中使用的幻数得出的坐标示例:

    float x1 = 0;
    float y1 = getHeight() * 0.24f;

    float x1ctl =  getWidth() * 0.125f;
    float y1ctl = 0;

    float x2ctl = getWidth() * 0.24f;
    float y2ctl = getHeight() * 0.22f;

    float x2 = getWidth() * 0.45f;
    float y2 = -getHeight() * 0.013f;
于 2012-05-23T18:07:25.050 回答