我有一个围绕它的中间旋转的矩形,我有另一个矩形,我想连接到旋转矩形的右上角.问题是我不知道如何到达角落,以便第二个矩形总是会粘在那个角落.
这是我的示例代码.现在第二个矩形将始终在同一个地方,这不是我追求的结果.
package Test;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
class Test{
public static void main(String[] args){
new Test();
}
public Test(){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Graphic());
frame.setSize(1000,700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
class Graphic extends JPanel{
private int x, y, windowW, windowH;
private double angle;
private Rectangle rect1, rect2;
private Path2D path;
private Timer timer;
private AffineTransform rotation;
public Graphic(){
windowW = (int) …Run Code Online (Sandbox Code Playgroud) 我的游戏中有两个 Path2D 对象,一个用于玩家,另一个用于其中一位 Boss 拥有的触手。我需要使用 Path2D,因为我希望绑定与玩家/触手一起旋转,这样如果你站在它旁边 5 个像素的地方就不会被它击中。问题是我无法使用,playerPath.intersects(tentaclPath)因为 Path2D 只能检查与矩形的交集,这是一个问题,因为它违背了在我的游戏中使用 Path2D 的目的。我如何能够检查它们是否相交而不将其中一条路径转移到矩形?
我使用ArrayList来存储关卡中每个矩形的"阴影",但是当我迭代这样的时候:
for(int n = 0; n < shadows.size(); ++n){
g2d.fillPolygon(shadows.get(n)[0]);
g2d.fillPolygon(shadows.get(n)[1]);
g2d.fillPolygon(shadows.get(n)[2]);
g2d.fillPolygon(shadows.get(n)[3]);
g2d.fillPolygon(shadows.get(n)[4]);
g2d.fillPolygon(shadows.get(n)[5]);
}
Run Code Online (Sandbox Code Playgroud)
我收到一个java.lang.IndexOutOfBoundsException看起来像这样的错误:Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 42, Size: 79
为什么即使通过索引号不等于或大于大小,我也会得到错误?该程序仍然像正常运行,但我仍然不希望它有任何错误.
我也尝试过的enchanced for循环,但后来我得到一个java.util.ConcurrentModificationException代替
for(Polygon[] polys : shadows){
g2d.fillPolygon(polys[0]);
g2d.fillPolygon(polys[1]);
g2d.fillPolygon(polys[2]);
g2d.fillPolygon(polys[3]);
g2d.fillPolygon(polys[4]);
g2d.fillPolygon(polys[5]);
}
Run Code Online (Sandbox Code Playgroud) java arrays runtime-error arraylist indexoutofboundsexception
java ×3
arraylist ×1
arrays ×1
awt ×1
bounds ×1
intersection ×1
path ×1
rectangles ×1
rotation ×1
shape ×1