Shaped Windows in Java 1.6.0u10
Most of the demo I've found on this topics seems to show windows with simple geometries, thought I would show a cooler window shape.
And here is the code
import com.sun.awt.AWTUtilities;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.Scanner;
public class Test extends JFrame {
public Test() {
setUndecorated(true);
ClassLoader loader = getClass().getClassLoader();
if ( loader == null )
System.out.printf("loader == null%n");
JLabel imagel = new JLabel(new ImageIcon(loader.getResource("tux.png")));
setLocation(200,200);
setSize(220,260);
add(imagel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test window = new Test();
GeneralPath path = createTuxPath();
AffineTransform at = AffineTransform.getScaleInstance(220.0/334, 260.0/393);
AWTUtilities.setWindowShape(window, path.createTransformedShape(at));
}
private static GeneralPath createTuxPath() {
final String tuxpath="M 177,0 C 168,0 158,2 148,7 C 120,21 124,53 128,115 C 129,132 113,143 99,165 C 85,187 84,204 73,223 C 63,240 65,239 68,254 C 68,254 67,255 67,255 C 63,259 58,268 52,273 C 46,278 29,277 25,283 C 21,288 25,296 25,311 C 25,317 24,322 23,326 C 22,331 22,334 24,337 C 27,344 33,345 67,352 C 85,356 102,366 113,367 C 125,367 127,364 134,357 C 135,357 136,356 137,355 C 138,355 138,355 139,355 C 154,348 149,350 173,349 C 197,347 204,346 226,352 C 227,357 229,360 231,361 C 234,364 239,366 250,367 C 260,367 267,364 274,357 C 279,353 283,348 297,339 C 309,331 325,323 329,320 C 330,318 333,316 333,310 C 334,305 329,303 326,302 C 322,299 312,291 306,282 C 303,277 308,269 303,262 C 302,261 301,260 300,260 C 302,255 303,250 304,246 C 310,224 293,186 276,162 C 260,139 246,125 240,95 C 233,66 241,74 235,49 C 233,36 227,26 222,19 C 216,12 208,8 205,6 C 199,3 190,-0 177,0 z";
GeneralPath gp = new GeneralPath();
Scanner s = new Scanner(tuxpath);
s.useDelimiter("[ ,]");
String op = "A";
do {
switch (op.charAt(0)) {
case 'M':
gp.moveTo(s.nextFloat(),s.nextFloat());
break;
case 'C':
gp.curveTo(s.nextFloat(),s.nextFloat(),s.nextFloat(),s.nextFloat(),s.nextFloat(),s.nextFloat());
break;
case 'L':
gp.lineTo(s.nextFloat(),s.nextFloat());
break;
case 'Q': // && other operation??
System.out.printf("I don't know what to do with '%s'. Need to read SVG spec. first. Bye.%n", op);
System.exit(0);
}
op = s.next();
} while ( op.charAt(0) != 'z' );
s.close();
return gp;
}
}
Here is the image I used.