Java Swingテクノロジを使用して、NetBeansプラットフォームでデスクトップアプリを作成しました。そのアプリでは、PointArray[]オブジェクトを使用して3D画像の画像処理を行います。
Windows 7、グラフィックカード、および十分なRAM容量(4GB RAM)を備えたPCでアプリを実行すると、3DImageボタンをクリックしてから5〜6秒以内に3D画像が作成および表示されます。しかし、私のアプリをWindows XPの低グラフィック構成PCで実行すると、3D画像のレンダリングに最大3分かかるか、画像が表示されない場合があります。では、どうすればその問題を解決できますか?
私のコードは以下の通りです。
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.PointArray;
import javax.media.j3d.PointAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
public final class FinalDImage extends JPanel {
private static int s = 0, count = 0, r = 0, g = 0, b = 0, medHeight = 0, medWidth = 0;
private static float divisior1 = 300000.0f;
private static Shape3D plShape;
private static TransformGroup objRotate;
private static BranchGroup scene;
private static JButton btn;
private static Canvas3D canvas3D;
private static SimpleUniverse simpleU;
private static GraphicsConfiguration gc;
private static BufferedImage histImage;
private static JPanel jPanel1 = new JPanel();
public FinalDImage(BufferedImage histImage) {
FinalDImage.histImage = histImage;
medWidth = (int) (histImage.getWidth() / 2.0f);
medHeight = (int) (histImage.getHeight() / 2.0f);
this.add(jPanel1);
initComponents();
}
public void initComponents() {
setLayout(new BorderLayout());
btn = new JButton("Intensity");
gc = SimpleUniverse.getPreferredConfiguration();
canvas3D = new Canvas3D(gc);//See the added gc? this is a preferred config
add("Center", canvas3D);
add(btn, BorderLayout.SOUTH);
scene = createSceneGraph(FinalDImage.histImage);
scene.setCapability(BranchGroup.ALLOW_DETACH);
scene.compile();
simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph(BufferedImage histImage) {
count = 0;
BranchGroup lineGroup = new BranchGroup();
Appearance app = new Appearance();
ColoringAttributes ca = new ColoringAttributes(new Color3f(204.0f, 204.0f, 204.0f), ColoringAttributes.SHADE_FLAT);
app.setColoringAttributes(ca);
Point3f plaPts;
Color3f color;
PointArray pla = new PointArray(histImage.getWidth() * histImage.getHeight(), GeometryArray.COLOR_3 | GeometryArray.COORDINATES);
if (histImage.getType() == 11) {
for (int i = histImage.getWidth() - 3; i >= 3; i--) {
for (int j = histImage.getHeight() - 3; j >= 3; j--) {
s = histImage.getRaster().getSample(i, j, 0);
plaPts = new Point3f((medWidth - i) / 1500.0f,
(medHeight - j) / 1500.0f,
s / divisior1);
color = new Color3f(s / 60000.0f, s / 60000.0f, s / 60000.0f);
pla.setCoordinate(count, plaPts);
pla.setColor(count, color);
count++;
}
}
} else {
for (int i = 2; i < histImage.getWidth() - 2; i++) {
for (int j = 2; j < histImage.getHeight() - 2; j++) {
r = histImage.getRaster().getSample(i, j, 0);
g = histImage.getRaster().getSample(i, j, 1);
b = histImage.getRaster().getSample(i, j, 2);
s = (r + g + b) / 3;
plaPts = new Point3f((medWidth - i) / 1200.0f,
(medHeight - j) / 1200.0f,
s / (divisior1/600.0f));
color = new Color3f(r / 300.0f, g / 300.0f, b / 300.0f);
pla.setCoordinate(count, plaPts);
pla.setColor(count, color);
count++;
}
}
}
PointAttributes a_point_just_bigger = new PointAttributes();
a_point_just_bigger.setPointSize(4.0f);//10 pixel-wide point
a_point_just_bigger.setPointAntialiasingEnable(true); //now points are sphere-like (not a cube)
app.setPointAttributes(a_point_just_bigger);
plShape = new Shape3D(pla,app);
objRotate = new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(plShape);
lineGroup.addChild(objRotate);
MouseRotate myMouseRotate = new MouseRotate();
myMouseRotate.setFactor(0.015, 0.015);
myMouseRotate.setTransformGroup(objRotate);
myMouseRotate.setSchedulingBounds(new BoundingSphere());
myMouseRotate.setSchedulingBounds(new BoundingSphere(
new Point3d(0.0, 0.0, 0.0), 100));
lineGroup.addChild(myMouseRotate);
MouseWheelZoom mz = new MouseWheelZoom();
mz.setFactor(0.01);
mz.setTransformGroup(objRotate);
mz.setSchedulingBounds(new BoundingSphere());
lineGroup.addChild(mz);
return lineGroup;
}
public static void main(String[] args) {
PlanarImage plImg3 = JAI.create("fileload", "E:\\Data\\office\\teeth3.tiff");
BufferedImage histImage = plImg3.getAsBufferedImage();
JFrame frame = new JFrame();
frame.add(new JScrollPane(new FinalDImage(histImage)));
frame.setSize(800, 700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}