JHeightMap - module for visualization of height maps in Java |
|
Authors: |
Bc. Martin Kinkor, Ing. Jana Hájková, Ph.D., Department of Computer Science and Engineering, University of West Bohemina in Pilsen, Czech Republic Created as the Bachealor Thesis (M. Kinkor: Module for Height Map Visualisation in Java, 2011), and further adjusted |
Module description: |
JHeightMap module should be used for 3D visualization of height maps represented in different formats and forms (CSV, image, array, ...).
It can be integrated into any java programme as a library. It enables to read height map description and draw it
into the interactive panel, which can be embeded into users GUI.
|
System requirements: |
Java 1.6 (or higher) Java3D instalation |
Downloads: |
JHeightMap 1.0
|
Documentation: |
Javadoc
|
First program (data matrix visualization):
The first example shows very simple way how any height map represented as the number matrix can be visualized. For this case, the matrix is generated randomly. In a practically use, matrix should be gained as the result on any computation. Usage of the module is simple, it is just important to link the library (.JAR file) to the project and to use several classes:
|
|
import java.awt.Dimension; import java.util.Random; import javax.swing.JFrame; import JHeightMap.JDefaultDataMap; import JHeightMap.JDefaultHeightMap; import JHeightMap.JPanelMap; public class FirstHeightMap extends JFrame { private final int WINDOW_WIDTH = 600; private final int WINDOW_HEIGHT = 400; private final int DATA_WIDTH = 300; private final int DATA_HEIGHT = 200; public FirstHeightMap() { // generates random values into the matrix float[][] myData = new float[DATA_WIDTH][DATA_HEIGHT]; Random r = new Random(); for (int i = 0; i < myData.length; i++) { for (int j = 0; j < myData[0].length; j++) { myData[i][j] = r.nextFloat(); } } float[] dataVector = JDefaultDataMap.matrix2vector(myData); // converts data matrix to vector JDefaultDataMap data=new JDefaultDataMap(dataVector, DATA_WIDTH, DATA_HEIGHT); JDefaultHeightMap map=new JDefaultHeightMap(data); JPanelMap panel=new JPanelMap(map); panel.compileScene(); add(panel); this.setTitle("My First JHeightMap Usage"); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); this.pack(); this.setVisible(true); } public static void main(String[] args) { new FirstHeightMap(); } } |
|
Image file loading (visualization of image) and key control activation:
This example shows several two more functions which can be used. Differences from the first example are highlighted in the source code. Both features can be used together or independently:
|
|
import java.awt.Dimension; import javax.swing.JFrame; import JHeightMap.JDefaultDataMap; import JHeightMap.JDefaultHeightMap; import JHeightMap.JGraphicLoadMap; import JHeightMap.JPanelMap; public class FileLoad extends JFrame { private final int WINDOW_WIDTH = 600; private final int WINDOW_HEIGHT = 400; private final double ZOOM_INC = 0.5; private final int ANGLE_INC = 10; public FileLoad() { String fileName="data\\image.png"; JGraphicLoadMap loader=new JGraphicLoadMap(fileName); JDefaultDataMap data=new JDefaultDataMap(loader); JDefaultHeightMap map=new JDefaultHeightMap(data); JPanelMap panel=new JPanelMap(map); panel.compileScene(); add(panel); JControls controls=new JControls(panel, ZOOM_INC, ANGLE_INC); panel.setBehavior(controls); this.setTitle("My First JHeightMap Usage"); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); this.pack(); this.setVisible(true); } public static void main(String[] args) { new FileLoad(); } } |
|
Loading CSV file, adding text into help, showing wiremodel and background color change:
Other advanced features are shown in the thirt example, all of them can be again used separatelly or in any combination even with the previous ones.
|
|
CSV download | import java.awt.Color; import java.awt.Dimension; import javax.swing.JFrame; import JHeightMap.JCSVLoadMap; import JHeightMap.JControls; import JHeightMap.JDefaultDataMap; import JHeightMap.JDefaultHeightMap; import JHeightMap.JPanelMap; public class FileLoadCSV extends JFrame { private final int WINDOW_WIDTH = 600; private final int WINDOW_HEIGHT = 400; public FileLoadCSV() { String fileName="data\\data.csv"; JCSVLoadMap loader=new JCSVLoadMap(fileName); JDefaultDataMap data=new JDefaultDataMap(loader); JDefaultHeightMap map=new JDefaultHeightMap(data); map.setWireModelMap(true); JPanelMap panel=new JPanelMap(map, Color.ORANGE); panel.compileScene(); add(panel); JControls controls=new JControls(panel); panel.setBehavior(controls); controls.addText("any text to be added"); this.setTitle("My First JHeightMap Usage"); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setPreferredSize(new Dimension(this.WINDOW_WIDTH, this.WINDOW_HEIGHT)); this.pack(); this.setVisible(true); } public static void main(String[] args) { new FileLoadCSV(); } } |
Lights - adding, removing, ...:
The last example describes the basic work with lights. Four different types of light can be used - ambient, directional, spot and point. This example works with the directional one. Into the sceene several lights can be added. Each of them has to have its direction and color, method addChilder() puts the created light into the scene and finally, it has to be added into the panel. |
|
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import JHeightMap.JCSVLoadMap;
import JHeightMap.JDefaultDataMap;
import JHeightMap.JDefaultHeightMap;
import JHeightMap.JLight;
import JHeightMap.JPanelMap;
public class LightsHeightMap extends JFrame {
private final int WINDOW_WIDTH = 600;
private final int WINDOW_HEIGHT = 400;
public LightsHeightMap() {
String fileName="data\\data.csv";
JCSVLoadMap loader=new JCSVLoadMap(fileName);
JDefaultDataMap data=new JDefaultDataMap(loader);
JDefaultHeightMap map=new JDefaultHeightMap(data);
JPanelMap panel=new JPanelMap(map);
panel.compileScene();
add(panel);
JLight light=new JLight(JLight.DIRECTIONAL_LIGHT);
light.setDirect(new Vector3f(1.0f, 0.0f, 1.0f));
light.setColor(new Color3f(1.0f, 0.0f, 0.0f));
light.addChilder();
panel.addLight(light);
this.setTitle("My First JHeightMap Usage");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setPreferredSize(new Dimension(this.WINDOW_WIDTH, this.WINDOW_HEIGHT));
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
new LightsHeightMap();
}
}
|