在jframe上调整立方体曲线的大小

时间:2012-05-21 23:53:59

标签: java swing graphics java-2d paintcomponent

我有这个曲线形状,我希望相对于我的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);

1 个答案:

答案 0 :(得分: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;