作为起点,我强烈建议您查看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 时创建它们。但既然这是原理的演示,那我就留给你去弄清楚