My program won't recognize the mouse's motions~
I have a mediocre knowledge of Java (I know the fundamentals) and have been watching a youtube series to learn how to program games better. The video, Java Game Programming for Beginners - #10 - More Collision Detection - YouTube, is about collision detection, but for some reason with the code I have here, the rectangle won't even follow the mouse. Side note: Any favorite youtube channels for Java would be much appreciated as well.
package collision;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class Main extends JFrame {
private Image dbImage;
private Graphics dbg;
int rectX, rectY;
public Main(){
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
addMouseMotionListener(new AL());
}
@Override
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){
Rectangle r1 = new Rectangle(rectX, rectY, 25, 25);
Rectangle r2 = new Rectangle(175, 175, 50, 50);
g.setColor(Color.RED);
g.fillRect(r2.x, r2.y, r2.width, r2.height);
g.setColor(Color.BLUE);
g.fillRect(r1.x, r1.y, r1.width, r1.height);
}
public class AL extends MouseAdapter {
@Override
public void mouseMoved(MouseEvent e){
rectX = e.getX()-12;
rectY = e.getY()-12;
}
}
public static void main(String[] args) {
Main main = new Main();
}
}
Re: My program won't recognize the mouse's motions~
False Alarm. I forgot to repaint. Anyone know how to delete a thread?