Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java Graphics2D绘制一组四元曲线_Java_Graphics_Graphics2d - Fatal编程技术网

如何使用Java Graphics2D绘制一组四元曲线

如何使用Java Graphics2D绘制一组四元曲线,java,graphics,graphics2d,Java,Graphics,Graphics2d,我在一张图中有很多四曲线2D方法,但当我填充它们时,它们实际上并没有填充整个图像 填充和曲线的代码: g2.setStroke(new BasicStroke(5)); QuadCurve2D earLeft1 = new QuadCurve2D.Double(145, 155, 137.5, 49, 150, 49); g2.draw(earLeft1); QuadCurve2D earLeft2 = new QuadCurve2D.Double(150, 49, 15

我在一张图中有很多四曲线2D方法,但当我填充它们时,它们实际上并没有填充整个图像

填充和曲线的代码:

g2.setStroke(new BasicStroke(5));
    QuadCurve2D earLeft1 = new QuadCurve2D.Double(145, 155, 137.5, 49, 150, 49);
    g2.draw(earLeft1);
    QuadCurve2D earLeft2 = new QuadCurve2D.Double(150, 49, 156.25, 49, 200, 100);
    g2.draw(earLeft2);
    QuadCurve2D betweenEars = new QuadCurve2D.Double(200, 100, 237.5, 88, 262.5, 87.5);
    g2.draw(betweenEars);
    QuadCurve2D earRight1 = new QuadCurve2D.Double(262.5, 87.5, 287.5, 25, 300, 25);
    g2.draw(earRight1);
    QuadCurve2D earRight2 = new QuadCurve2D.Double(300, 25, 312.5, 25, 337.5, 137.5);
    g2.draw(earRight2);
    CubicCurve2D headPhoneLeft = new CubicCurve2D.Double(145, 155, 75, 175, 100, 250, 150, 250);
    g2.draw(headPhoneLeft);
    CubicCurve2D headPhoneRight = new CubicCurve2D.Double(337.5, 137.5, 387.5, 137.5, 393.75, 188, 362.5, 225);
    g2.draw(headPhoneRight);
    QuadCurve2D headbandTop1 = new QuadCurve2D.Double(109, 177, 150, 75, 225, 50);
    g2.draw(headbandTop1);
    QuadCurve2D headbandTop2 = new QuadCurve2D.Double(225, 50, 300, 50, 372, 150);
    g2.draw(headbandTop2);
    QuadCurve2D headbandBottom1 = new QuadCurve2D.Double(135, 155, 150, 112.5, 212.5, 78);
    g2.draw(headbandBottom1);
    QuadCurve2D headbandBottom2 = new QuadCurve2D.Double(212.5, 78, 306.25, 78, 351, 137.5);
    g2.draw(headbandBottom2);
    QuadCurve2D faceBottomLeft = new QuadCurve2D.Double(150, 250, 162.5, 275, 200, 300);
    g2.draw(faceBottomLeft);
    QuadCurve2D faceBottomRight = new QuadCurve2D.Double(362.5, 225, 363.5, 237.5, 350, 262.5);
    g2.draw(faceBottomRight);
    CubicCurve2D leftArm = new CubicCurve2D.Double(200, 300, 87.5, 300, 87.5, 375, 188.5, 362.5);
    g2.draw(leftArm);
    CubicCurve2D rightArm = new CubicCurve2D.Double(350, 262.5, 425, 237.5, 450, 300, 375, 325);
    g2.draw(rightArm);
    QuadCurve2D leftLegOuter = new QuadCurve2D.Double(188.5, 362.5, 154, 425, 200, 512.5);
    g2.draw(leftLegOuter);
    QuadCurve2D rightLegOuter = new QuadCurve2D.Double(375, 325, 388.5, 356.25, 387.5, 412.5);
    g2.draw(rightLegOuter);
    QuadCurve2D leftFootTop = new QuadCurve2D.Double(200, 512.5, 125, 500, 130, 562.5);
    g2.draw(leftFootTop);
    QuadCurve2D leftFootBottom = new QuadCurve2D.Double(130, 562.5, 175, 575, 262.5, 562.5);
    g2.draw(leftFootBottom);
    QuadCurve2D leftLegInner = new QuadCurve2D.Double(262.5, 562.5, 237.5, 400, 268.75, 363);
    g2.draw(leftLegInner);
    QuadCurve2D rightLegInner = new QuadCurve2D.Double(268.75, 363, 318.75, 362.5, 337.5, 475);
    g2.draw(rightLegInner);
    QuadCurve2D rightFootBottom = new QuadCurve2D.Double(337.5, 475, 400, 480, 455, 470);
    g2.draw(rightFootBottom);
    QuadCurve2D rightFootTop = new QuadCurve2D.Double(455, 470, 450, 400, 387.5, 412.5);
    g2.draw(rightFootTop);

    CubicCurve2D tailInner = new CubicCurve2D.Double(268.75, 363, 287.5, 450,125, 387.5, 62.5, 400);
    g2.draw(tailInner);

    //QuadCurve2D tailInner1 = new QuadCurve2D.Double(268.75, 363, 275, 387.5, 200, 400);
    //g2.draw(tailInner1);
    //QuadCurve2D tailInner2 = new QuadCurve2D.Double(200, 400, 125, 387.5, 62.5, 400);
    //g2.draw(tailInner2);
    QuadCurve2D tailOuter1 = new QuadCurve2D.Double(62.5, 400, 25, 425, 200, 437.5);
    g2.draw(tailOuter1);
    QuadCurve2D tailOuter2 = new QuadCurve2D.Double(200, 437.5, 287.5, 425, 300, 375);
    g2.draw(tailOuter2);

    int[] x = {175, 200, 225, 225, 287, 300, 309, 337, 325, 309, 302, 292, 240, 227, 226, 215};
    int[] y = {225, 210, 237, 200, 187, 212, 187, 187, 262, 262, 230, 268, 275, 250, 277, 281};
    GeneralPath mouthAndTeeth = new GeneralPath();

    mouthAndTeeth.moveTo(x[0], y[0]);

    for (int index = 1; index < x.length; index++) {
        mouthAndTeeth.lineTo(x[index], y[index]);
    };

    mouthAndTeeth.closePath();
    g2.draw(mouthAndTeeth);

    /*
    g2.fill(earLeft1);
    g2.fill(earLeft2);
    g2.fill(earRight1);
    g2.fill(earRight2);
    g2.fill(betweenEars);
    g2.fill(headbandBottom1);
    g2.fill(headbandBottom2);
    g2.fill(headbandTop1);
    g2.fill(headbandTop2);
    g2.fill(headPhoneLeft);
    g2.fill(headPhoneRight);
    g2.fill(faceBottomLeft);
    g2.fill(faceBottomRight);
    g2.fill(leftArm);
    g2.fill(rightArm);
    g2.fill(leftFootBottom);
    g2.fill(leftFootTop);
    g2.fill(rightFootBottom);
    g2.fill(rightFootTop);
    g2.fill(leftLegInner);
    g2.fill(leftLegOuter);
    g2.fill(rightLegInner);
    g2.fill(rightLegOuter);
    //g2.fill(tailInner1);
    //g2.fill(tailInner2);
    g2.fill(tailOuter1);
    g2.fill(tailOuter2);
    */
g2.设定行程(新基本行程(5));
quadcurve2dearleft1=新的QuadCurve2D.Double(145、155、137.5、49、150、49);
g2.绘制(earLeft1);
quadcurve2dearleft2=新的QuadCurve2D.Double(150,49156.25,49200100);
g2.绘制(earLeft2);
中间的四边形曲线2d=新的四边形曲线2d.Double(200100237.5,88262.5,87.5);
g2.绘制(中间线);
QuadCurve2D耳环1=新的QuadCurve2D双色(262.5,87.5,287.5,25300,25);
g2.绘制(图1);
QuadCurve2D耳环2=新的QuadCurve2D双色(300,25312.5,25337.5137.5);
g2.绘制(图2);
CubicCurve2D耳机左=新的CubicCurve2D.Double(14515575175100250150250);
g2.绘制(左);
CubicCurve2D耳机右=新的CubicCurve2D.Double(337.5137.5387.5137.5393.75188362.5225);
g2.绘制(右);
QuadCurve2D头带TOP1=新的QuadCurve2D双头带(1091771507522550);
g2.绘制(头带顶部1);
QuadCurve2D头带Top2=新的QuadCurve2D双(225,50,300,50,372,150);
g2.绘制(头带顶部2);
QuadCurve2D头带底部1=新的QuadCurve2D.双(135、155、150、112.5、212.5、78);
g2.牵引(头带底部1);
QuadCurve2D头带底部2=新的QuadCurve2D双(212.5,78,306.25,78,351,137.5);
g2.牵引(头带底部2);
QuadCurve2D faceBottomLeft=新的QuadCurve2D.双(150250162.5275200300);
g2.绘制(面左下角);
QuadCurve2D faceBottomRight=新的QuadCurve2D.双(362.5225363.5237.5350262.5);
g2.绘制(面右下角);
CubicCurve2D左臂=新的CubicCurve2D.Double(20030087.53000 87.5375188.5362.5);
g2.画(左臂);
CubicCurve2D右臂=新的CubicCurve2D.Double(350262.5425237.5450300375.325);
g2.牵引(右臂);
QuadCurve2D leftLegOuter=新的QuadCurve2D.Double(188.5362.5154425200512.5);
g2.绘制(左腿外侧);
QuadCurve2D rightLegOuter=新的QuadCurve2D.Double(375、325、388.5、356.25、387.5、412.5);
g2.绘制(右腿外侧);
QuadCurve2D leftFootTop=新的QuadCurve2D.Double(200512.5125500130562.5);
g2.画图(左脚);
QuadCurve2D leftFootBottom=新的QuadCurve2D.Double(130562.5175575262.5562.5);
g2.绘制(左脚底);
QuadCurve2D leftLegInner=新的QuadCurve2D.Double(262.5562.5237.5400268.75363);
g2.抽签(左路);
QuadCurve2D rightLegInner=新的QuadCurve2D.Double(268.7536338318.75362.5337.5475);
g2.抽签(右旋);
QuadCurve2D rightFootBottom=新的QuadCurve2D.Double(337.5475400480405470);
g2.绘制(右脚底);
QuadCurve2D rightFootTop=新的QuadCurve2D.双(455470450400387.5412.5);
g2.画图(右脚);
CubicCurve2D-tailInner=新的CubicCurve2D.Double(268.75363287.5450125387.5362.5400);
g2.牵引(泰利纳);
//QuadCurve2D tailInner1=新的QuadCurve2D.Double(268.75363275387.5200400);
//g2.绘制(tailInner1);
//QuadCurve2D tailInner2=新的QuadCurve2D.Double(200400125387.5,62.5400);
//g2.牵引(尾翼2);
QuadCurve2D尾端1=新的QuadCurve2D.双(62.5400425425200437.5);
g2.牵引(尾线1);
QuadCurve2D尾端2=新的QuadCurve2D.双(200437.5287.52425300375);
g2.牵引(尾侧2);
int[]x={175200225225287300325309302292402272226215};
int[]y={225、210、237、200、187、212、187、187、262、262、230、268、275、250、277、281};
GeneralPath mouthandDeaths=新的GeneralPath();
移动到(x[0],y[0]);
对于(int index=1;index
这是我唯一的解决办法,但它没有做到。建议?

“一个”解决方案可能是将“形状”绘制为
缓冲图像
,并简单地缩放图像,但这也会缩放笔划/线条大小

更好的解决方案可能是将图形封装到
形状
中,并改用
形状#createTransformedShape

public class Cat extends Path2D.Double {

    public Cat() {
        //Left Ear
        moveTo(145, 155);
        curveTo(145, 155, 137.5, 49, 150, 49);
        moveTo(150, 49);
        curveTo(150, 49, 156.25, 49, 200, 100);
        // Between Ears
        moveTo(200, 100);
        curveTo(200, 100, 237.5, 88, 262.5, 87.5);
        // Right ear
        moveTo(262.5, 87.5);
        curveTo(262.5, 87.5, 287.5, 25, 300, 25);
        moveTo(300, 25);
        curveTo(300, 25, 312.5, 25, 337.5, 137.5);
        // Head phone left
        moveTo(300, 25);
        append(new CubicCurve2D.Double(145, 155, 75, 175, 100, 250, 150, 250), false);
        moveTo(337.5, 137.5);
        append(new CubicCurve2D.Double(337.5, 137.5, 387.5, 137.5, 393.75, 188, 362.5, 225), false);
        moveTo(109, 177);
        curveTo(109, 177, 150, 75, 225, 50);
        moveTo(225, 50);
        curveTo(225, 50, 300, 50, 372, 150);
        moveTo(135, 155);
        curveTo(135, 155, 150, 112.5, 212.5, 78);
        moveTo(212.5, 78);
        curveTo(212.5, 78, 306.25, 78, 351, 137.5);
        moveTo(150, 250);
        curveTo(150, 250, 162.5, 275, 200, 300);
        moveTo(362.5, 225);
        curveTo(362.5, 225, 363.5, 237.5, 350, 262.5);
        moveTo(200, 300);
        append(new CubicCurve2D.Double(200, 300, 87.5, 300, 87.5, 375, 188.5, 362.5), false);
        moveTo(350, 262.5);
        append(new CubicCurve2D.Double(350, 262.5, 425, 237.5, 450, 300, 375, 325), false);
        moveTo(188.5, 362.5);
        curveTo(188.5, 362.5, 154, 425, 200, 512.5);
        moveTo(375, 325);
        curveTo(375, 325, 388.5, 356.25, 387.5, 412.5);
        moveTo(200, 512.5);
        curveTo(200, 512.5, 125, 500, 130, 562.5);
        moveTo(130, 562.5);
        curveTo(130, 562.5, 175, 575, 262.5, 562.5);
        moveTo(262.5, 562.5);
        curveTo(262.5, 562.5, 237.5, 400, 268.75, 363);
        moveTo(268.75, 363);
        curveTo(268.75, 363, 318.75, 362.5, 337.5, 475);
        moveTo(337.5, 475);
        curveTo(337.5, 475, 400, 480, 455, 470);
        moveTo(455, 470);
        curveTo(455, 470, 450, 400, 387.5, 412.5);

        moveTo(268.75, 363);
        append(new CubicCurve2D.Double(268.75, 363, 287.5, 450, 125, 387.5, 62.5, 400), false);

        moveTo(62.5, 400);
        curveTo(62.5, 400, 25, 425, 200, 437.5);
        moveTo(200, 437.5);
        curveTo(200, 437.5, 287.5, 425, 300, 375);

        int[] x = {175, 200, 225, 225, 287, 300, 309, 337, 325, 309, 302, 292, 240, 227, 226, 215};
        int[] y = {225, 210, 237, 200, 187, 212, 187, 187, 262, 262, 230, 268, 275, 250, 277, 281};
        GeneralPath mouthAndTeeth = new GeneralPath();

        mouthAndTeeth.moveTo(x[0], y[0]);

        for (int index = 1; index < x.length; index++) {
            mouthAndTeeth.lineTo(x[index], y[index]);
        }

        mouthAndTeeth.closePath();
        append(mouthAndTeeth, false);
    }

}

您使用的形状取决于需要,例如,像这样使用
形状
,将不允许您对各个部分进行不同的着色

考虑创建自定义
形状
,可能基于
路径2D
考虑提供一个演示您的问题的颜色。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应。不要依赖绝对值,使用已知约束(
getWidth
getHeight
)并计算坐标。您“可以”尝试缩放图像,但这将更改像素的大小
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Cat cat;

        public TestPane() {
            cat = new Cat();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setStroke(new BasicStroke(5));

            int catWidth = cat.getBounds().x + cat.getBounds().width;
            int catHeight = cat.getBounds().y + cat.getBounds().height;

            int width = getWidth() - 1;
            int height = getHeight() - 1;

            double scaleWidth = width / (double)catWidth;
            double scaleHeight = height / (double)catHeight;

            double scale = Math.min(scaleHeight, scaleWidth);
            AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
            Shape shape = cat.createTransformedShape(at);

            g2d.draw(shape);

            g2d.dispose();
        }

    }

}