test2.java


test2.java

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;


class test2 extends JPanel implements MouseMotionListener
{
    Point base, current;
    growbox r;
    
    test2()
    {
	addMouseListener(new MouseHandler());
	addMouseMotionListener(this);
    }

    public void paintComponent(Graphics g)
    {
	super.paintComponent(g);
	if (r!=null) r.draw(g);
    }

   int moving = 0;

    private class MouseHandler extends MouseAdapter
    {
	public void mousePressed(MouseEvent event)
	{
	    current = event.getPoint();
	    base = event.getPoint();
	    switch (getCursor().getType()) {
	    case Cursor.MOVE_CURSOR:
		moving = 1;
		break;		
	    case Cursor.NW_RESIZE_CURSOR:
		current.x = r.xmin;
		current.y = r.ymin;
		base.x = r.xmax;
		base.y = r.ymax;
		break;
	    case Cursor.NE_RESIZE_CURSOR:
		current.x = r.xmax;
		current.y = r.ymin;
		base.x = r.xmin;
		base.y = r.ymax;
		break;
	    case Cursor.SE_RESIZE_CURSOR:
		current.x = r.xmax;
		current.y = r.ymax;
		base.x = r.xmin;
		base.y = r.ymin;
		break;
	    case Cursor.SW_RESIZE_CURSOR:
		current.x = r.xmin;
		current.y = r.ymax;
		base.x = r.xmax;
		base.y = r.ymin;
		break;
	   case Cursor.W_RESIZE_CURSOR:
		moving = 2;
		current.x = r.xmin;
		current.y = r.ymin;
		base.x = r.xmax;
		base.y = r.ymax;
		break;
	    case Cursor.E_RESIZE_CURSOR:
		moving = 2;
		current.x = r.xmax;
		current.y = r.ymax;
		base.x = r.xmin;
		base.y = r.ymin;
		break;
	    case Cursor.N_RESIZE_CURSOR:
		moving = 3;
		current.x = r.xmin;
		current.y = r.ymin;
		base.x = r.xmax;
		base.y = r.ymax;
		break;
	    case Cursor.S_RESIZE_CURSOR:
		moving = 3;
		current.x = r.xmax;
		current.y = r.ymax;
		base.x = r.xmin;
		base.y = r.ymin;
		break;
	    }

	}


	public void mouseReleased(MouseEvent event)
	{
	    moving = 0;
	}
    }

    public void mouseDragged(MouseEvent event)
    {
	int bx = base.x;
	int by = base.y;
	if (r==null) r = new growbox();
	current = event.getPoint();
	switch (moving) {
	case 1:
		int dx = current.x - bx;
		int dy = current.y -by;
		r.xmin  += dx;
		r.xmax  += dx;
		r.ymin  += dy;
		r.ymax  += dy;
		base = current;
		break;
	case 2:
		int cy = (by==r.ymin)? r.ymax: r.ymin;
		r.set(bx,by,current.x,cy);
		break;
	case 3:
		int cx = (bx==r.xmin)? r.xmax: r.xmin;	
		r.set(bx,by,cx,current.y);
		break;
	default:
		r.set(bx,by,current.x,current.y);
	}
	repaint();
    }

    public boolean near(int x1, int y1, int x2,int y2) {
	if (Math.abs(x2-x1)<3 && Math.abs(y2-y1)<3) return true;
	return false;
   }

    public void mouseMoved(MouseEvent event)
    {
	if (r==null) return;
	Cursor c;
	int x = event.getX();
	int y = event.getY();
	if (near(r.xmin,r.ymin,x,y)) 
		c = Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
	else if (near(r.xmax,r.ymin,x,y))
		c = Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
	else if (near(r.xmin,r.ymax,x,y))
		c = Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
	else if (near(r.xmax,r.ymax,x,y))
		c = Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
	else if (Math.abs(r.xmin-x)<3)
		c = Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
	else if (Math.abs(r.xmax-x)<3)
		c = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
	else if (Math.abs(r.ymin-y)<3)
		c = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
	else if (Math.abs(r.ymax-y)<3)
		c = Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
	else if (r.contains(event.getX(),event.getY())) 
		c = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR );
	else c = Cursor.getDefaultCursor();
	setCursor(c );
    }

   public static void main(String[] args)
   {
       EventQueue.invokeLater(new Runnable() {
	   public void run()
	   {
	       test2 panel = new test2();
	       JFrame frame = new JFrame("growbox Test2");
	       frame.add(panel);
	       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	       frame.setSize(400,400);
	       frame.setVisible(true);
	   }
       });
   }
}


Maintained by John Loomis, updated Mon Mar 16 16:48:48 2020