如何使用Java Graphics2D绘制一组QuadCurves

时间:2015-05-04 22:56:54

标签: java graphics graphics2d

我在绘图中有很多QuadCurve2D方法,但是当我填充它们时,它们实际上并没有填满整个图像。

填充和曲线代码:

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);
    */

这是我唯一的解决方案,但它没有这样做。建议?

1 个答案:

答案 0 :(得分:1)

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

更好的解决方案可能是将图形封装到Shape并改为使用Shape#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);
    }

}

然后根据容器的可用空间缩放形状......

Fat Cat

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

    }

}

您使用的内容将归结为需求,例如,使用这样的Shape,不允许您以不同方式为各个部分着色