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(420, 220);
BorderLayout border = new BorderLayout();
setLayout(border);
JLabel message = new JLabel("Click OK to close this program.");
add(BorderLayout.NORTH, message);
PrankPanel prank = new PrankPanel();
prank.ok.addActionListener(this);
add(BorderLayout.CENTER, prank);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
public Insets getInsets() {
return new Insets(40, 10, 10, 10);
}
public static void main(String[] args) {
new MousePrank();
}
}
class PrankPanel extends JPanel implements MouseMotionListener {
JButton ok = new JButton("OK");
int buttonX, buttonY, mouseX, mouseY;
int width, height;
PrankPanel() {
super();
setLayout(null);
addMouseMotionListener(this);
buttonX = 110;
buttonY = 110;
ok.setBounds(new Rectangle(buttonX, buttonY, 70, 20));
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(mouseX, buttonX, width);
repaint();
}
if (Math.abs((mouseY + 10) - buttonY) < 50) {
buttonY = moveButton(mouseY, buttonY, height);
repaint();
}
}
public void mouseDragged(MouseEvent event) {
// Ignore event
}
private int moveButton(int mouseAt, int buttonAt, int 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(buttonX, buttonY, 70, 20);
}
}