Skip to content

Instantly share code, notes, and snippets.

@Sciss
Created July 14, 2011 20:17
Show Gist options
  • Save Sciss/1083339 to your computer and use it in GitHub Desktop.
Save Sciss/1083339 to your computer and use it in GitHub Desktop.
Arbitrarily shaped windows with Swing on OS X
// arbitrary shape windows (os x)
object Test {
import javax.swing._
import java.awt._
val f = new JFrame()
f.setUndecorated(true)
f.setBackground( new Color(0,0,0,0) )
f.setSize( 400, 400 )
val cp = f.getContentPane
class Gugu extends JComponent {
var fun = (g:Graphics2D) => ()
override def paintComponent( g: Graphics ) {
super.paintComponent(g)
fun(g.asInstanceOf[Graphics2D])
}
}
val g = new Gugu
cp.add( g, BorderLayout.CENTER )
g.fun = { g2 => g2.setColor(Color.red); g2.drawLine(0,0,g.getWidth,g.getHeight)}
// prevent the window from being dragged with the mouse
f.getRootPane.putClientProperty("apple.awt.draggableWindowBackground",java.lang.Boolean.FALSE)
f.setVisible(true)
g.fun = { g2 => g2.setColor(Color.green); g2.fillOval(0,0,g.getWidth,g.getHeight)}
g.repaint() // hmmm, the old stuff is still there
g.fun = { g2 => g2.setColor(Color.blue); g2.drawOval(0,0,g.getWidth-1,g.getHeight-1)}
cp.remove(g)
f.repaint() // hmmm, the old stuff is still there
// but this works
cp.add(g)
g.fun = { g2 => g2.clearRect(0,0,100,100)}
cp.repaint()
g.fun = { g2 => for(i<-0 to 20) { g2.clearRect(i*20,0,10,400)}}
cp.repaint()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment