CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Mouse prank gone wild (http://www.codingforums.com/showthread.php?t=262075)

dan-dan 05-21-2012 04:27 PM

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);
    }



Fou-Lu 05-21-2012 04:42 PM

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.

dan-dan 05-21-2012 06:29 PM

I just added the getParent() method to the existing repaint() methods and it works perfectly.

Once again, thanks for your help Fou-Lu.


All times are GMT +1. The time now is 07:09 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.