Regarding this short video of basic collision detection:
https://www.youtube.com/watch?v=ptqhnmP8FY0&list=PL6E90696571998DC2
Can someone tell me why the upper boundary needs to be the size of the ball? I thought that an oval was drawn with a bounding rectangle starting from the upper left. If that is the case then it would seem that y==0 would be the upper boundary, but that is obviously not the case.
Summary question: why is the upper boundary 20 and not 0 if the bounding triangle starts in the upper left.
Here is the code:
public class JavaGame extends JFrame {
int x,y,sizeX = 350,sizeY=350;
//boolean erase = false;
private Image dbImage;
private Graphics dbg;
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT){
if(x<= 0)
x=0;
else
x-=5;
}
if (keyCode == e.VK_RIGHT){
if(x==sizeX-20)
x=sizeX-20;
else
x+=5;
}if (keyCode == e.VK_UP){
if(y==20)
y=20;
else
y-=5;
}if (keyCode == e.VK_DOWN){
if(y==sizeY-20)
y=sizeY-20;
else
y+=5;
}
/*if (keyCode == e.VK_S){
erase = true;
}*/
}
public void keyReleased(KeyEvent e){
}
}
public JavaGame(){
addKeyListener(new AL());
x=y=150;
setTitle("Java Game");
setBackground(Color.WHITE);
setSize(sizeX,sizeY);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g){
g.fillOval(x, y, 20, 20);
/*if(erase){
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 500);
erase = false;
}*/
repaint();
}
public static void main(String[] args) {
new JavaGame();
}
}