Original Post
Hey guys,
I posted early on how to build a program after learning the syntax. And so I decided too, so far I have gotten a bunch of shapes moving across a JFrame in Java. Now, I really want to add collision. After extensive searching on GameDev, I couldn't find exactly what I was looking to do, maybe someone could help me?
Here's my code:
In the paintComponent at the bottom I tried to draw 4 new shapes and make them as walls...Beyond that, I have no idea what I am doing or even if that was the right thing to do...The plan is, to have every shape bounce off the wall it collides with, so maybe someone could help me understand how to do it?
I posted early on how to build a program after learning the syntax. And so I decided too, so far I have gotten a bunch of shapes moving across a JFrame in Java. Now, I really want to add collision. After extensive searching on GameDev, I couldn't find exactly what I was looking to do, maybe someone could help me?
Here's my code:
import javax.swing.*;
import java.awt.*;
public class SimpleAnimation {
int x = 150;
int y = 250;
int xPos = 1150;
int yPos = 80;
int aPos = 150;
int bPos = 50;
int rPos = 10;
int zPos = 250;
private static final int WIDTH = 1280, HEIGHT = WIDTH / 2;
public void go() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
// speed
for (int i = 0; i < 270; i++) {
x += 55;
x -= 40;
yPos += 55;
yPos -= 40;
if (yPos > 800) {
yPos = yPos + 10;
}
aPos+= 20;
bPos++;
rPos += 5;
drawPanel.repaint();
try {
Thread.sleep(50);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class MyDrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.GREEN);
g.fillOval(x, y, 40, 40);
g.setColor(Color.WHITE);
g.fillRect(xPos, yPos, 40, 75);
g.setColor(Color.YELLOW);
g.fillArc(aPos, bPos, 1000, 500, 50, 50);
g.setColor(Color.WHITE);
g.fillRect(xPos - 500, yPos - xPos / 2, 40, 75);
g.setColor(Color.WHITE);
g.fillRect(xPos * 2, yPos * 2, 40, 75);
g.setColor(Color.WHITE);
g.fillRect(rPos, zPos, 40, 75);
g.setColor(Color.YELLOW);
g.fillOval(xPos - 100, yPos - 1000, 75, 75);
g.setColor(Color.BLACK); // top wall
g.fillRect(0, 0, 1280, 2);
g.setColor(Color.BLACK); // bottom wall
g.fillRect(1, 610, 1280, 2);
g.setColor(Color.BLACK); // left wall
g.fillRect(0, 0, HEIGHT, 615);
g.setColor(Color.BLACK); // Right wall
g.fillRect(1273, 1, 2, 615);
}
}
public static void main(String... args) {
SimpleAnimation gui = new SimpleAnimation();
gui.go();
}
}
In the paintComponent at the bottom I tried to draw 4 new shapes and make them as walls...Beyond that, I have no idea what I am doing or even if that was the right thing to do...The plan is, to have every shape bounce off the wall it collides with, so maybe someone could help me understand how to do it?