如何使用Java Graphics2D绘制一组QuadCurves [英] How to paint a group of QuadCurves using Java Graphics2D

查看:89
本文介绍了如何使用Java Graphics2D绘制一组QuadCurves的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



填充和曲线代码:

  g2.setStroke(new BasicStroke(5)); 
QuadCurve2D earLeft1 =新的QuadCurve2D.Double(145,155,137.5,49,150,49);
g2.draw(earLeft1);
QuadCurve2D earLeft2 =新的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 =新的QuadCurve2D.Double(262.5,87.5,287.5,25,300,25);
g2.draw(earRight1);
QuadCurve2D earRight2 =新的QuadCurve2D.Double(300,25,312.5,25,337.5,137.5);
g2.draw(earRight2);
CubicCurve2D headPhoneLeft =新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 =新的QuadCurve2D.Double(109,177,150,75,225,50);
g2.draw(headbandTop1);
QuadCurve2D headbandTop2 =新QuadCurve2D.Double(225,50,300,50,372,150);
g2.draw(headbandTop2);
QuadCurve2D headbandBottom1 =新的QuadCurve2D.Double(135,155,150,112.5,212.5,78);
g2.draw(headbandBottom1);
QuadCurve2D headbandBottom2 =新的QuadCurve2D.Double(212.5,78,306.25,78,351,137.5);
g2.draw(headbandBottom2);
QuadCurve2D faceBottomLeft =新的QuadCurve2D.Double(150,250,162.5,275,200,300);
g2.draw(faceBottomLeft);
QuadCurve2D faceBottomRight =新的QuadCurve2D.Double(362.5,225,363.5,237.5,350,262.5);
g2.draw(faceBottomRight);
CubicCurve2D leftArm =新的CubicCurve2D.Double(200,300,87.5,300,87.5,375,188.5,362.5);
g2.draw(leftArm);
CubicCurve2D rightArm =新的CubicCurve2D.Double(350,262.5,425,237.5,450,300,375,325);
g2.draw(rightArm);
QuadCurve2D leftLegOuter =新的QuadCurve2D.Double(188.5,362.5,154,425,200,512.5);
g2.draw(leftLegOuter);
QuadCurve2D rightLegOuter =新的QuadCurve2D.Double(375,325,388.5,356.25,387.5,412.5);
g2.draw(rightLegOuter);
QuadCurve2D leftFootTop =新的QuadCurve2D.Double(200,512.5,125,500,130,562.5);
g2.draw(leftFootTop);
QuadCurve2D leftFootBottom =新的QuadCurve2D.Double(130,562.5,175,575,262.5,562.5);
g2.draw(leftFootBottom);
QuadCurve2D leftLegInner =新的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 =新QuadCurve2D.Double(337.5,475,400,480,455,470);
g2.draw(rightFootBottom);
QuadCurve2D rightFootTop =新的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 =新的QuadCurve2D.Double(268.75,363,275,387.5,200,400);
//g2.draw(tailInner1);
// QuadCurve2D tailInner2 =新的QuadCurve2D.Double(200,400,125,387.5,62.5,400);
//g2.draw(tailInner2);
QuadCurve2D tailOuter1 =新的QuadCurve2D.Double(62.5,400,25,425,200,437.5);
g2.draw(tailOuter1);
QuadCurve2D tailOuter2 =新的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);
* /

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

解决方案

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);
//在耳朵之间
moveTo(200,100);
curveTo(200,100,237.5,88,262.5,87.5);
//右耳
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);
//头电话左
moveTo(300,25);
append(新的CubicCurve2D.Double(145,155,75,175,100,250,150,250),false);
moveTo(337.5,137.5);
append(新的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(新的CubicCurve2D.Double(200,300,87.5,300,87.5,375,188.5,362.5),false);
moveTo(350,262.5);
append(新的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(新的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);
}

}

然后根据容器的可用空间...



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

$ b $ public Test(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
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);
}

保护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 这样的内容,不会让您以不同的颜色对每个部分着色。


I have a lot of QuadCurve2D methods in a drawing, but when I fill them they don't actually fill the whole image.

Code for fill and curves:

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

This has been my only solution but it's not doing it. Suggestions?

解决方案

"A" solution might be to paint the "shape" to a BufferedImage and simply scale the image, but this will scale the stroke/line size as well.

A better solution might be to encapsulate the drawing into a Shape and use Shape#createTransformedShape instead.

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

}

And then scale the shape based on the available space of the container...

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

    }

}

Which you use will come down to needs, for example, using a Shape like this, won't allow you to color individual sections differently

这篇关于如何使用Java Graphics2D绘制一组QuadCurves的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆