MyGroup4t.java

The inductor is supposed to be centered at (0, 0) but it is not.

Command window output:

C:\ece538\circuit3>java MyGroup4t

Inductor bounds

BoundsInLocal: xcen 0.250000 ycen -0.375000 width 6.70833 height 3.45833
BoundsInParent: xcen 0.250000 ycen -0.375000 width 0.670833 height 0.345833

grid group bounds

BoundsInLocal: xcen 0.00000 ycen 0.00000 width 9.01042 height 9.01042
BoundsInParent: xcen 200.000 ycen 200.000 width 865.000 height 865.000

Note that 96 * 9.01042 = 865;


MyGroup4t.java

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.collections.*;
import javafx.geometry.Bounds;
import java.lang.Math;

public class MyGroup4t extends Application {
    public void start(Stage stage) {
        Group grp = new Group();
        Scene scene = new Scene(grp, 400, 400);

	grp.setTranslateX(scene.getWidth()/2);
	grp.setTranslateY(scene.getHeight()/2);
	double res = 96;
	grp.setScaleX(res);
	grp.setScaleY(res);

	//grp.setRotate(10.0);

	drawGrid(grp);


	Capacitor2 c1 = new Capacitor2();
	c1.draw(grp);
	c1.setPosition(0.0,1.0);

	Capacitor2 c2 = new Capacitor2();
	c2.clr = Color.BLUE;
	c2.draw(grp);
	c2.setAngle(30.0);
	c2.setPosition(1.0,0.0);

	drawResistor(grp);

	drawInductor(grp);

	
	// show group structure
	/*
	ObservableList<Node> ol = grp.getChildren();
	for (Node n: ol) System.out.println(n);
	int ng = 5;
	try {
		if (Class.forName("javafx.scene.Group").isInstance(ol.get(ng))) System.out.println("We are a group object");
	}
	catch (Exception e) {
		System.out.println(e);
	}

	Group g = (Group) ol.get(ng);
	ol = g.getChildren();
	System.out.format("\nGroup %d:",ng);
	for (Node n: ol) System.out.println(n);

	*/
	System.out.println("\ngrid group bounds");
	show_bounds(grp);


        stage.setTitle("MyGroup4 Test");
        stage.setScene(scene);
        stage.show();
    }
    public void drawGrid(Group grp) {

	double res = grp.getScaleX();
	ObservableList<Node> ol = grp.getChildren();
	Color c = Color.rgb(220,200,255);

	double u = 4.0;
	Path p1 = new Path();
	ObservableList<PathElement> p = p1.getElements();
	for (int i=-4; i<=4; i++) {
		double v = i;
		p.add(new MoveTo(-u,v));
		p.add(new LineTo(u,v));
		p.add(new MoveTo(v,-u));
		p.add(new LineTo(v,u));
	}
	p1.setStrokeWidth(1.0/res);
	p1.setStroke(c);
	ol.add(p1);

	// locate origin

	Path p2 = new Path();
	p = p2.getElements();	
	u = 0.05;
	p.add(new MoveTo(-u,u));
	p.add(new LineTo(u,-u));
	p.add(new MoveTo(-u,-u));
	p.add(new LineTo(u,u));
	p2.setStrokeWidth(1.0/res);
	ol.add(p2);
   }

   void show_bounds(Group grp) {
	double xcen, ycen;
	Bounds b1 = grp.getBoundsInLocal();
	//System.out.println("BoundsInLocal: " + b1);
	xcen = 0.5*(b1.getMinX()+b1.getMaxX());
	ycen = 0.5*(b1.getMinY()+b1.getMaxY());
	System.out.format("\nBoundsInLocal: xcen %g ycen %g width %g height %g\n",
	xcen, ycen, b1.getWidth(), b1.getHeight());
	Bounds b2 = grp.getBoundsInParent();
	xcen = 0.5*(b2.getMinX()+b2.getMaxX());
	ycen = 0.5*(b2.getMinY()+b2.getMaxY());
	//System.out.println("BoundsInParent: " + b2);
	System.out.format("BoundsInParent: xcen %g ycen %g width %g height %g\n",
	xcen, ycen, b2.getWidth(), b2.getHeight());
   }


   void drawResistor(Group parent)
   {
	double w = 2.5/7.0;
	double y = 1.25;
	Group grp = new Group();
	double resc = 0.1;
	grp.setScaleX(resc);
	grp.setScaleY(resc);

	double res = parent.getScaleX();
	Path mypath = new Path();
	ObservableList<PathElement> p = mypath.getElements();
	p.add(new MoveTo(-2.5,0));
	p.add(new LineTo(-6*w,0));
	p.add(new LineTo(-5*w,-y));
	p.add(new LineTo(-3*w,y));
	p.add(new LineTo(-w,-y));
	p.add(new LineTo(w,y));
	p.add(new LineTo(3*w,-y));
	p.add(new LineTo(5*w,y));
	p.add(new LineTo(6*w,0));
	p.add(new LineTo(2.5,0));

	mypath.setStrokeWidth(20/res);
	mypath.setStroke(Color.GREEN);
 	mypath.setStrokeLineCap(StrokeLineCap.ROUND);

	ObservableList<Node> ol = grp.getChildren();
	ol.add(mypath);

	parent.getChildren().add(grp);
	
	double xpos = -1.0, ypos = 0.0;
	grp.setTranslateX(xpos);
	grp.setTranslateY(-ypos);

   }

   void drawInductor(Group parent)
   {
	double[] xpts = {-2.5,-2.5,-1.0,-1.0,-1.0,-1.5,-1.5};
	double[] ypts = { 0.0,-2.0,-2.0, 0.0, 1.0, 1.0, 0.0};

	Group grp = new Group();
	double resc = 0.1;
	grp.setScaleX(resc);
	grp.setScaleY(resc);


	double res = parent.getScaleX();

	Path mypath = new Path();
	ObservableList<PathElement> p = mypath.getElements();
	p.add(new MoveTo(-2.5,0));
	p.add(new CubicCurveTo(xpts[1],ypts[1],xpts[2],ypts[2],xpts[3],ypts[3]));
	p.add(new CubicCurveTo(xpts[4],ypts[4],xpts[5],ypts[5],xpts[6],ypts[6]));
	
	for (int n=0; n<4; n++) {
		for (int j=0; j<7; j++) xpts[j] += 1.0;
		p.add(new CubicCurveTo(xpts[1],ypts[1],xpts[2],ypts[2],xpts[3],ypts[3]));
		if (n<3) p.add(new CubicCurveTo(xpts[4],ypts[4],xpts[5],ypts[5],xpts[6],ypts[6]));
	}
	
	

	mypath.setStrokeWidth(20/res);
	mypath.setStroke(Color.GREEN);
 	mypath.setStrokeLineCap(StrokeLineCap.ROUND);

	ObservableList<Node> ol = grp.getChildren();
	ol.add(mypath);

	System.out.println("\nInductor bounds");
	show_bounds(grp);


	parent.getChildren().add(grp);

	double xpos = 0.0, ypos = 0.0;
	grp.setTranslateX(xpos);
	grp.setTranslateY(-ypos);

   }
	
}


Maintained by John Loomis, updated Thu Feb 08 15:02:26 2018