Java Creating irregular windows

Source: Internet
Author: User

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 >> & 0xFF;
}
}

Java Creating irregular windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.