I am trying to get basic intersections to work with a game I am developing and because of that I need to define their hitboxes. However when using g2d.draw(rectangle) the rectangle doesn't move relative to its updated coordinates.
int x = 100 ;
int y = 100 ;
int x2 = x + 100;
int y2 = y + 100;
Rectangle hitbox = new Rectangle(x,y,x2,y2) ;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Graphical loop start
g2d.draw(hitbox) ;
repaint() ;
//Graphical loop end
}
The gameloop with the keylistener components
public void run() {
while(running) {
//Player movement
if (left) {
if (x <= -225) {
x = 1440 ;
}
x = x - 2 ;
}
if (up) {
if(y <= -225) {
y = 900 ;
}
y = y - 2 ;
}
if (right) {
if (x >= 1416) {
x = -24 ;
}
x = x + 2;
}
if (down) {
if (y >= 900) {
y = -10 ;
}
y = y + 2 ;
}
//Player movement
//ball movement
if (cubey > y) {
cubey-- ;
}
if(cubey < y) {
cubey++ ;
}
if (cubex > x) {
cubex-- ;
}
if (cubex < x) {
cubex++ ;
}