我一直在网上寻找这个,但我似乎无法找到如何旋转一堆线。我做了一架飞机"使用"Resource": [
"arn:aws:s3:::xxxxxx/user/aa${cognito-identity.amazonaws.com:sub}/*"
"arn:aws:s3:::xxxxxx/user/ab${cognito-identity.amazonaws.com:sub}/*"
"arn:aws:s3:::xxxxxx/user/ac${cognito-identity.amazonaws.com:sub}/*"
.
.
.
"arn:aws:s3:::xxxxxx/user/zy${cognito-identity.amazonaws.com:sub}/*"
"arn:aws:s3:::xxxxxx/user/zz${cognito-identity.amazonaws.com:sub}/*"
]
功能,但我想知道当它撞到墙壁时如何将整个物体向右旋转90度。这是我目前的代码:
Graphics.drawLine()
答案 0 :(得分:2)
每次更改方向时都需要更改它,因此,您需要在每个g.drawLine(...)
条件内移动if
次来电...
但是在旋转飞机时,你还需要照顾"界限"或者屏幕的限制,因此,我修改了if (x == 0)
和... && x > 0
条件,以便有20
的差距......
让我们从左到右开始飞机:
g.drawLine(x, y, x + 20, y); // body - drawn in terms of x
g.drawLine(x + 15, y - 5, x + 15, y + 5); // wing
g.drawLine(x, y - 2, x, y + 2);
这告诉我们:
所以,要改变它从左向右移动,我们需要反转机翼和尾部位置......
然后,我们有这个代码:
g.drawLine(x, y, x + 20, y); // Body length 20
g.drawLine(x + 5, y - 5, x + 5, y + 5); // Wing's are 5 units from the front
g.drawLine(x + 20, y - 2, x + 20, y + 2); // Tail is on the front (at body's length)
当下降时:
所以我们有:
g.drawLine(x, y, x, y + 20); // Body is now on Y axis
g.drawLine(x - 5, y + 15, x + 5, y + 15); // Wings are 15 units from the back
g.drawLine(x - 2, y, x + 2, y); // Tail is on the back
应用与从右到左相同的逻辑:
g.drawLine(x, y + 20, x, y);
g.drawLine(x - 5, y + 5, x + 5, y + 5);
g.drawLine(x - 2, y + 20, x + 2, y + 20);
然后您的paintComponent(...)
方法如下所示:
public void paintComponent(Graphics g) {
// both paint and paintComponent work - difference?
super.paintComponent(g); // call superclass's paintComponent
if (right && up) {
g.drawLine(x, y, x + 20, y); // body - drawn in terms of x
g.drawLine(x + 15, y - 5, x + 15, y + 5); // wing
g.drawLine(x, y - 2, x, y + 2);
x++;
if (x == getWidth() - 25) {
right = false;
up = false;
}
} else if (!right && !up) {
g.drawLine(x, y, x, y + 20); // body - drawn in terms of x
g.drawLine(x - 5, y + 15, x + 5, y + 15); // wing
g.drawLine(x - 2, y, x + 2, y);
y++;
if (y == getHeight() - 25) {
up = true;
}
} else {
if (x <= getWidth() - 15 && x > 20) {
g.drawLine(x, y, x + 20, y); // body - drawn in terms of x
g.drawLine(x + 5, y - 5, x + 5, y + 5); // wing
g.drawLine(x + 20, y - 2, x + 20, y + 2);
x--;
}
if (x == 20) {
g.drawLine(x, y, x, y + 20); // body - drawn in terms of x
g.drawLine(x - 5, y + 5, x + 5, y + 5); // wing
g.drawLine(x - 2, y + 20, x + 2, y + 20);
y--;
}
if (y == 10) {
right = true;
}
}
}
那就是它!这是一张图片,展示了它的外观:
请勿拨打frame.setSize()
,但覆盖MovePlane
的JPanel getPreferredSize()
,以返回固定大小的300, 300
。
始终将您的GUI放在Event Dispatch Thread (EDT)中,方法是将main
方法包裹起来:
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//Your code here
}
});
}
与提示#1相关,请在覆盖frame.pack();
frame.setSize()
之后调用JPanel
而不是getPreferredSize()
:)