View Single Post
Old 05-21-2012, 04:27 PM   PM User | #1
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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);
    }

dan-dan is offline   Reply With Quote