Shapes combination in Java?

前端 未结 2 1993
面向向阳花
面向向阳花 2021-01-23 16:01

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

For example,

相关标签:
2条回答
  • 2021-01-23 16:19

    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).

    0 讨论(0)
  • 2021-01-23 16:21

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

    enter image description here

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题