Results 1 to 2 of 2
  1. #1
    jim.f.hurley is offline Member
    Join Date
    Sep 2012
    Location
    MI
    Posts
    6
    Rep Power
    0

    Question 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();
    }
    }
    Last edited by jim.f.hurley; 09-20-2012 at 09:33 PM.

  2. #2
    jim.f.hurley is offline Member
    Join Date
    Sep 2012
    Location
    MI
    Posts
    6
    Rep Power
    0

    Default Re: My program won't recognize the mouse's motions~

    False Alarm. I forgot to repaint. Anyone know how to delete a thread?

Similar Threads

  1. Java program in Eclipse help (Mouse events in applet)
    By M-Dub4321 in forum New To Java
    Replies: 6
    Last Post: 06-11-2012, 04:06 PM
  2. Mouse Lag on A JApplet Paint program.
    By timo0060 in forum Java 2D
    Replies: 5
    Last Post: 04-30-2012, 08:20 PM
  3. Replies: 7
    Last Post: 04-05-2012, 06:15 AM
  4. Replies: 9
    Last Post: 06-17-2011, 05:10 AM
  5. Is this Mouse program possible?
    By gblax123 in forum New To Java
    Replies: 4
    Last Post: 03-26-2011, 10:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •