簡體   English   中英

圓形 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 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 本身中可用的邊界/命中檢測

邊界檢測

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都創建它們。 但由於這是原理的演示,我將把它留給你來弄清楚

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM