简体   繁体   中英

Shapes in a Circle Java AWT and Swing

So I am trying to write a code where I draw squares inside a circle and that if the square is to draw outside the circle it would not (basically the circle acts as a border or frame). I am stuck on how I need to do this. Can anybody help. Thanks.

Here is my code.

public class Portal {

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

}

As a starting point, I would, highly, recommend having a look at the 2D Graphics tutorials

Clipping

One solution is just to take advantage of the clipping support of the 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();
        }

    }
}

Bounds detection

Another solution would be to take advantage of the bounds/hit detection available in the shapes API itself

边界检测

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();
        }

    }
}

Warning - this is not an optimised solution. This solution creates a number of short lived objects which could cause a deviation in the application performance and I would consider spending time devising a solution which could pre-cache the "boxes" instead of creating them each time paintComponent is called. But since this is a demonstration of the principle, I'll leave that up to you to figure out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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