Shapes combination in Java?

2019-02-27 21:51发布

问题:

Does java Shape interface contract and library routines allow combining multiple shapes into one object extending Shape interface?

For example, may I define class Flower which will consist of several ovals for petals and core?

Or the Shape supposes only one continuous outline? If so then is there any class in Java for holding multiple shapes, may be some class for vectorized graphics?

回答1:

To manipulate shapes in Java like you're describing, you want to use the Area class, which has these operations. Just convert a Shape to an Area with new Area(Shape).



回答2:

Here is my attempt - using a rotate transform anchored on the center of the flower shape.

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class DaisyDisplay {

    DaisyDisplay() {
        BufferedImage daisy = new BufferedImage(
                200,200,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = daisy.createGraphics();
        g.setColor(Color.GREEN.darker());
        g.fillRect(0, 0, 200, 200);
        Daisy daisyPainter = new Daisy();
        daisyPainter.paint(g);
        g.dispose();

        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(daisy)));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DaisyDisplay();
            }
        });
    }
}

class Daisy {

    public void paint(Graphics2D g) {
        Area daisyArea = getDaisyShape();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);

        paintDaisyPart(g,daisyArea);
        g.setTransform(AffineTransform.getRotateInstance(
                Math.PI*1/8,
                100,100));
        paintDaisyPart(g,daisyArea);
        g.setTransform(AffineTransform.getRotateInstance(
                Math.PI*3/8,
                100,100);
        paintDaisyPart(g,daisyArea);
        g.setTransform(AffineTransform.getRotateInstance(
                Math.PI*2/8,
                100,100));
        paintDaisyPart(g,daisyArea);
    }

    public void paintDaisyPart(Graphics2D g, Area daisyArea) {
        g.setClip(daisyArea);

        g.setColor(Color.YELLOW);
        g.fillRect(0, 0, 200, 200);

        g.setColor(Color.YELLOW.darker());
        g.setClip(null);
        g.setStroke(new BasicStroke(3));
        g.draw(daisyArea);
    }

    public Area getDaisyShape() {
        Ellipse2D.Double core = new Ellipse2D.Double(70,70,60,60);

        Area area = new Area(core);
        int size = 200;
        int pad = 10;
        int petalWidth = 50;
        int petalLength = 75;

        // left petal
        area.add(new Area(new Ellipse2D.Double(
                pad,(size-petalWidth)/2,petalLength,petalWidth)));
        // right petal
        area.add(new Area(new Ellipse2D.Double(
                (size-petalLength-pad),(size-petalWidth)/2,petalLength,petalWidth)));
        // top petal
        area.add(new Area(new Ellipse2D.Double(
                (size-petalWidth)/2,pad,petalWidth,petalLength)));
        // bottom petal
        area.add(new Area(new Ellipse2D.Double(
                (size-petalWidth)/2,(size-petalLength-pad),petalWidth,petalLength)));

        return area;
    }
}