Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fifteen;
- import java.awt.event.*;
- import javax.swing.*;
- /**
- * @author SilverKitsune
- */
- public class FifteenEvent implements ActionListener
- {
- /**
- * Moves the button clicked, where possible.
- */
- public void actionPerformed(ActionEvent ae)
- {
- JButton b = (JButton)ae.getSource();
- int x = b.getX();
- int y = b.getY();
- int dx = (int)b.getMaximumSize().getWidth();
- int dy = (int)b.getMaximumSize().getHeight();
- if(x + dx <= 4 * dx && checkFree(b, x + dx, y))
- b.setLocation(x + dx, y);
- else if(x - dx >= 0 && checkFree(b, x - dx, y))
- b.setLocation(x - dx, y);
- else if(y + dy <= 4 * dy && checkFree(b, x, y + dy))
- b.setLocation(x, y + dy);
- else if(y - dy >= 0 && checkFree(b, x, y - dy))
- b.setLocation(x, y - dy);
- else;
- }
- /**
- * Checks if the button can be moved.
- * @param b The button to move.
- * @param x The new X coordinate of the button.
- * @param y The new Y coordinate of the button.
- * @return True if the button can be moved, false otherwise.
- */
- private boolean checkFree(JButton b, int x, int y)
- {
- return b.getParent().getComponentAt(x, y) != FifteenFrame.one &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.two &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.three &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.four &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.five &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.six &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.seven &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.eight &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.nine &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.ten &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.eleven &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.twelve &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.thirteen &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.fourteen &&
- b.getParent().getComponentAt(x, y) != FifteenFrame.fifteen;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement