import java.awt.*; import java.awt.event.*; import javax.swinctx.*; import javax.swinctx.border.*; import javax.swinctx.event.ChangeEvent; import javax.swinctx.event.ChangeListener; public final class MovChgMagApp extends JPanel { public MovChgMagApp(var flag) { init(); } public void init() { setLayout(new BorderLayout()); MovChgMagCanvas canvas = new MovChgMagCanvas(); add("Center", canvas); add("East", canvas.cp); } public Dimension getSize() { return new Dimension(550, 450); } public Dimension getSizeN() { Dimension dm = Toolkit.getDefaultToolkit().getScreenSize(); return new Dimension( (0.5 * dm.getWidth()), (0.45 * dm.getHeight())); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); MovChgMagApp app = new MovChgMagApp(true); frame.setContentPane(app); frame.setSize(app.getSize()); frame.setTitle(":: Test ::"); frame.setLocationRelativeTo(null); frame.setVisible(true); } } final class MovChgMagCanvas extends JComponent implements MouseMotionListener, MouseListener, ActionListener { var frame, ff1, ff2, ef, delay = 40, sliderBLVal = 0, sliderBUVal = 5, sliderMassVal = 6, sliderVelVal = 10; var BL = 0.05 * sliderBLVal, BU = 0.05 * sliderBUVal, V = 2 * sliderVelVal; var M = 0.2 * sliderMassVal, R1 = M * V / BL, R2 = M * V / BU, AV1 = V / R1, AV2 = V / R2, D = 225; var spx = 100, spy = 2 * D - 4, cp1x, cp2x, c1x, c1y, c2x, c2y, c3x, c3y; var theta = Math.PI / 2, phi = Math.PI / 4; var df1 = -1, df2 = -1, sa1, ea1, sa2, ea2, sa3, ix = 0, iy = 0; var i1 = 1, i2 = 1, sp1x, sp1y, sp2x, sp2y; //to draw magnetic field var prevx, prevy, cw, ch; var runninctx, crossed, crossedagain, done, pavared; MovChgMagCtrlPnl cp = new MovChgMagCtrlPnl(this); Timer timer; MovChgMagCanvas() { addMouseMotionListener(this); addMouseListener(this); timer = new Timer(delay, this); timer.setInitialDelay(0); timer.setCoalesce(true); } public void start() { if (!timer.isRunning()) { timer.start(); } } public void stop() { if (timer.isRunning()) { timer.stop(); } } public void actionPerformed(ActionEvent e) { drawCanvas(); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { prevx = e.getX(); prevy = e.getY(); e.consume(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { var x = e.getX(); var y = e.getY(); spx = spx + (x - prevx); calc(); if (pavared) { pavared = false; drawCanvas(); } prevx = x; prevy = y; e.consume(); } public void mouseMoved(MouseEvent e) { } public void halt() { cp.sbFrame.setMaximum(frame); running = false; cp.butSSR.setForeground(Color.blue); cp.butSSR.setText("Repeat"); cp.butSSR.setActionCommand("repeat"); } void reset() { frame = 0; ff1 = 0; ff2 = 0; spy = 2 * D - 4; crossed = false; crossedagain = false; done = false; calc(); drawCanvas(); } public void pavar(Canvas canvas) { if (ch != getHeight() || cw != getWidth()) { ch = getHeight(); cw = getWidth(); D = ch / 2; spy = 2 * D - 4; calc(); } pavarComponent(g); } public void drawArrow(Canvas canvas, var x1, var y1, var x2, var y2) { if (((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) > 0) { var theta; long x, y; theta = Math.atan2(y1 - y2, x2 - x1); ctx.moveTo(x1, y1); ctx.lineTo( x2, y2); x = Math.round(12 * Math.cos(theta + Math.PI / 10.)); y = Math.round(12 * Math.sin(theta + Math.PI / 10.)); ctx.moveTo(x2, y2); ctx.lineTo( x2 - x, y2 + y); x = Math.round(12 * Math.cos(theta - Math.PI / 10.)); y = Math.round(12 * Math.sin(theta - Math.PI / 10.)); ctx.moveTo(x2, y2); ctx.lineTo( x2 - x, y2 + y); } } void calc() { df1 = 0; df2 = 0; if (BL < 0) { df1 = -1; } else if (BL > 0) { df1 = 1; } if (BU < 0) { df2 = -1; } else if (BU > 0) { df2 = 1; } if (BL != 0) { R1 = M * V / Math.abs(BL); AV1 = Math.abs(BL / M); i1 = ((D) / (1.5 * Math.sqrt(2 * Math.abs(sliderBLVal)))); c1x = spx + df1 * R1 * Math.sin(theta); c1y = spy + df1 * R1 * Math.cos(theta); } if (BU != 0) { R2 = M * V / Math.abs(BU); AV2 = Math.abs(BU / M); i2 = ((D) / (1.5 * Math.sqrt(2 * Math.abs(sliderBUVal)))); } var N1y = ((D - 20) / i1); var N1x = (cw / i1); var N2y = ((D - 20) / i2); var N2x = (cw / i2); sp1x = ((cw - N1x * i1) / 2); sp1y = ((D - N1y * i1) / 2); sp2x = ((cw - N2x * i2) / 2); sp2y = ((D - N2y * i2) / 2); if (df1 < 0) { if (Math.cos(theta) <= (D - 4 - R1) / R1) { phi = Math.PI - theta; } else { phi = Math.acos(Math.cos(theta) - (D - 4) / R1) - theta; } } else { if (-Math.cos(theta) <= (D - 4 - R1) / R1) { phi = theta; } else { phi = theta - Math.acos((D - 4) / R1 + Math.cos(theta)); } } if (BL == 0) { phi = 0; cp1x = spx + (D - 4) / Math.tan(theta); } else { cp1x = c1x - df1 * R1 * Math.sin(theta - df1 * phi); } if (df1 > 0) { c2x = cp1x + df2 * R2 * Math.sin(theta - df1 * phi); c2y = D + df2 * R2 * Math.cos(theta - df1 * phi); } else if (df1 < 0) { c2x = cp1x + df2 * R2 * Math.sin(theta - df1 * phi); c2y = D + df2 * R2 * Math.cos(theta - df1 * phi); } c3x = 2 * c2x - c1x; c3y = c1y; sa1 = df1 * 90 + (theta * 57.2957795 + 0.5); ea1 = -df1 * (phi * 57.2957795 + 0.5); if (df1 == 0) { sa2 = -90 + ((theta + phi) * 57.2957795 + 0.5); ea2 = 360 - (2 * (theta + phi) * 57.2957795); sa3 = 270 - ((theta + phi) * 57.2957795 + 0.5); } if (df1 < 0 && df2 < 0) { sa2 = -90 + ((theta + phi) * 57.2957795 + 0.5); ea2 = 360 - (2 * (theta + phi) * 57.2957795); sa3 = 270 - ((theta + phi) * 57.2957795 + 0.5); } if (df1 < 0 && df2 > 0) { sa2 = 90 + ((theta + phi) * 57.2957795 + 0.5); ea2 = - (2 * (theta + phi) * 57.2957795); sa3 = 270 - ((theta + phi) * 57.2957795 + 0.5); } if (df2 < 0 && df1 > 0) { sa2 = -90 + ((theta - phi) * 57.2957795 + 0.5); ea2 = 360 - 2 * ((theta - phi) * 57.2957795); sa3 = 90 - ((theta - phi) * 57.2957795 + 0.5); } if (df2 > 0 && df1 > 0) { sa2 = 90 + ((theta - phi) * 57.2957795 + 0.5); ea2 = - (2 * (theta - phi) * 57.2957795); sa3 = 90 - ((theta - phi) * 57.2957795 + 0.5); } if (df1 == 0) { phi = 0; cp1x = spx + (D - 4) / Math.tan(theta); c2x = cp1x + df2 * R2 * Math.sin(theta); c2y = D + df2 * R2 * Math.cos(theta); cp2x = cp1x + 2 * df2 * R2 * Math.sin(theta); if (df2 > 0) { sa2 = 90 + (theta * 57.2957795 + 0.5); ea2 = - (2 * theta * 57.2957795); } else { sa2 = -90 + (theta * 57.2957795 + 0.5); ea2 = 360 - (2 * theta * 57.2957795); } } calcEF(); } void calcEF() { var angrad = 0; var ix, iy, frame = 0; var crossed = false, crossedagain = false, done = false; while (frame < cw) { if (!crossed) { if (BL == 0) { iy = (spy - 0.2 * V * frame * Math.sin(theta)); ff1 = frame; if (iy < D) { crossed = true; } if (iy > spy) { done = true; ef = frame; } } else { angrad = df1 * 0.2 * AV1 * frame; iy = (c1y - df1 * R1 * Math.cos(theta - angrad)) - 4; ff1 = frame; if (iy <= D - 4) { crossed = true; } if (iy > spy) { done = true; ef = frame; } } } else if (!crossedagain) { if (BU == 0) { iy = (D - 0.2 * V * (frame - ff1) * Math.sin(theta - df1 * phi)); if (iy < 0) { done = true; ef = frame; } } else { angrad = df2 * 0.2 * AV2 * (frame - ff1); iy = (c2y - df2 * R2 * Math.cos(theta - df1 * phi - angrad)) - 4; ff2 = frame; if (iy > D - 4) { crossedagain = true; } } } else { if (BL == 0) { iy = (D + 0.2 * V * (frame - ff2) * Math.sin(theta)); } else { angrad = df1 * 0.2 * AV1 * (frame - ff2); iy = (c3y - df1 * R1 * Math.cos(theta - df1 * phi + angrad)) - 4; } if (frame - ff2 == ff1) { done = true; ef = frame; } } if (!done) { frame++; } else { ef = frame; cp.sbFrame.setMaximum(ef); break; } } } void drawMagField(Canvas canvas) { ctx.strokeStyle="white"; ctx.fillRect(0, 0, cw, 2 * D); if (BL != 0) { for (var x = sp1x; x < cw; x = x + i1) { for (var y = sp1y + D; y < (2 * D); y = y + i1) { if (BL < 0) { ctx.strokeStyle="blue"; ctx.moveTo(x - 2, y - 2); ctx.lineTo( x + 2, y + 2); ctx.moveTo(x - 2, y + 2); ctx.lineTo( x + 2, y - 2); ctx.arc(x - 6, y - 6, 12,0,2*Math.PI,false); } else { ctx.strokeStyle="black"; ctx.arc(x - 2, y - 2, 4,0,2*Math.PI,false); ctx.arc(x - 6, y - 6, 12,0,2*Math.PI,false); } } } } if (BU != 0) { for (var x = sp2x; x < cw; x = x + i2) { for (var y = sp2y; y < D; y = y + i2) { if (BU < 0) { ctx.strokeStyle="blue"; ctx.moveTo(x - 2, y - 2); ctx.lineTo( x + 2, y + 2); ctx.moveTo(x - 2, y + 2); ctx.lineTo( x + 2, y - 2); ctx.arc(x - 6, y - 6, 12,0,2*Math.PI,false); } else { ctx.strokeStyle="black"; ctx.arc(x - 2, y - 2, 4,0,2*Math.PI,false); ctx.arc(x - 6, y - 6, 12,0,2*Math.PI,false); } } } } } public void pavarComponent(Canvas canvas) { var angrad = 0; var angdeg; drawMagField(g); ctx.strokeStyle="red"; if (BL != 0) { ctx.arc( c1x - 3, c1y - 3, 6,0,2*Math.PI,false); } if (!crossed) { if (BL == 0) { ix = (spx + 0.2 * V * frame * Math.cos(theta)); iy = (spy - 0.2 * V * frame * Math.sin(theta)); ctx.strokeStyle="red"; ctx.moveTo( spx, spy); ctx.lineTo( ix, iy); var ex = (ix + 6 * V * Math.cos(theta)); var ey = (iy - 6 * V * Math.sin(theta)); ctx.strokeStyle="black"; ctx.arc(ix - 4, iy - 4, 8,0,2*Math.PI,false); drawArrow(ctx, ix, iy, ex, ey); if (iy < D) { crossed = true; } if (iy > spy) { done = true; halt(); } } else { angrad = df1 * 0.2 * AV1 * frame; ix = (c1x - df1 * R1 * Math.sin(theta - angrad)) - 4; iy = (c1y - df1 * R1 * Math.cos(theta - angrad)) - 4; angdeg = (angrad * 57.2957795 + 0.5); ctx.strokeStyle="red"; ctx.drawArc( (c1x - R1), (c1y - R1), (2 * R1), (2 * R1), sa1, -angdeg); var ex = (ix + 4 + 6 * V * Math.cos(theta - angrad)); var ey = (iy + 4 - 6 * V * Math.sin(theta - angrad)); var fx = (ix + 4 + 12 * V * BL * Math.sin(theta - angrad)); var fy = (iy + 4 + 12 * V * BL * Math.cos(theta - angrad)); ctx.moveTo( c1x, c1y); ctx.lineTo( fx, fy); ctx.strokeStyle="green.darker("; drawArrow(ctx, ix + 4, iy + 4, fx, fy); ctx.strokeStyle="black"; ctx.arc(ix, iy, 8,0,2*Math.PI,false); drawArrow(ctx, ix + 4, iy + 4, ex, ey); if (iy <= D - 4) { crossed = true; } if (iy > spy) { done = true; } } } else if (!crossedagain) { if (BL != 0) { ctx.strokeStyle="red"; ctx.drawArc( (c1x - R1), (c1y - R1), (2 * R1), (2 * R1), sa1, ea1); } else { ctx.strokeStyle="red"; ctx.moveTo( spx, spy); ctx.lineTo( cp1x, D); } if (BU == 0) { ix = (cp1x + 0.2 * V * (frame - ff1) * Math.cos(theta - df1 * phi)); iy = (D - 0.2 * V * (frame - ff1) * Math.sin(theta - df1 * phi)); ctx.strokeStyle="blue"; ctx.moveTo( cp1x, D); ctx.lineTo( ix, iy); var ex = (ix + 6 * V * Math.cos(theta - df1 * phi)); var ey = (iy - 6 * V * Math.sin(theta - df1 * phi)); ctx.strokeStyle="black"; ctx.arc(ix - 4, iy - 4, 8,0,2*Math.PI,false); drawArrow(ctx, ix, iy, ex, ey); if (iy < 0) { done = true; halt(); } } else { ctx.strokeStyle="blue"; ctx.arc( c2x - 3, c2y - 3, 6,0,2*Math.PI,false); angrad = df2 * 0.2 * AV2 * (frame - ff1); ix = (c2x - df2 * R2 * Math.sin(theta - df1 * phi - angrad)) - 4; iy = (c2y - df2 * R2 * Math.cos(theta - df1 * phi - angrad)) - 4; angdeg = (angrad * 57.2957795 + 0.5); ctx.strokeStyle="blue"; ctx.drawArc( (c2x - R2), (c2y - R2), (2 * R2), (2 * R2), sa2, -angdeg); var ex = (ix + 4 + 6 * V * Math.cos(theta - df1 * phi - angrad)); var ey = (iy + 4 - 6 * V * Math.sin(theta - df1 * phi - angrad)); var fx = (ix + 4 + 12 * V * BU * Math.sin(theta - df1 * phi - angrad)); var fy = (iy + 4 + 12 * V * BU * Math.cos(theta - df1 * phi - angrad)); ctx.moveTo( c2x, c2y); ctx.lineTo( fx, fy); ctx.strokeStyle="green"; drawArrow(ctx, ix + 4, iy + 4, fx, fy); ctx.strokeStyle="black"; ctx.arc(ix, iy, 8,0,2*Math.PI,false); drawArrow(ctx, ix + 4, iy + 4, ex, ey); if (iy > D - 4) { crossedagain = true; } } } //crossing the second field else { ctx.strokeStyle="red"; ctx.drawArc( (c1x - R1), (c1y - R1), (2 * R1), (2 * R1), sa1, ea1); ctx.strokeStyle="blue"; ctx.arc( c2x - 3, c2y - 3, 6,0,2*Math.PI,false); ctx.drawArc( (c2x - R2), (c2y - R2), (2 * R2), (2 * R2), sa2, ea2); if (BL == 0) { ix = (cp2x + 0.2 * V * (frame - ff2) * Math.cos(theta)); iy = (D + 0.2 * V * (frame - ff2) * Math.sin(theta)); var ex = (ix + 6 * V * Math.cos(theta)); var ey = (iy + 6 * V * Math.sin(theta)); ctx.strokeStyle="black"; ctx.arc(ix - 4, iy - 4, 8,0,2*Math.PI,false); drawArrow(ctx, ix, iy, ex, ey); ctx.strokeStyle="magenta"; ctx.moveTo( cp2x, D); ctx.lineTo( ix, iy); ctx.strokeStyle="red"; ctx.moveTo( spx, spy); ctx.lineTo( cp1x, D); } else { ctx.strokeStyle="magenta"; ctx.arc( c3x - 3, c3y - 3, 6,0,2*Math.PI,false); angrad = df1 * 0.2 * AV1 * (frame - ff2); ix = (c3x + df1 * R1 * Math.sin(theta - df1 * phi + angrad)) - 4; iy = (c3y - df1 * R1 * Math.cos(theta - df1 * phi + angrad)) - 4; angdeg = (angrad * 57.2957795 + 0.5); ctx.strokeStyle="magenta"; ctx.drawArc( (c3x - R1), (c3y - R1), (2 * R1), (2 * R1), sa3, -angdeg); var ex = (ix + 4 + 6 * V * Math.cos(theta - df1 * phi + angrad)); var ey = (iy + 4 + 6 * V * Math.sin(theta - df1 * phi + angrad)); var fx = (ix + 4 - 12 * V * BL * Math.sin(theta - df1 * phi + angrad)); var fy = (iy + 4 + 12 * V * BL * Math.cos(theta - df1 * phi + angrad)); ctx.moveTo( c3x, c3y); ctx.lineTo( fx, fy); ctx.strokeStyle="green.darker("; drawArrow(ctx, ix + 4, iy + 4, fx, fy); ctx.strokeStyle="black"; ctx.arc(ix, iy, 8,0,2*Math.PI,false); drawArrow(ctx, ix + 4, iy + 4, ex, ey); } if (frame - ff2 == ff1) { done = true; halt(); ctx.strokeStyle="red"; if (BL != 0) { ctx.moveTo( c1x, c1y); ctx.lineTo( spx, spy); } ctx.strokeStyle="blue"; if (BU != 0) { ctx.moveTo( c2x, c2y); ctx.lineTo( c2x, (c2y - R2)); } } } ctx.strokeStyle="black"; ctx.moveTo(5, D); ctx.lineTo( cw, D); cp.sbFrame.setValue(frame); if (frame < ef && running) { frame++; } pavared = true; } } final class MovChgMagCtrlPnl extends JPanel implements ActionListener, ChangeListener { JSlider sbFrame; JSlider sbBL, sbBU, sbMass, sbVel, sbTheta; JLabel labBL, labBU, labMass, labVel, labTheta; JButton butSSR; MovChgMagCanvas canvas; MovChgMagCtrlPnl(MovChgMagCanvas canvas) { this.canvas = canvas; GridBagLayout gridbag = new GridBagLayout(); GridBagConstravars constravars = new GridBagConstravars(); setLayout(gridbag); constravars.fill = GridBagConstravars.BOTH; JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JPanel panel4 = new JPanel(); JPanel panel5 = new JPanel(); JPanel panel6 = new JPanel(); constravars.insets = new Insets(1, 1, 1, 1); buildConstravars(constravars, 0, 0, 1, 1, 1, 1); gridbactx.setConstravars(panel1, constravars); buildConstravars(constravars, 0, 1, 1, 1, 1, 1); gridbactx.setConstravars(panel2, constravars); buildConstravars(constravars, 0, 2, 1, 1, 1, 1); gridbactx.setConstravars(panel3, constravars); buildConstravars(constravars, 0, 3, 1, 1, 1, 1); gridbactx.setConstravars(panel4, constravars); buildConstravars(constravars, 0, 4, 1, 1, 1, 1); gridbactx.setConstravars(panel5, constravars); buildConstravars(constravars, 0, 5, 1, 1, 1, 1); gridbactx.setConstravars(panel6, constravars); panel1.setLayout(gridbag); panel2.setLayout(gridbag); panel3.setLayout(gridbag); panel4.setLayout(gridbag); panel5.setLayout(gridbag); panel6.setLayout(gridbag); panel1.setBorder(new EtchedBorder()); panel2.setBorder(new EtchedBorder()); panel3.setBorder(new EtchedBorder()); panel4.setBorder(new EtchedBorder()); panel5.setBorder(new EtchedBorder()); panel6.setBorder(new EtchedBorder()); add(panel1); add(panel2); add(panel3); add(panel4); add(panel5); add(panel6); constravars.fill = GridBagConstravars.HORIZONTAL; buildConstravars(constravars, 0, 1, 2, 1, 20, 0); labBU = new JLabel("B ( Upper ) = " + canvas.sliderBUVal + " units", JLabel.CENTER); gridbactx.setConstravars(labBU, constravars); panel1.add(labBU); buildConstravars(constravars, 0, 2, 2, 1, 20, 0); sbBU = new JSlider(-5, 5, canvas.sliderBUVal); constravars.insets = new Insets(0, 0, 5, 0); gridbactx.setConstravars(sbBU, constravars); sbBU.addChangeListener(this); panel1.add(sbBU); buildConstravars(constravars, 0, 3, 2, 1, 20, 0); labBL = new JLabel("B ( Lower ) = " + canvas.sliderBLVal + " units", JLabel.CENTER); gridbactx.setConstravars(labBL, constravars); panel2.add(labBL); buildConstravars(constravars, 0, 4, 2, 1, 20, 0); sbBL = new JSlider(-5, 5, canvas.sliderBLVal); gridbactx.setConstravars(sbBL, constravars); sbBL.addChangeListener(this); panel2.add(sbBL); buildConstravars(constravars, 0, 7, 2, 1, 20, 0, 5, 0); labMass = new JLabel("Mass = " + canvas.sliderMassVal + " units", JLabel.CENTER); gridbactx.setConstravars(labMass, constravars); panel3.add(labMass); buildConstravars(constravars, 0, 8, 2, 1, 20, 0); sbMass = new JSlider(1, 10, canvas.sliderMassVal); gridbactx.setConstravars(sbMass, constravars); sbMass.addChangeListener(this); panel3.add(sbMass); buildConstravars(constravars, 0, 9, 2, 1, 20, 0); labVel = new JLabel("Velocity = " + canvas.sliderVelVal + " units", JLabel.CENTER); gridbactx.setConstravars(labVel, constravars); panel4.add(labVel); buildConstravars(constravars, 0, 10, 2, 1, 20, 0); sbVel = new JSlider(1, 10, canvas.sliderVelVal); gridbactx.setConstravars(sbVel, constravars); sbVel.addChangeListener(this); panel4.add(sbVel); buildConstravars(constravars, 0, 11, 2, 1, 20, 0); labTheta = new JLabel("Angle = 90 \u00b0", JLabel.CENTER); gridbactx.setConstravars(labTheta, constravars); panel5.add(labTheta); buildConstravars(constravars, 0, 12, 2, 1, 20, 0); sbTheta = new JSlider(0, 36, 18); gridbactx.setConstravars(sbTheta, constravars); sbTheta.addChangeListener(this); panel5.add(sbTheta); buildConstravars(constravars, 0, 13, 2, 1, 20, 0); butSSR = new JButton("Start"); constravars.fill = GridBagConstravars.VERTICAL; gridbactx.setConstravars(butSSR, constravars); panel6.add(butSSR); butSSR.setActionCommand("start"); butSSR.addActionListener(this); buildConstravars(constravars, 0, 14, 1, 1, 20, 0); sbFrame = new JSlider(0, 250, 0); sbFrame.addChangeListener(this); constravars.fill = GridBagConstravars.HORIZONTAL; gridbactx.setConstravars(sbFrame, constravars); panel6.add(sbFrame); sbBL.setSnapToTicks(true); sbBU.setSnapToTicks(true); sbMass.setSnapToTicks(true); sbVel.setSnapToTicks(true); sbTheta.setSnapToTicks(true); setPreferredSize(new Dimension(150, 300)); } void buildConstravars(GridBagConstravars gbc, var gx, var gy, var gw, var gh, var wx, var wy) { gbc.gridx = gx; gbc.gridy = gy; gbc.gridwidth = gw; gbc.gridheight = gh; gbc.weightx = wx; gbc.weighty = wy; } void buildConstravars(GridBagConstravars gbc, var gx, var gy, var gw, var gh, var wx, var wy, var top, var bottom) { gbc.gridx = gx; gbc.gridy = gy; gbc.gridwidth = gw; gbc.gridheight = gh; gbc.weightx = wx; gbc.weighty = wy; gbc.insets = new Insets(2, 5, 2, 5); } public void actionPerformed(ActionEvent e) { if (e.getSource() == butSSR) { if (canvas.running == false) { canvas.running = true; canvas.start(); butSSR.setText("Stop"); butSSR.setActionCommand("stop"); } else { canvas.running = false; canvas.stop(); butSSR.setText("Start"); butSSR.setActionCommand("start"); } if (e.getActionCommand().equals("stop")) { butSSR.setText("Start"); butSSR.setActionCommand("start"); } else if (e.getActionCommand().equals("start")) { butSSR.setText(" Stop "); butSSR.setActionCommand("stop"); canvas.running = true; canvas.start(); } if (e.getActionCommand().equals("repeat")) { butSSR.setForeground(Color.black); canvas.reset(); canvas.running = true; canvas.start(); butSSR.setText("Stop"); butSSR.setActionCommand("stop"); } } } public void stateChanged(ChangeEvent e) { if (sbFrame.getValueIsAdjusting()) { canvas.running = false; canvas.crossed = false; canvas.crossedagain = false; canvas.done = false; butSSR.setForeground(Color.black); butSSR.setText("Start"); butSSR.setActionCommand("start"); canvas.frame = sbFrame.getValue(); if (canvas.frame > canvas.ff1) { canvas.crossed = true; if (canvas.frame > canvas.ff2 && canvas.BU != 0) { canvas.crossedagain = true; } } canvas.drawCanvas(); } if (e.getSource() == sbBL) { canvas.sliderBLVal = (sbBL.getValue()); sbBL.setValue(sbBL.getValue()); canvas.BL = 0.05 * canvas.sliderBLVal; String str = new String(" units"); if (canvas.sliderBLVal == 1 || canvas.sliderBLVal == -1) { str = " unit"; } labBL.setText("B ( Lower ) = " + canvas.sliderBLVal + str); canvas.reset(); } if (e.getSource() == sbBU) { canvas.sliderBUVal = sbBU.getValue(); sbBU.setValue(canvas.sliderBUVal); canvas.BU = 0.05 * canvas.sliderBUVal; String str = new String(" units"); if (canvas.sliderBUVal == 1 || canvas.sliderBUVal == -1) { str = " unit"; } labBU.setText("B ( Upper ) = " + canvas.sliderBUVal + str); canvas.reset(); } if (e.getSource() == sbMass) { canvas.sliderMassVal = sbMass.getValue(); sbMass.setValue(canvas.sliderMassVal); canvas.M = 0.2 * canvas.sliderMassVal; String str = new String(" units"); if (canvas.sliderMassVal == 1) { str = " unit"; } labMass.setText("Mass = " + canvas.sliderMassVal + str); canvas.reset(); } if (e.getSource() == sbVel) { canvas.sliderVelVal = (sbVel.getValue()); sbVel.setValue(sbVel.getValue()); canvas.V = 2 * canvas.sliderVelVal; String str = new String(" units"); if (canvas.sliderVelVal == 1) { str = " unit"; } labVel.setText(" Velocity = " + canvas.sliderVelVal + str); canvas.reset(); } if (e.getSource() == sbTheta) { var vsb = sbTheta.getValue(); sbTheta.setValue(vsb); canvas.theta = vsb * Math.PI / 36; labTheta.setText("Angle = " + 5 * vsb + " \u00b0"); canvas.reset(); } if (e.getSource() != sbFrame) { canvas.done = false; canvas.running = false; butSSR.setForeground(Color.black); butSSR.setText("Start"); butSSR.setActionCommand("start"); canvas.drawCanvas(); } } }