圆形形状 Java AWT 和 Swing

Shapes in a Circle Java AWT and Swing

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

这是我的代码。

public class Portal {

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

}

作为起点,我强烈建议您查看 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();
        }

    }
}

边界检测

另一种解决方案是利用形状 API 本身 bounds/hit 中可用的检测

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 时都创建它们。不过因为是原理的演示,就留给大家自己琢磨了