Rails Against The Machine

Just a mind dump. Why are you even reading this?

Thursday, 19 June 2008

 

Cleaner pixel level drawing with java

Need to change alpha transparency independently of colour so changing from index color to direct.
More bitmask fun

package services;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.DirectColorModel;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;

public class ImageFactory
{

private static int width = 255;
private static int height = 255;

public static BufferedImage Image()
{
// This creates an RGBA colormodel... I think.
DirectColorModel colormodel = new DirectColorModel(32, 0xff000000, 0xff0000, 0xff00, 0xff);
int[] pixels = generatePixels(width, height);
SampleModel sample = colormodel.createCompatibleSampleModel(width, height);
DataBufferInt data = new DataBufferInt(pixels, width * height);
WritableRaster raster = WritableRaster.createWritableRaster(sample, data, new Point(0, 0));
return new BufferedImage(colormodel, raster, false, null);
}

private static int[] generatePixels(int w, int h)
{
int[] pixData = new int[width * height];
int i = 0;
for (int y = 0; y < height; y++) {
int red = (y * 255) / (height - 1);
for (int x = 0; x < width; x++) {
int green = (x * 255) / (width - 1);
int blue = 128;
int alpha = Math.max(0,x-y);
pixData[i++] = (alpha << 32) | (red << 24) |
(green << 16) | (blue << 8);
}
}
return pixData;
}
}

Thursday, 5 June 2008

 

Cascading drop downs

Java script code using prototype

window.onload = init;

function init(){
//
if (field = $('parent_dropdown_id')) {
field.observe('change', function(e) {
//make a request and get the json output
new Ajax.Request('url_to_get_data_from', {
method:'get',
parameters: field.serialize(true),
onSuccess: function(transport){
var json = transport.responseText.evalJSON();
some_namespace.display(json);
}
});
});
}
}

var some_namespace= {
display: function (data){
var field = $('child_dropdown_id');
opt= field.options;
opt.length=0; //You delete by resizing the array
for(var i=0;i<data.length;i++){
opt[opt.length] = new Option(data[i][0],data[i][1]);
}
}
}



On the server you just return a json opject to populate the drop down

 

Stable back url

Back buttons based on request.env["HTTP_REFERER"] tend to get broken by renders and redirects
Set it in the session with.
  def set_backurl 
referer = request.env["HTTP_REFERER"]
return false if !referer
getIt = request.env["REQUEST_URI"].split("?")[1]
if getIt.nil?
getIt = ""
else
getIt = "?" + getIt if !getIt.match(/\?/)
end
session[:backurl]=referer + getIt
end


get it with
    @backurl =session[:backurl]

Archives

July 2007   August 2007   September 2007   December 2007   January 2008   February 2008   March 2008   April 2008   June 2008   July 2008   August 2008   October 2008   November 2008   January 2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]