« Hybrid tutorial - Writing a desktop application in Javascript | Main | Hybrid tutorial - Teppefall Frame Extensions and the JJHMain parameter -XrunScriptOnOwnThread »

Hybrid tutorial - The anti-social image viewer

About
This application displays one image from your own desktop. It is cutting edge anti-social-ware.

Dependencies
You must create an ext/ folder and add bsf.jar, js.jar and commons-logging.jar into this folder.

antisocial.xml
<application script="antisocial.js">
<id>com.teppefall.example</id>
<name>The Anti-Social Image Viewer</name>
<version>1.0</version>
<initialWindow>
<title>The Anti-Social Image Viewer</title>
<content>anti_social_gui.jxml</content>
<systemChrome>normal</systemChrome>
</initialWindow>

<icon>
<image64x64>teppefall-stort-ikon.png</image64x64>
</icon>
</application>

anti_social_gui.jfc (Warning: dropping a Effects2D script into the Effects2D panel runs the code without a security sandbox!)
<component class="com.teppefall.ds.render2d.Effects2D" id="effects">
<property name="foreground" type="color" value="#ffffff"/>
<property name="background" type="color" value="#0000ff"/>
<property name="opaque" type="boolean" value="true"/>
</component>

antisocial.js
importPackage(Packages.java.lang)
importPackage(Packages.java.awt.event)
importPackage(Packages.java.io)
importPackage(Packages.java.awt)
importPackage(Packages.java.awt.image)
importPackage(Packages.javax.imageio)
importPackage(Packages.com.teppefall.ds.render2d)
importPackage(Packages.com.teppefall.ds.render2d.compositing)

context.info("I am " + context.getApplication().getApplicationName())
effects = context.getApplication().getAssembly().getComponentById("effects")
context.info(effects)

var ds, img, bg, desktop, file, files, cachedFilename, cachedFileSize, cachedDate
var transHeight = 40
var r = {
destroy : function() {
shutdown = true
},
initialize : function(ds, fgColor, bgColor) {
bg = ColorLogic.deriveTransparent(Color.BLACK, 200)
desktop = new File(System.getProperty("user.home") + "/Desktop")
context.info(desktop)
if(desktop != null && desktop.isDirectory()) {
context.info("find files")
files = desktop.listFiles(new FileFilter({
accept: function (file) {
if(file.getName().endsWith(".jpg") || file.getName().endsWith(".png"))
return true
return false
}
}))
if(files.length != 0) {
index = Math.floor(Math.random()*files.length)
context.info("index=" + index)
file = files[index]
context.info("file=" + file)
}
}
else {
file = new File("teppefall-stort-ikon.png")
}
if(file != null) {
img = ImageIO.read(file)
cachedFilename = file.getName()
cachedFileSize = Math.round(file.length() / 1024) + "KB"
cachedDate = new Date(file.lastModified())
}
logo = ImageIO.read(new File("teppefall-stort-ikon.png"))
},
render : function(g, size) {
g.setColor(Color.WHITE)
g.fillRect(0, 0, size.width, size.height)
g.setColor(Color.WHITE)
g.setClip(0,0,size.width, size.height)
if(img != null) {
g.drawImage(
img,
size.width/2-img.getWidth()/2,
size.height/2-img.getHeight()/2,
img.getWidth(), img.getHeight(), null
)
g.setColor(bg)
g.fillRect(
0,
size.height - transHeight,
size.width+1, // huh?
transHeight+1 // huh?
)
g.setColor(Color.WHITE)
g.drawString(
"Filename: " + cachedFilename +" Size: "+ cachedFileSize +" Modified: "+ cachedDate +" Pixel size: "+ img.getWidth() +"x"+ img.getHeight(),
20,
size.height - (transHeight/2) + (g.getFont().getSize() /2)
);
}
else {
g.setColor(Color.BLACK)
g.drawString("No picture", 20, 40);
}
}
}

effects.setRenderer(new D2DRenderer(r))

run.cmd
java -cp teppefall-runtime.jar com.teppefall.hybrid.app.JJHMain antisocial.xml

The Anti-Social Image Viewer (3).png


TrackBack

TrackBack URL for this entry:
http://www.installer.teppefall.com/movabletype/mt-tb.cgi/215

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

This website is all about alpha and beta software

Download non-beta software here