TilePanel
The following bit of code was written as (a rather small) part of a school project, intended to visualise electric wave propagation on the surface of a heart. Basically, you got a set of differential equations and then set some initial values to see what happened. We used the code shown below to visualise this.
The methods shown here were part of a class extending JPanel that had some auxillary functions for changing parameters etc. Because we had more pixels than data each value of the dataset was represented as a tile of x by x pixels. I hope the code speaks for itself, it's not much but if you ever need a quick fix to paint fast enough for animation, this could be it.
public void initialise()
{
image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
rgbArray = new int[tileSize*tileSize];
}
protected void paintComponent(Graphics g)
{
if (data == null) return;
image.flush();
g.clearRect(0,0,imageW, imageH);
for (int y=0; y<dataH; y++) {
for (int x=0; x<dataW; x++) {
iX = tileSize * x;
iY = tileSize * y;
color = doubleToColour(data[x][y]);
Arrays.fill(rgbArray, color);
image.setRGB(iX, iY, tileSize, tileSize, rgbArray, 0, tileSize);
}
}
g.drawImage(image, paddingX, paddingY, null);
}
/*
* Passes a data reference to the TilePanel and calculates the new panel
* size
*/
public void setData(double[][] data)
{
this.data = data;
dataW = data.length - 4;
dataH = data[0].length - 4;
imageW = dataW*tileSize;
imageH = dataH*tileSize;
image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
rgbArray = new int[tileSize*tileSize];
setPreferredSize(new Dimension(imageW+2*paddingX, imageH+2*paddingY));
}
private BufferedImage image; // The buffered canvas used to paint on
private int[] rgbArray; // A small array used to paint tiles quickly
private double[][] data; // The data source for the TilePanel
private int tileSize = 5; // The width & height of each tile in pixels
private int paddingX = 0; // Padding can be used to set some
private int paddingY = 0; // whitespace around the visualisationThe key to making fast painting lies mostly in the use of a BufferedImage instead of painting directly to the canvas. Using the image.setRGB() method for arrays instead of individual pixels sped things up a great deal to, although updating the whole image this way was slower than the method shown here. No idea why.
The animation part is quite simple in java: I used a Timer object to refresh the panel every 1/16th of a second and a 2nd timer to update the differential equations, which required a much smaller timestep.
If anyone knows a clean & easy way to make it faster, please, let me know.
Jul 11th, 2008
Comments
No comments yet! Feel free to post some using the form below.
If you wish to add code to your comment you can use code tags, like this: <code class="php">yourCodeHere</code>.
Quite a large number of languages are supported, although I can't guarantee it'll be pretty. Inside the code tags you can use any characters except for the string "</code>".