code_junkie

My (Java/Swing) MouseListener isn't listening, help me figure out why

Nov 10th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class DisplayArea extends JPanel implements MouseListener, MouseMotionListener {
  6. public DisplayArea(Rectangle bounds, Display display) {
  7. setLayout(null);
  8. setBounds(bounds);
  9. setOpaque(false);
  10. setPreferredSize(new Dimension(bounds.width, bounds.height));
  11.  
  12. this.display = display;
  13. }
  14.  
  15. public void paintComponent(Graphics g) {
  16. Graphics2D g2 = (Graphics2D)g;
  17. if (display.getControlPanel().Antialiasing()) {
  18. g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  19. }
  20. g2.setColor(Color.white);
  21. g2.fillRect(0, 0, getWidth(), getHeight());
  22. }
  23.  
  24. public void mousePressed(MouseEvent event) {
  25. System.out.println("mousePressed()");
  26. mx1 = event.getX();
  27. my1 = event.getY();
  28. }
  29.  
  30. public void mouseReleased(MouseEvent event) {
  31. System.out.println("mouseReleased()");
  32. mx2 = event.getX();
  33. my2 = event.getY();
  34.  
  35. int mode = display.getControlPanel().Mode();
  36. switch (mode) {
  37. case ControlPanel.LINE:
  38. System.out.println("Line from " + mx1 + ", " + my1 + " to " + mx2 + ", " + my2 + ".");
  39. }
  40. }
  41.  
  42. public void mouseEntered(MouseEvent event) {
  43. System.out.println("mouseEntered()");
  44. }
  45.  
  46. public void mouseExited(MouseEvent event) {
  47. System.out.println("mouseExited()");
  48. }
  49.  
  50. public void mouseClicked(MouseEvent event) {
  51. System.out.println("mouseClicked()");
  52. }
  53.  
  54. public void mouseMoved(MouseEvent event) {
  55. System.out.println("mouseMoved()");
  56. }
  57.  
  58. public void mouseDragged(MouseEvent event) {
  59. System.out.println("mouseDragged()");
  60. }
  61.  
  62. private Display display = null;
  63.  
  64. private int mx1 = -1;
  65. private int my1 = -1;
  66. private int mx2 = -1;
  67. private int my2 = -1;
  68. }
  69.  
  70. da = new DisplayArea(new Rectangle(CONTROL_WIDTH, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT), this);
  71.  
  72. this.addMouseListener(this);
  73. this.addMouseMotionListener(this);
Add Comment
Please, Sign In to add comment