Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 56 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  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 ResizeMode {
  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 ResizeMode resizeMode;
  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.             resizeMode = ResizeMode.LEFT;
  57.         else if (startX >= getWidth() - BORDER_WIDTH)
  58.             resizeMode = ResizeMode.RIGHT;
  59.         else if (startY <= BORDER_WIDTH)
  60.             resizeMode = ResizeMode.TOP;
  61.         else if (startY >= getHeight() - BORDER_WIDTH)
  62.             resizeMode = ResizeMode.BOTTOM;
  63.     }
  64.  
  65.     public void mouseReleased(MouseEvent e) {
  66.         startX = startY = -1;
  67.         resizeMode = 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(resizeMode == null || resizeMode == ResizeMode.LEFT || resizeMode == ResizeMode.TOP)
  81.                 setLocation(p.x + mouse.x - startX, p.y + mouse.y - startY);
  82.             if(resizeMode == ResizeMode.LEFT)
  83.                 setSize(getWidth() + startX - mouse.x, getHeight());
  84.             else if(resizeMode == ResizeMode.RIGHT)
  85.                 setSize(getWidth() + mouse.x - lastX, getHeight());
  86.             else if(resizeMode == ResizeMode.TOP)
  87.                 setSize(getWidth(), getHeight() + startY - mouse.y);
  88.             else if(resizeMode == ResizeMode.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.             resizeMode = ResizeMode.LEFT;
  102.         else if (mouse.x >= us.x + getWidth() - BORDER_WIDTH)
  103.             resizeMode = ResizeMode.RIGHT;
  104.         else if (mouse.y <= us.y + BORDER_WIDTH)
  105.             resizeMode = ResizeMode.TOP;
  106.         else if (mouse.y >= us.y + getHeight() - BORDER_WIDTH)
  107.             resizeMode = ResizeMode.BOTTOM;
  108.         else {
  109.             resizeMode = null;
  110.             setCursor(Cursor.getDefaultCursor());
  111.         }
  112.         if (resizeMode != null) {
  113.             Cursor c = Cursor.getDefaultCursor();
  114.             if (resizeMode == ResizeMode.LEFT)
  115.                 c = new Cursor(Cursor.W_RESIZE_CURSOR);
  116.             else if (resizeMode == ResizeMode.RIGHT)
  117.                 c = new Cursor(Cursor.E_RESIZE_CURSOR);
  118.             else if (resizeMode == ResizeMode.TOP)
  119.                 c = new Cursor(Cursor.N_RESIZE_CURSOR);
  120.             else if (resizeMode == ResizeMode.BOTTOM)
  121.                 c = new Cursor(Cursor.S_RESIZE_CURSOR);
  122.             setCursor(c);
  123.         }
  124.     }
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top