Tag:
20080523 Code & Form blog hacked

Screen capture of hacked front page for Code & Form.

It seems that this blog was hacked earlier today. Someone got access to write a new index.html, which took precedence over the index.php file. It doesn’t look like any data was deleted, I guess they just wanted to waste my time a little.

I don’t whether the site was hacked through FTP or via WordPress. I’ve changed all my passwords and backed up the database and all files related to the blog, hopefully that will help.

No Comments »

I’ve decided to put my Processing hacks from the Code & hacks page and consolidate them into a downloadable archive. Thus the Code & Form code library is born. It will contain various demos and hacks, as well as example code for the unlekkerLib library. It should be much easier to publish code this way, since students etc. can simply download the archive and dump the contents in their sketchbook folder for quick access.

I’ve started a Google Code repository for this purpose, which seems a good way to go. I’m still figuring out SVN etc, hopefully I can switch to publishing the unlekkerLib source “live” through SVN once I figure it out. I’ll also publish archives of code written during past workshops to the repository.

There seems to be a growing list of Processing-related Google Code repositories out there, examples include Toxi’s Toxiclibs, interfascia and jddaniels. Do a search for processing.org to find more.

2 Comments »

Turns out Java has no mechanism for discovering the names of disk volumes. You can use java.io.File.listRoots() to get the root paths for multiple disks, but you can’t auto-discover their names.

Here’s a quick hack that will work on Windows. It uses Runtime.getRuntime().exec() to run “cmd /c dir “, the output of which is parsed to get the volume name.

import java.io.*; 

void setup() {
  println("Testing getVolName");

  String path="D:";
  println("Volume name for "+path+" is: "+
    "'"+getDiskVolumeName(path)+"'");
}

public static String getDiskVolumeName(String path) {
  String volname="Unknown";
  String check="Volume in drive "+path.charAt(0)+" is ";
  try  {
    Process p=Runtime.getRuntime().exec("cmd /c dir "+path);
    //p.waitFor(); 

    BufferedReader reader=new BufferedReader(
      new InputStreamReader(p.getInputStream()));
    String line=reader.readLine();
    while(line!=null) {
      if(line.indexOf(check)!=-1)
        volname=line.substring(line.indexOf(check)+check.length());

//      System.out.println(line);
      line=reader.readLine();
    }
  }
  catch(Exception e1) {
    println("Failure: "+e1.toString());
  }
  return volname;
}

No Comments »

Sample code to use mouse wheel events in Processing.

Read the rest of this entry »

1 Comment »

I just added a Flickr badge to Generator.x using the excellent phpFlickr library. The code to retrieve the images from the Generator.x Flickr group is less than 20 lines. I turned it into a function and put inside a basic WP plugin shell, giving me a new API call for inserting into the sidebar.php template.

Code - gxflickr.php
/*
Plugin Name: Generator.x Flickr utilities
Plugin URI: http://www.generatorx.no/
Description: Support utilities for Generator.x web
Version: 1.0
Author: Marius Watz
Author URI: http://www.generatorx.no/
*/

require_once("phpFlickr/phpFlickr.php");

function gxFlickrSidebar() {
  $f = new phpFlickr("API_KEY");
  $f->enableCache(
    "dbname",
    "mysql://username:password@host/dbname"
  );

  echo("<div class=\"gxflickrSidebar\">");
  echo("<div class=\"gxFlickrTitle\"><a href=
      \"http://flickr.com/groups/generatorx/\">".
      "» GX on Flickr:</a> Recent images</div>");

  // get photos using the Generator.x group's ID
  $photos = $f->groups_pools_getPhotos('59096658@N00', NULL, NULL, NULL, 12);

  foreach ((array)$photos['photo'] as $photo) {
    echo “<a href=http://www.flickr.com/photos/” . $photo['owner'] .
      “/” . $photo['id'] .”/in/pool-generatorx>”;
    echo “<img border=’0′ alt=’$photo[ownername]: $photo[title]‘ src=” .
      $f->buildPhotoURL($photo, “Square”) . “>”;
    echo “</a>”;
  }
  echo(”</div>”);
}

Once that’s done, all you need to do is insert the function call in your Wordpress template:

[..html..]
  if (is_home() || is_page()) {
    gxFlickrSidebar();
  }
[..html..]

No Comments »

For two joined bezier curves to be continuous (i.e. no corners or abrupt slope changes), the control points on each side of the joining point must be colinear. This behavior is default in vector editing programs like Adobe Illustrator.

An example in Processing:

// BezierJoins.pde
// Marius Watz - http://workshop.evolutionzone.com

void setup() {
  size(400,400);
}

void draw() {
  background(200);
  noFill();

  float xD=width/2-mouseX;
  float yD=height/2-mouseY;  

  bezier(0,0,
    300,100,
    width/2-xD,height/2-yD,
    width/2,height/2);

  bezier(width/2,height/2,
    width/2+xD,height/2+yD,
    400,300,
    400,400);
}

1 Comment »

No, I’m not talking about Christmas ornaments. I just came up with a quick hack to turn off the window frame from inside Processing. See this thread on the Processing forums to know why this is useful.

When running in the IDE the sketch runs as an applet, so it can’t use the “–present” command line switches available to applications because init() is called instead of main(). My hack simply overrides the default init(), sets the frame to be undecorated and then calls the regular PApplet.init().

Update: Setting the location of the window needs to happen in setup(), not init().

import processing.opengl.*;

void setup(){
  size(400,400,OPENGL);
  background(0);
  frame.setLocation(0,0); // needs to be in setup(), not init()
}

void draw(){
  stroke(255);
  line(0,0,200,200);
}  

public void init() {
  frame.setUndecorated(true); // works.

  // call PApplet.init() to take care of business
  super.init();
}

4 Comments »

This is a simple variant to the built-in saveStrings() method in Processing. It allows you to write strings to a GZIP compressed file instead of a plain text file. Very useful when writing text-based data files that add up to a few megabytes.

Source code - saveStringsGZIP.pde
// saveStringsGZIP.pde
// Marius Watz - http://workshop.evolutionzone.com
//
// Code for saving an array of strings to a GZIP compressed file.
// Based on Processing's built-in saveStrings() method.

// Java classes needed for file I/O.
import java.io.*;
import java.util.zip.GZIPOutputStream;

void setup() {
  String [] s=new String[2000];
  for(int i=0; i< s.length; i++) s[i]=nf(i,50);
  saveStringsGZIP("test.txt",s);
}

// Based on code from processing.core.PApplet, by the Processing team.
public void saveStringsGZIP(String filename, String strings[]) {
  try {
    String location = savePath(filename+".gz");
    GZIPOutputStream fos =
      new GZIPOutputStream(new FileOutputStream(location));
    PrintWriter writer =
      new PrintWriter(new OutputStreamWriter(fos));
    for (int i = 0; i < strings.length; i++)
      if(strings[i]!=null) writer.println(strings[i]);
    writer.flush();
    fos.close();
  } catch (IOException e) {
    e.printStackTrace();
    throw new RuntimeException("saveStringsGZIP() failed: "
      + e.getMessage());
  }
}

No Comments »

Here are some links to code I’ve written or hacks I’ve set up for use with Processing.

No Comments »