在整形Java的组合?(Shapes combination in Java?)

2019-07-29 18:01发布

请问java的Shape界面合同,库函数允许多个图形组合成一个扩展物体Shape的界面?

例如,我可以定义类Flower ,它将由几个椭圆形的花瓣为和核心?

Shape设唯一一个连续的轮廓? 如果是这样那么有没有在Java中用于容纳多种形状的任何一类,可能会出现一些类矢量图形?

Answer 1:

为了操纵形状在Java中像你所描述的,你要使用的Area类,它有这些操作。 只是一个转换Shape ,以一个Areanew Area(Shape)



Answer 2:

这里是我的尝试 - 利用旋转变换锚定在花形的中心。

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


文章来源: Shapes combination in Java?