Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 05-21-2012, 04:42 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
dan-dan (05-21-2012)
Old 05-21-2012, 06:29 PM   PM User | #3
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
I just added the getParent() method to the existing repaint() methods and it works perfectly.

Once again, thanks for your help Fou-Lu.
dan-dan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:04 AM.


Advertisement
Log in to turn off these ads.