所以我想寫一個代碼,我在圓內畫正方形,如果正方形要畫在圓外,它就不會(基本上圓作為邊框或框架)。我被困在我需要如何做到這一點上。任何人都可以幫忙。謝謝。
這是我的代碼。
public class Portal {
public void drawPortal(Graphics2D g2){
g2.setColor(Color.black);
g2.fillOval(80, 110, 600, 100);
}
}
uj5u.com熱心網友回復:
作為起點,我強烈建議您查看
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();
}
}
}
邊界檢測
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/323033.html
上一篇:Docker學習
