I've been looking for this online, but I can't seem to find how to rotate a bunch of lines. I made a "plane" with the Graphics.drawLine()
function, but I want to know how to rotate the whole thing 90 degrees to the right when it hits a wall. Here is my current code:
/* MovePlane
* Moves plane to the right, down, left, up, and repeats using Timer object
*/
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class MovePlane extends JPanel implements ActionListener {
private int delay = 5;
private Timer timer;
private int x = 10; // x position
private int y = 10; // y position
private boolean right = true;
private boolean up = true;
public MovePlane() {
timer = new Timer(delay, this);
timer.start(); // start the timer - infinite
}
public void actionPerformed(ActionEvent e) {
// will run when the timer fires
repaint();
}
public void paintComponent(Graphics g) {
// both paint and paintComponent work - difference?
super.paintComponent(g); // call superclass's paintComponent
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);
if (right && up) {
x++;
if (x == getWidth()-25) {
right = false;
up = false;
}
} else if (!right && !up) {
y++;
if (y == getHeight()-10) {
up = true;
}
} else {
if (x <= getWidth()-15 && x > 0) {
x--;
}
if (x == 0) {
y--;
}
if (y == 10) {
right = true;
}
}
}
public static void main(String args[]) {
JFrame frame = new JFrame("Move Plane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MovePlane bp = new MovePlane();
frame.add(bp);
frame.setSize(300, 300); // set frame size
frame.setVisible(true); // display frame
}
}