Java(Swing) - 如何旋转一堆线?

时间:2017-05-15 17:47:52

标签: java swing

我一直在网上寻找这个,但我似乎无法找到如何旋转一堆线。我做了一架飞机"使用"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()

1 个答案:

答案 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);

这告诉我们:

  • 你的飞机的体长是20
  • 你的翅膀距离背部15个单位
  • 你的尾巴正好在后面

所以,要改变它从左向右移动,我们需要反转机翼和尾部位置......

  • 飞机长度仍然相同
  • Wings现在距后面5个单位
  • 尾巴现在在前面

然后,我们有这个代码:

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)

当下降时:

  • 身体的长度现在在Y轴上
  • 翅膀应该从后面15个单位
  • 尾巴在后面

所以我们有:

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;
        }
    }
}

那就是它!这是一张图片,展示了它的外观:

enter image description here

其他提示:

  1. 请勿拨打frame.setSize(),但覆盖MovePlane的JPanel getPreferredSize(),以返回固定大小的300, 300

  2. 始终将您的GUI放在Event Dispatch Thread (EDT)中,方法是将main方法包裹起来:

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Your code here
            }
        });
    }
    
  3. 与提示#1相关,请在覆盖frame.pack(); frame.setSize()之后调用JPanel而不是getPreferredSize():)