Guest User

Untitled

a guest
Apr 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. package net.newbiehacker.nengine.mapeditor;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.border.LineBorder;
  5. import java.awt.*;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;
  9.  
  10. /**
  11. * @author newbiehacker
  12. */
  13. public class ToolWindow extends JWindow implements MouseListener, MouseMotionListener {
  14. private static enum DragMode {
  15. TOP, BOTTOM, RIGHT, LEFT
  16. }
  17.  
  18. private static final int BORDER_WIDTH = 3;
  19. private int startX = -1, startY = -1, lastX = -1, lastY = -1;
  20. private DragMode dragMode;
  21.  
  22. public ToolWindow(JFrame owner, String title, Component inside) {
  23. super(owner);
  24. final Color bg = new Color(102, 153, 255);
  25. ((JPanel) getContentPane()).setBorder(new LineBorder(bg, BORDER_WIDTH));
  26. JLabel t = new JLabel(title, JLabel.CENTER);
  27. getContentPane().setBackground(bg);
  28. t.setFont(t.getFont().deriveFont(Font.BOLD, 12));
  29. add(t, BorderLayout.NORTH);
  30. add(inside, BorderLayout.CENTER);
  31. addMouseListener(this);
  32. addMouseMotionListener(this);
  33. addMouseHandler(inside);
  34. setAlwaysOnTop(true);
  35. pack();
  36. }
  37.  
  38. private void addMouseHandler(Component c) {
  39. c.addMouseMotionListener(this);
  40. if (c instanceof Container) {
  41. Container cont = (Container) c;
  42. for (Component c1 : cont.getComponents()) {
  43. addMouseHandler(c1);
  44. }
  45. }
  46. }
  47.  
  48. public void mouseClicked(MouseEvent e) {
  49. }
  50.  
  51. public void mousePressed(MouseEvent e) {
  52. Point mouse = e.getPoint();
  53. lastX = startX = mouse.x;
  54. lastY = startY = mouse.y;
  55. if (startX <= BORDER_WIDTH)
  56. dragMode = DragMode.LEFT;
  57. else if (startX >= getWidth() - BORDER_WIDTH)
  58. dragMode = DragMode.RIGHT;
  59. else if (startY <= BORDER_WIDTH)
  60. dragMode = DragMode.TOP;
  61. else if (startY >= getHeight() - BORDER_WIDTH)
  62. dragMode = DragMode.BOTTOM;
  63. }
  64.  
  65. public void mouseReleased(MouseEvent e) {
  66. startX = startY = -1;
  67. dragMode = null;
  68. }
  69.  
  70. public void mouseEntered(MouseEvent e) {
  71. }
  72.  
  73. public void mouseExited(MouseEvent e) {
  74. }
  75.  
  76. public void mouseDragged(MouseEvent e) {
  77. if (e.getSource() == this) {
  78. Point mouse = e.getPoint();
  79. Point p = getLocation();
  80. if(dragMode == null || dragMode == DragMode.LEFT || dragMode == DragMode.TOP)
  81. setLocation(p.x + mouse.x - startX, p.y + mouse.y - startY);
  82. if(dragMode == DragMode.LEFT)
  83. setSize(getWidth() + startX - mouse.x, getHeight());
  84. else if(dragMode == DragMode.RIGHT)
  85. setSize(getWidth() + mouse.x - lastX, getHeight());
  86. else if(dragMode == DragMode.TOP)
  87. setSize(getWidth(), getHeight() + startY - mouse.y);
  88. else if(dragMode == DragMode.BOTTOM)
  89. setSize(getWidth(), getHeight() + mouse.y - lastY);
  90. lastX = mouse.x;
  91. lastY = mouse.y;
  92. }
  93. }
  94.  
  95. public void mouseMoved(MouseEvent e) {
  96. Point mouse = e.getLocationOnScreen();
  97. Point us = getLocationOnScreen();
  98. startX = e.getX();
  99. startY = e.getY();
  100. if (mouse.x <= us.x + BORDER_WIDTH)
  101. dragMode = DragMode.LEFT;
  102. else if (mouse.x >= us.x + getWidth() - BORDER_WIDTH)
  103. dragMode = DragMode.RIGHT;
  104. else if (mouse.y <= us.y + BORDER_WIDTH)
  105. dragMode = DragMode.TOP;
  106. else if (mouse.y >= us.y + getHeight() - BORDER_WIDTH)
  107. dragMode = DragMode.BOTTOM;
  108. else {
  109. dragMode = null;
  110. setCursor(Cursor.getDefaultCursor());
  111. }
  112. if (dragMode != null) {
  113. Cursor c = Cursor.getDefaultCursor();
  114. if (dragMode == DragMode.LEFT)
  115. c = new Cursor(Cursor.W_RESIZE_CURSOR);
  116. else if (dragMode == DragMode.RIGHT)
  117. c = new Cursor(Cursor.E_RESIZE_CURSOR);
  118. else if (dragMode == DragMode.TOP)
  119. c = new Cursor(Cursor.N_RESIZE_CURSOR);
  120. else if (dragMode == DragMode.BOTTOM)
  121. c = new Cursor(Cursor.S_RESIZE_CURSOR);
  122. setCursor(c);
  123. }
  124. }
  125. }
Add Comment
Please, Sign In to add comment