import com.sun.awt.AWTUtilities;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Area;
import java.awt.image.PixelGrabber;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
public class createShape
{
private Image img;
private JFrame jf;
private Point origin;
public createShape(JFrame jf, String image)
{
this.jf = jf;
MediaTracker mt = new MediaTracker(jf);
this.img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(image));
mt.addImage(this.img, 0);
try
{
mt.waitForAll();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
try
{
initialize();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void initialize()
throws IOException
{
this.jf.setSize(this.img.getWidth(null), this.img.getHeight(null));
this.jf.setUndecorated(true);
this.origin = new Point();
AWTUtilities.setWindowShape(this.jf, getImageShape(this.img));
this.jf.setLocationRelativeTo(null);
}
public void addDragLisener()
{
this.jf.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
createShape.this.origin.x = e.getX();
createShape.this.origin.y = e.getY();
}
public void mouseClicked(MouseEvent e)
{
if (e.getButton() == 3) {
System.exit(0);
}
}
public void mouseReleased(MouseEvent e)
{
super.mouseReleased(e);
}
public void mouseEntered(MouseEvent e)
{
createShape.this.jf.repaint();
}
});
this.jf.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point p = createShape.this.jf.getLocation();
createShape.this.jf.setLocation(p.x + e.getX() - createShape.this.origin.x, p.y + e.getY() - createShape.this.origin.y);
}
});
}
public Shape getImageShape(Image img)
{
ArrayList<Integer> x = new ArrayList();
ArrayList<Integer> y = new ArrayList();
int width = img.getWidth(null);
int height = img.getHeight(null);
PixelGrabber pgr = new PixelGrabber(img, 0, 0, -1, -1, true);
try
{
pgr.grabPixels();
}
catch (InterruptedException ex)
{
ex.getStackTrace();
}
int[] pixels = (int[])pgr.getPixels();
for (int i = 0; i < pixels.length; i++)
{
int alpha = getAlpha(pixels[i]);
if (alpha != 0)
{
x.add(Integer.valueOf(i % width > 0 ? i % width - 1 : 0));
y.add(Integer.valueOf(i % width == 0 ? i / width - 1 : i == 0 ? 0 : i / width));
}
}
int[][] matrix = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
matrix[i][j] = 0;
}
}
for (int c = 0; c < x.size(); c++) {
matrix[((Integer)y.get(c)).intValue()][((Integer)x.get(c)).intValue()] = 1;
}
Area rec = new Area();
int temp = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++) {
if (matrix[i][j] == 1)
{
if (temp == 0) {
temp = j;
} else if (j == width) {
if (temp == 0)
{
Rectangle rectemp = new Rectangle(j, i, 1, 1);
rec.add(new Area(rectemp));
}
else
{
Rectangle rectemp = new Rectangle(temp, i, j - temp, 1);
rec.add(new Area(rectemp));
temp = 0;
}
}
}
else if (temp != 0)
{
Rectangle rectemp = new Rectangle(temp, i, j - temp, 1);
rec.add(new Area(rectemp));
temp = 0;
}
}
temp = 0;
}
return rec;
}
private int getAlpha(int pixel)
{
return pixel >> 24 & 0xFF;
}
}