Keine Box sichtbar

Status
Nicht offen für weitere Antworten.

taejon

Mitglied
Hallo

ich entwickle auf Mac OS Tiger auf Eclipse 3.2. Versuche in einer Application eine Box abzubilden und (später mal) zu transformieren. In dem Programm (siehe unten) sehe ich beim Laufenlassen keine Box. Stattdessen die Fehlermeldung: :(

Exception in thread "main" java.lang.NullPointerException
at efgcalc2.addBox(efgcalc2.java:330)
at efgcalc2.main(efgcalc2.java:354)

Weiss jemand Rat ? Mache ich was grundsätzlich falsch ??? Wie kriege ich die Abb. rechts, dass Sie erscheint ??
Vielen dank im voraus

taejon



Code:
import java.awt.Frame;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.Scrollbar;
import java.awt.TextField;
import java.awt.Window;
import javax.swing.*;
import java.awt.Panel;
import java.awt.GridBagLayout;
import javax.swing.JSlider;
import java.awt.Button;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;  // Muss man komischerweise extra importieren (schwachsinn hoch 3 grrr!!)
// Jetzt die java3D Libs
import javax.media.j3d.*;
import com.sun.j3d.utils.behaviors.vp.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Box;


public class efgcalc2 extends Frame {

	private static final long serialVersionUID = 1L;
	private Label label = null;
	private Label label1 = null;
	private Label label2 = null;
	private Label label3 = null;
	private Label label4 = null;
	private Label label5 = null;
	private Label label6 = null;
	private Label label7 = null;
	private TextField textField = null;
	private TextField textField1 = null;
	private TextField textField2 = null;
	private TextField textField3 = null;
	private TextField textField4 = null;
	private TextField textField5 = null;
	private TextField textField6 = null;
	private TextField textField7 = null;
	private Panel panel = null;
	private JSlider jSlider = null;
	private JSlider jSlider1 = null;
	private TextField textField8 = null;
	private TextField textField9 = null;
	private Button button = null;
	private Button button1 = null;
	
	protected SimpleUniverse myUniverse;
	protected BranchGroup rootBranchGroup;

	
	private TextField getTextField() {
		if (textField == null) {
			textField = new TextField();
			textField.setBounds(new Rectangle(60, 74, 54, 20));
		}
		return textField;
	}
	
	private TextField getTextField1() {
		if (textField1 == null) {
			textField1 = new TextField();
			textField1.setBounds(new Rectangle(59, 100, 55, 21));
		}
		return textField1;
	}
		private TextField getTextField2() {
		if (textField2 == null) {
			textField2 = new TextField();
			textField2.setBounds(new Rectangle(59, 127, 55, 21));
		}
		return textField2;
	}
	
	private TextField getTextField3() {
		if (textField3 == null) {
			textField3 = new TextField();
			textField3.setBounds(new Rectangle(60, 153, 54, 20));
		}
		return textField3;
	}
	
	private TextField getTextField4() {
		if (textField4 == null) {
			textField4 = new TextField();
			textField4.setBounds(new Rectangle(59, 179, 56, 20));
		}
		return textField4;
	}
	
	private TextField getTextField5() {
		if (textField5 == null) {
			textField5 = new TextField();
			textField5.setBounds(new Rectangle(60, 257, 56, 23));
		}
		return textField5;
	}
	
	private TextField getTextField6() {
		if (textField6 == null) {
			textField6 = new TextField();
			textField6.setBounds(new Rectangle(58, 205, 57, 22));
		}
		return textField6;
	}
		private TextField getTextField7() {
		if (textField7 == null) {
			textField7 = new TextField();
			textField7.setBounds(new Rectangle(59, 231, 56, 21));
		}
		return textField7;
	}
		private Panel getPanel() {
		if (panel == null) {
			panel = new Panel();
			panel.setLayout(new GridBagLayout());
			panel.setBounds(new Rectangle(159, 76, 430, 314));
		}
		return panel;
	}
	
	private JSlider getJSlider() {
		if (jSlider == null) {
			jSlider = new JSlider();
			jSlider.setBounds(new Rectangle(9, 300, 72, 13));
		}
		return jSlider;
	}
		private JSlider getJSlider1() {
		if (jSlider1 == null) {
			jSlider1 = new JSlider();
			jSlider1.setBounds(new Rectangle(8, 349, 75, 16));
		}
		return jSlider1;
	}
		private TextField getTextField8() {
		if (textField8 == null) {
			textField8 = new TextField();
			textField8.setBounds(new Rectangle(90, 300, 44, 14));
		}
		return textField8;
	}
	
	private TextField getTextField9() {
		if (textField9 == null) {
			textField9 = new TextField();
			textField9.setBounds(new Rectangle(91, 349, 43, 15));
		}
		return textField9;
	}
		private Button getButton() {
		if (button == null) {
			button = new Button();
			button.setBounds(new Rectangle(13, 318, 43, 19));
			button.setLabel("repaint");
		}
		return button;
	}
		private Button getButton1() {
		if (button1 == null) {
			button1 = new Button();
			button1.setBounds(new Rectangle(12, 370, 47, 19));
			button1.setLabel("repaint");
		}
		return button1;
	}
	
		public efgcalc2()    // Standardkonstruktor 
	{
		super(" Berechnung des EFG");
		initialize();
		setSize(800, 700);
		// Alles fuer die 3D-Konfiguration
		
		setVisible(true);
	}
		private void initialize() {
		label7 = new Label();
		label7.setBounds(new Rectangle(7, 256, 45, 23));
		label7.setText("gamma");
		label6 = new Label();
		label6.setBounds(new Rectangle(7, 230, 45, 23));
		label6.setText("beta");
		label5 = new Label();
		label5.setBounds(new Rectangle(7, 204, 45, 23));
		label5.setText("alpha");
		label4 = new Label();
		label4.setBounds(new Rectangle(6, 178, 46, 23));
		label4.setText("c0");
		label3 = new Label();
		label3.setBounds(new Rectangle(7, 152, 45, 23));
		label3.setText("b0");
		label2 = new Label();
		label2.setBounds(new Rectangle(7, 126, 45, 23));
		label2.setText("a0");
		label1 = new Label();
		label1.setBounds(new Rectangle(7, 99, 45, 23));
		label1.setText("Sclice");
		label = new Label();
		label.setBounds(new Rectangle(7, 72, 45, 23));
		label.setText("Gridsize");
		this.setLayout(null);
		this.setSize(604, 402);
		this.setTitle("Frame");
		this.add(label, null);
		this.add(label1, null);
		this.add(label2, null);
		this.add(label3, null);
		this.add(label4, null);
		this.add(label5, null);
		this.add(label6, null);
		this.add(label7, null);
		this.add(getTextField(), null);
		this.add(getTextField1(), null);
		this.add(getTextField2(), null);
		this.add(getTextField3(), null);
		this.add(getTextField4(), null);
		this.add(getTextField5(), null);
		this.add(getTextField6(), null);
		this.add(getTextField7(), null);
		this.add(getPanel(), null);
		this.add(getJSlider(), null);
		this.add(getJSlider1(), null);
		this.add(getTextField8(), null);
		this.add(getTextField9(), null);
		this.add(getButton(), null);
		this.add(getButton1(), null);
		// Here the basic configuration for Java3D
		
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		Canvas3D myCanvas3D = new Canvas3D(config);
		add (panel, myCanvas3D);
		SimpleUniverse myUniverse = new SimpleUniverse(myCanvas3D);
		myUniverse.getViewingPlatform().setNominalViewingTransform();
	    BranchGroup rootBranchGroup = new BranchGroup();
	    //Colorcube colorcube = new Colorcube();    
	}
	// Additional method for setting a Box
	public void addBox(float x, float y, float z, Color3f diffuse, Color3f spec)
	{
		Appearance app = new Appearance();
		Material   mat = new Material();
		mat.setDiffuseColor(diffuse);
		mat.setSpecularColor(spec);
		mat.setShininess(5.0f);
		app.setMaterial(mat);
		Box box = new Box(x, y, z, app);
		// Then add it to the rootBranchGroup
		rootBranchGroup.addChild(box);
	}
	
	// Method for additional light
	public void addDirectionalLight(Vector3f direction, Color3f color)
	{
		BoundingSphere bounds = new BoundingSphere();
		bounds.setRadius(1000d);
		DirectionalLight lightD = new DirectionalLight(color, direction);
		lightD.setInfluencingBounds(bounds);
	    rootBranchGroup.addChild(lightD);
	}
	public void finalise()
	{
		myUniverse.addBranchGraph(rootBranchGroup);
		myUniverse.getViewingPlatform().setNominalViewingTransform();		
	}
		public static void main(String[] args) 
	{
		efgcalc2 efgwin = new efgcalc2();
	//	efgwin.setSize(400, 400);
		efgwin.addBox(0.4f, 0.5f, 0.6f, new Color3f(1, 0, 0), new Color3f(1, 0, 0));
		efgwin.addDirectionalLight(new Vector3f(0f, 0f, -1), new Color3f(1f, 1f, 0f));
		efgwin.finalise();
		efgwin.show();
		
	}
}
 

madboy

Top Contributor
ersetze Zeile 239
Code:
BranchGroup rootBranchGroup = new BranchGroup();
durch
Code:
rootBranchGroup = new BranchGroup();
Ach ja: nächstes mal bitte den Code mit korrekten Zeilennummern posten (Zeilen 330 und254 sind nicht im Listing) :wink:
 

taejon

Mitglied
Hallo

erst mal vielen Dank, hmm die Exceptions werden nicht mehr geworfen, aber die Box sehe ich immer noch nicht.
Muss ich da was grundsätzliches ändern ? :(
Ich meine muss das Panel anders belegt werden ? Ich komme da nicht recht weiter...

Ach ja die Zeilennummern hatten sich verändert, weil ich Kommentare gelöscht hatte (sorry)
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben