【问题标题】:Shapes in a Circle Java AWT and Swing圆形中的形状 Java AWT 和 Swing
【发布时间】:2021-12-05 03:49:28
【问题描述】:

所以我正在尝试编写一个代码,在其中我在圆内绘制正方形,如果正方形要在圆外绘制,则不会(基本上,圆充当边框或框架)。我被困在我需要如何做这件事上。任何人都可以帮忙。谢谢。

这是我的代码。

public class Portal {

    public void drawPortal(Graphics2D g2){
        g2.setColor(Color.black);
        g2.fillOval(80, 110, 600, 100);
    }

}

【问题讨论】:

    标签: java swing awt shapes


    【解决方案1】:

    作为起点,我强烈建议您查看2D Graphics 教程

    剪辑

    一种解决方案就是利用Graphics API 的剪辑支持

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.geom.Ellipse2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private Shape circle = new Ellipse2D.Double(100, 100, 200, 200);
    
            public TestPane() {
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(Color.RED);
                g2d.fill(circle);
                g2d.setClip(circle);
                g2d.setColor(Color.BLACK);
    
                int circleWidth = circle.getBounds().width;
                int circleHeight = circle.getBounds().height;
    
                int circleX = circle.getBounds().x;
                int circleY = circle.getBounds().y;
    
                for (int y = circleX; y < circleY + circleHeight; y += 20) {
                    for (int x = circleY; x < circleX + circleWidth; x += 20) {
                        g2d.drawRect(x, y, 20, 20);
                    }
                }
                g2d.dispose();
            }
    
        }
    }
    

    边界检测

    另一种解决方案是利用 shape API 本身提供的边界/命中检测

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.Shape;
    import java.awt.geom.Ellipse2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private Shape circle = new Ellipse2D.Double(100, 100, 200, 200);
    
            public TestPane() {
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(Color.RED);
                g2d.fill(circle);
                g2d.setColor(Color.BLACK);
    
                int circleWidth = circle.getBounds().width;
                int circleHeight = circle.getBounds().height;
    
                int circleX = circle.getBounds().x;
                int circleY = circle.getBounds().y;
    
                for (int y = circleX; y < circleY + circleHeight; y += 20) {
                    for (int x = circleY; x < circleX + circleWidth; x += 20) {
                        Rectangle box = new Rectangle(x, y, 20, 20);
                        if (circle.contains(box)) {
                            g2d.draw(box);
                        }
                    }
                }
                g2d.dispose();
            }
    
        }
    }
    

    警告 - 这不是优化的解决方案。该解决方案创建了许多短期对象,这些对象可能会导致应用程序性能出现偏差,我会考虑花时间设计一种解决方案,该解决方案可以预先缓存“盒子”,而不是在每次调用 paintComponent 时创建它们。但既然这是原理的演示,那我就留给你去弄清楚

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2015-08-20
      • 2021-08-22
      相关资源
      最近更新 更多