简体   繁体   English

我们如何合并 Java awt 中的形状?

[英]How can we merge shapes in Java awt?

I am trying to merge two shapes into one object which can be used to draw the merged shape on the window with Graphics2D.for example 2 rounded rectangles with one of the rounded rectangles rotated 90 degrees merged into one shape.我正在尝试将两个形状合并到一个 object 中,它可用于使用 Graphics2D 在 window 上绘制合并的形状。例如,两个圆角矩形,其中一个圆角矩形旋转 90 度合并为一个形状。 I searched SO for possible solutions but all of them were talking about algorithms which create shapes out of 2 or more shapes using vertexes(I can't really use them with the Graphics 2D library), I also tried to googling it, which just gave solutions in JavaFX which is not what I am using.我搜索了可能的解决方案,但他们都在谈论使用顶点从 2 个或更多形状中创建形状的算法(我不能真正将它们与 Graphics 2D 库一起使用),我还尝试用谷歌搜索它,它只是给了JavaFX 中的解决方案不是我使用的。 I had also tried out some code myself, but it was still using two shape objects.我自己也尝试了一些代码,但它仍然使用两个形状对象。 Here's what I've tried:这是我尝试过的:


import javax.swing.*;
import java.awt.*;

public class Red extends JPanel {
    public Red(Color c, int width, int height){
        this.setBackground(c);
        this.setSize(width, height);
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D gtd = (Graphics2D) g;
        gtd.setColor(Color.red);
        gtd.fillRoundRect(500,500,50,15, 3, 3);
        gtd.rotate(Math.toRadians(90),525,500 + 15/2 );
        gtd.fillRoundRect(500,500,50,15, 3, 3);
    }
}

I want to merge:我想合并:


        gtd.fillRoundRect(500,500,50,15, 3, 3);
        gtd.rotate(Math.toRadians(90),525,500 + 15/2 );
        gtd.fillRoundRect(500,500,50,15, 3, 3);

into one shape that can be used as a normal rectangle object and be drawn such as:变成一种可以用作普通矩形 object 的形状并绘制如下:

MergedShape X = new MergedShape(x,y, width, height)
gtd.draw(X);

finally, So to put it short: How can we merge 2 shapes into one, that can also be used as a normal shape with the Graphics2D method?最后,简而言之:我们如何将 2 个形状合并为一个,也可以通过 Graphics2D 方法用作普通形状?

thanks in advance!提前致谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM