package com.redpois0n.guiscrot;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

import org.jnativehook.GlobalScreen;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseMotionListener;

@SuppressWarnings("serial")
public class CoverFrame extends JFrame implements MouseMotionListener, MouseInputListener, NativeMouseMotionListener {
	
	public static final float OPACITY = 0.5F;
	
	private Rectangle rect;
	private Image image;
	
	private int x;
	private int y;
	
	private int x2;
	private int y2;
	
	private boolean dragging;
	
	
	public CoverFrame(Rectangle rect) {
		this(rect, null);
	}
	
	public CoverFrame(Rectangle rect, Image image) {
		this.rect = rect;
		this.image = image;
		GlobalScreen.addNativeMouseMotionListener(this);
		setUndecorated(true);
		setBounds(rect);
		setContentPane(new CoverPanel());

		if (image == null) {
			setOpacity(OPACITY);
		}
		
		Main.ACTIVE_COVER_FRAMES.add(this);
	}
	
	private class CoverPanel extends JPanel {
		
		@Override
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			
			int tx = x2 == 0 ? x : x2;
			int ty = y2 == 0 ? y : y2;
			
			if (image != null) {
				g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
				g.setColor(new Color(0, 0, 0, 100));		
								
				g.fillRect(0, 0, x, getHeight());
				g.fillRect(x, 0, getWidth(), y);			

				g.fillRect(tx, y, getWidth(), getHeight());
				g.fillRect(x, ty, tx - x, getHeight());
			} else {
				g.setColor(Color.black);
				g.fillRect(0, 0, getWidth(), getHeight());
			}	
			
			if (x2 != 0 && y2 != 0 && dragging) {
				g.setColor(Color.red);
				g.drawRect(x, y, tx - x, ty - x);
			}			
						
			g.setColor(Color.white);
			g.fillRect(x, 0, 1, getHeight());
			g.fillRect(0, y, getWidth(), 1);
		}
	}

	@Override
	protected void finalize() {
		Main.ACTIVE_COVER_FRAMES.remove(this);
	}

	@Override
	public void nativeMouseDragged(NativeMouseEvent arg0) {
		x2 = arg0.getX() - rect.x;
		y2 = arg0.getY() - rect.y ;
		
		repaint();
	}

	@Override
	public void nativeMouseMoved(NativeMouseEvent arg0) {
		if (!dragging) {
			x = arg0.getX() - rect.x;
			y = arg0.getY() - rect.y;
		}

		repaint();
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		dragging = true;
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		dragging = false;
		x2 = 0;
		y2 = 0;
		repaint();
	}

}

@@ -1384,24 +1384,219 @@
 ent(g);%0A%09%09%09%0A
+%09%09%09if (x %3E x2) %7B%0A%09%09%09%09int temp = x;%0A%09%09%09%09x = x2;%0A%09%09%09%09x2 = temp;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09if (y %3E y2) %7B%0A%09%09%09%09int temp = y;%0A%09%09%09%09y = y2;%0A%09%09%09%09y2 = temp;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09/** If nothing is selected, default to x and y**/%0A
 %09%09%09int tx = 
@@ -1667,24 +1667,58 @@
  != null) %7B%0A
+%09%09%09%09/** Draw image over frame **/%0A
 %09%09%09%09g.drawIm
@@ -1766,16 +1766,64 @@
  null);%0A
+%09%09%09%09%0A%09%09%09%09/** Set color to transparent black **/%0A
 %09%09%09%09g.se
@@ -1861,20 +1861,98 @@
 ;%09%09%0A%09%09%09%09
+%0A
 %09%09%09%09
+/** Draw black transparent color over all areas that isn't being selected **/
 %0A%09%09%09%09g.f
@@ -2435,16 +2435,115 @@
 (), 1);%0A
+%09%09%09%0A%09%09%09if (dragging) %7B%0A%09%09%09%09g.drawString(%22X: %22 + (x + rect.x) + %22, Y: %22 + (y + rect.y), x, y);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A
@@ -2751,17 +2751,16 @@
 - rect.y
- 
 ;%0A%09%09%0A%09%09r
@@ -3325,16 +3325,29 @@
 = true;%0A
+%09%09repaint();%0A
 %09%7D%0A%0A%09@Ov

fix for black squares over eachother