Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts

    Mouse prank gone wild

    This is an example from a book I'm reading. I've looked over the code a few times and everything seems indentical to my example. It also doesn't return any errors.

    Instead of the button just moving, the JLabel duplicates, and the JButton creates a new instance of itself whenever it reaches the edge of the rectangle. It aso drags the background with it (essentially changing its colour).

    It's so difficult to study a language when the exmples themselves are bugged. Any help would be greatly appreciated

    PHP Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class 
    MousePrank extends JFrame implements ActionListener {
        public 
    MousePrank() {
            
    super("Message");
            
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
    setSize(420220);
            
    BorderLayout border = new BorderLayout();
            
    setLayout(border);
            
    JLabel message = new JLabel("Click OK to close this program.");
            
    add(BorderLayout.NORTHmessage);
            
    PrankPanel prank = new PrankPanel();
            
    prank.ok.addActionListener(this);
            
    add(BorderLayout.CENTERprank);
            
    setVisible(true);
        }

        public 
    void actionPerformed(ActionEvent event) {
            
    System.exit(0);
        }
        
        public 
    Insets getInsets() {
            return new 
    Insets(40101010);
        }
        
        public static 
    void main(String[] args) {
            new 
    MousePrank();

        }
    }

    class 
    PrankPanel extends JPanel implements MouseMotionListener {
        
    JButton ok = new JButton("OK");
        
    int buttonXbuttonYmouseXmouseY;
        
    int widthheight;
        
        
    PrankPanel() {
            
    super();
            
    setLayout(null);
            
    addMouseMotionListener(this);
            
    buttonX 110;
            
    buttonY 110;
            
    ok.setBounds(new Rectangle(buttonXbuttonY7020));
            
    add(ok);
        }
        
        public 
    void mouseMoved(MouseEvent event) {
            
    mouseX event.getX();
            
    mouseY event.getY();
            
    width = (int)getSize().getWidth();
            
    height = (int)getSize().getHeight();
            if (
    Math.abs((mouseX 35) - buttonX) < 50) {
                
    buttonX moveButton(mouseXbuttonXwidth);
                
    repaint();
            }
            if (
    Math.abs((mouseY 10) - buttonY) < 50) {
                
    buttonY moveButton(mouseYbuttonYheight);
                
    repaint();
            }
        }
        
        public 
    void mouseDragged(MouseEvent event) {
            
    // Ignore event
        
    }
        
        private 
    int moveButton(int mouseAtint buttonAtint border) {
            if (
    buttonAt mouseAt)
                
    buttonAt--;
            else 
    buttonAt++;
            
            if (
    buttonAt > (border 20))
                    
    buttonAt 10;
            
            if (
    buttonAt 0)
                
    buttonAt border 80;
            
            return 
    buttonAt;
        }
        
        public 
    void paintComponent(Graphics comp) {
            
    super.paintComponents(comp);
            
    ok.setBounds(buttonXbuttonY7020);
        }


  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Just a repaint issue from the looks of it. Several fixes, but the cheapest and easiest one would be to add
    PHP Code:
    if (this.getParent() != null)
    {
        
    this.getParent().repaint();

    To the end of the mouseMoved() method.

    It may be easier to simply move everything into a single panel. This way you can cheat and won't need to worry about a double buffer.

  • Users who have thanked Fou-Lu for this post:

    dan-dan (05-21-2012)

  • #3
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    I just added the getParent() method to the existing repaint() methods and it works perfectly.

    Once again, thanks for your help Fou-Lu.


  •  

    Posting Permissions

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