Skip to content

Instantly share code, notes, and snippets.

@oscarryz oscarryz/gist:999815
Created May 31, 2011

Embed
What would you like to do?
Recortes.ryz
//;# http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
recortes {
AWTUtilitiesWrapper {
import( java.awt.GraphicsConfiguration )
import( java.awt.Shape )
import( java.awt.Window )
import( java.lang.reflect.InvocationTargetException )
import( java.lang.reflect.Method )
import( java.util.logging.Level )
import( java.util.logging.Logger )
//; Classes
__ awtUtilitiesClass : Class
__ translucencyClass : Class
//; Methods
__ mIsTranslucencySupported : Method
__ mIsTranslucencyCapable : Method
__ mSetWindowShape : Method
__ mSetWindowOpacity : Method
__ mSetWindowOpaque : Method
__ init() {
awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities")
translucencyClass = Class.forName("com.sun.awt.AWTUtilities$Translucency")
mIsTranslucencySupported = awtUtilitiesClass.getMethod("isTranslucencySupported", translucencyClass)
mIsTranslucencyCapable = awtUtilitiesClass.getMethod("isTranslucencyCapable" , GraphicsConfiguration.class)
mSetWindowShape = awtUtilitiesClass.getMethod("setWindowShape" , Window.class, Shape.class)
mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity" , Window.class, float.class)
mSetWindowOpaque = awtUtilitiesClass.getMethod("setWindowOpaque" , Window.class, boolean.class)
}
- __ set(method: Method , window: Window , value: Object ) {
method.invoke(null, window, value)
}
__ setWindowOpacity(window:Window , opacity: Float ) {
set(mSetWindowOpacity, window, opacity )
}
__ setWindowOpaque(window:Window , opaque: Boolean ) {
set(mSetWindowOpaque, window, opaque )
}
}
import( java.awt.* )
import( javax.swing.* )
import( java.awt.image.* )
import( java.awt.event.* )
import( java.util.Date )
import( java.io.File )
import( javax.imageio.ImageIO )
import( javax.swing.border.Border )
import( javax.swing.filechooser.FileSystemView )
Recortes : MouseAdapter {
loaded : BufferedImage
result : BufferedImage
original : BufferedImage
helper = RecortesHelper()
robot : Robot = helper.getRobot()
frame = JFrame()
dialog = JDialog( frame )
label = JLabel()
text = JLabel("<html><div><i>Drag to create the snippet</i></div></html>")
html = "<html>
<i>Image saved in:<br>
<font size="11">%s</font>
</i>
</html>
"
isDragging = false
finished = false
x = 0
y = 0
x0 = 0
y0 = 0
main() {
AWTUtilitiesWrapper.init()
d : Dimension = Toolkit.getDefaultToolkit().getScreenSize()
frame.setUndecorated( true )
frame.setAlwaysOnTop( true )
label.setOpaque( false )
frame.add( label )
frame.addMouseListener( self )
frame.addMouseMotionListener( self )
//; OSX toolbar
//;frame.setLocation(0, -6 )
frame.setLocation( 0, 0 )
frame.setSize( d )
frame.setVisible( true )
dialog.setUndecorated( true )
AWTUtilitiesWrapper.setWindowOpacity( frame, 0.3f )
AWTUtilitiesWrapper.setWindowOpaque( dialog, false )
font = Font(Font.SANS_SERIF, Font.PLAIN , 33 )
background = Color( 0,0,0,220 )
foreground = Color( 255,255,255,255 )
border : Border = BorderFactory.createEmptyBorder(10,10,20,20)
text.setBorder( border )
text.setFont( font )
text.setBackground( background )
text.setForeground( foreground )
text.setOpaque( true )
//;Info dialog
dialog.add( text )
dialog.setSize(1,1)
dialog.pack()
dialog.setLocationRelativeTo(frame)
dialog.setLocation( dialog.getLocation().x, 100 )
dialog.setVisible( true )
}
loadTranslucentImage() : BufferedImage {
loaded.isNull?((){
out.println("loaded was null")
d : Dimension = Toolkit.getDefaultToolkit().getScreenSize()
original = BufferedImage(d.width,d.height,BufferedImage.TRANSLUCENT)
})
loaded.notNull?((){
original = BufferedImage(loaded.getWidth(), loaded.getHeight(), BufferedImage.TRANSLUCENT)
snip : BufferedImage = BufferedImage( sx(), sy(), BufferedImage.OPAQUE )
o : Graphics2D = original.createGraphics()
o.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.3f ))
o.drawImage( loaded, null, 0 , 0 )
s : Graphics2D = snip.createGraphics()
//; x * -1 + x - x0
//; x .* -1 .+ x .- x0
s.drawImage(loaded, null, x .* (-1) .+ ( x ) .- (x0) , y0.*(-1))
s.dispose()
result = snip
//;Draw the red square...
o.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 1.0f ))
o.drawImage( snip, null , x0, y0 )
color : Color = o.getColor()
o.setColor( Color.red )
o.drawRect( x0, y0, snip.getWidth().-(1) , snip.getHeight().-(1) )
o.drawRect( x0.+(1), y0.+(1), snip.getWidth().-(3) , snip.getHeight().-(3) )
o.setColor( color )
o.dispose()
})
original
}
//; mouse listener interface:
//; When the user releases the mouse, is because
//; he has finished with the screen shot
mouseReleased( e : MouseEvent ) {
isDragging = false
out.println( "Result is null!")
result.isNull?({
out.println( "Result is null!")
})
result.notNull?({
out.println( "Salvando")
baseDir : String = FileSystemView.getFileSystemView().getHomeDirectory().getPath()
now = Date()
filePathName : String = String.format("%td-%1$tm-%1$ty-%1$tH-%1$tM-%1$tS.png",now)
//; OSX Desktop folder
//;file : File = File( String.format("%s/%s",baseDir,filePathName))
file : File = File( String.format("%s/Desktop/demo/%s",baseDir,filePathName))
out.println( file )
helper.write( result, file )
text.setText( String.format( html ,filePathName))
dialog.pack()
dialog.setVisible( true )
finished = true
})
}
//;# If we've finished already
//;# then exit
mouseMoved(e : MouseEvent ) {
finished.isTrue?({
helper.exit()
})
}
//;#Start drawing the red square to select the area to take
//;#the snippet from...
mouseDragged( e : MouseEvent ) {
p : Point = e.getPoint()
x = p.x()
y = p.y()
//;isDragging is false, when we haven't dragged at all
//;( like the first time)
//;After this isDragging turns true
isDragging.isFalse?((){
dialog.setVisible( false )
x0 = x
y0 = y
isDragging = true
AWTUtilitiesWrapper.setWindowOpacity( frame, 0.0f )
rectangle : Rectangle = Rectangle( Toolkit.getDefaultToolkit().getScreenSize() )
loaded = robot.createScreenCapture( rectangle )
AWTUtilitiesWrapper.setWindowOpacity( frame, 1.0f )
AWTUtilitiesWrapper.setWindowOpaque( frame, true )
})
i : ImageIcon = ImageIcon( loadTranslucentImage() )
label.setIcon( i )
}
//;Utility methods to get x and y from point
- x( aPoint : Point ):Integer {
ad : Double = aPoint.getX()
ad.intValue()
}
- y( aPoint : Point ):Integer {
ad : Double = aPoint.getY()
ad.intValue()
}
sx = 1
sy = 1
- sx() : Integer {
sx = x.-(x0)
ltEq0 : Boolean = sx.<(1)
ltEq0.isTrue?({
sx = 1
})
sx
}
- sy() : Integer {
sy : Int = y.-(y0)
ltEq0 : Boolean = sy.<(1)
ltEq0.isTrue?({
sy = 1
})
sy
}
}
import( java.io.File )
import( java.io.* )
import( java.awt.Robot )
import( java.awt.Desktop )
import( javax.imageio.ImageIO)
import( java.awt.image.BufferedImage )
RecortesHelper {
writtenFile : File
link = "<div
align="center">
<img src="%s" width="768"/>
</div><br>
"
getRobot() : Robot {
r = Robot()
r
}
write( image : BufferedImage, file: File ) {
writtenFile = file
ImageIO.write( image, "png", writtenFile )
doc : File = File(file.getParent(), "doc.html")
fos : FileOutputStream = FileOutputStream(doc, true)
ps : PrintStream = PrintStream( fos )
ps.printf(link, file.getName() )
ps.close()
}
exit() {
Desktop.getDesktop().open( self.writtenFile )
System.exit(0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.