[英]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.