简体   繁体   中英

Mapping different colors to points on a plane in Java3D

I am trying to write a Java3D application which emulates the what you would see on a spectrogram such as this: http://en.wikipedia.org/wiki/File:Spectrogram-19thC.png . My main difficulty right now is figuring out how to actually display amplitude values on the plane in the same way as the spectrogram does: using different colors to designate different intensities for each time-frequency coordinate ((x, y) point) on the spectrogram.

Here is the current version of my code. My constructor takes as an argument a 2D array containing time-frequency coordinates. I create a plane representing the spectrogram and set up some for loops to use when actually displaying the amplitude values. I'm aware of how to calculate the amplitude values for each point on the 2D plane and have a idea of how to assign ranges of amplitude values to colors (although I haven't yet coded these).

My main problem is with displaying these colors on the plane itself. While Java3D allows users to specify colors for an entire object (using the Appearance, ColoringAttributes and Color3f classes), I haven't seen any examples of mapping different colors to different points on an object, which is what I want to do. I want to be able to control the color of each pixel that makes up the plane, so that instead of making it completely blue, red, etc, I can color each pixel differently depending on the amplitude value I calculate for that point.

Does anyone know if this is possible in Java3D? If so, suggestions of how it can be done or links to resources that deal with it would be greatly appreciated. I am fairly new to Java3D, so any advice would be great.

I should add that my motivation for doing this in Java3D is to ultimately generate a spatiotemporal time-frequency graph using 2D planes (ie, stacking up the 2D spectrograms to create a 3D image).

import java.util.*;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;

import javax.vecmath.*;
import javax.media.j3d.*;

import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.applet.*;
import com.sun.j3d.utils.behaviors.vp.*;

public class DrawTimeFrequencyGraph extends Applet {

public DrawTimeFrequencyGraph (float [][] timeFrequencyArray) {

/* Create the Canvas3D and BranchGroup objects needed for the scene graph */

setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();

Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);

BranchGroup scene = new BranchGroup();

/* Create a 2D plane to represent the graph
* Note: The (x, y, z) coordinates are currently hardcoded, but should be set up
* to reflect the min and max frequency and time values in the 2D array */

QuadArray timeFrequencyGraph = new QuadArray(4, GeometryArray.COORDINATES);
timeFrequencyGraph.setCoordinate(0, new Point3f(-10f, 0f, 0f));
timeFrequencyGraph.setCoordinate(1, new Point3f(0f, 0f, 0f));
timeFrequencyGraph.setCoordinate(2, new Point3f(0f, 3f, 0f));
timeFrequencyGraph.setCoordinate(3, new Point3f(-10f, 3f, 0f));

/* Set up the appearance of the plane i.e. graph values on it using various colors */

for(int i = 0; i <  timeFrequencyArray.length; i++){
  for(int j = 0; j < timeFrequencyArray[i].length; j++){


   /* TO DO: Calculate amplitude values, map them to colors and figure out how to
    * map the colors to points on the plane that has been created.

   }
  }


 }
}

You might also look at JFreeChart , which includes an XYBlockRenderer . Arbitrary color palettes may be constructed using a suitable PaintScale , referenced here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM