Animation help 3

Mike Eghtebas
Mike Eghtebas used Ask the Experts™
on
As shown below, a circular drum with a string wrapped around it is pulled in x-direction. as a result:

1. The drum rotates (see angle a).
2. Point of drum-string separation (originally at 0 degrees) moves up along the drum surface (see angle b).

The program is to accept the following four variables:
r, h, x inch/sec rate, and max via number of Jtext controls. Upon click the animation to start pulling the string and rotating the drum to max X at the giving rate (inches/sec).

At start, the string is pulled from X=0. In the process of pull, the angle of drum rotation a will be displayed in degrees.

In order see the drum is rotating, we can rotate a redial line drawn from center of the drum to zero angle.

The equation producing the data is given in the attached java program.

Thank you.

The output:
 x = 00.000 a = 00.000 b = 00.000
 x = 50.000 a = 01.759 b = 00.667
 x = 100.000 a = 05.498 b = 00.992
 x = 150.000 a = 09.905 b = 01.153
 x = 200.000 a = 14.571 b = 01.247
 x = 250.000 a = 19.358 b = 01.307
 x = 300.000 a = 24.212 b = 01.348
 x = 350.000 a = 29.105 b = 01.378
.
.

import java.text.DecimalFormat;
public class Ax {
  public static void main(String args[]) {
      DecimalFormat df = new DecimalFormat("00.000");
      double h = 60; //height of drum from base line
      double r = 10; //drum radius
      double k = r/h;
      double j = 0;
      double a = 0;
      double C = 0; // equals cos(b) 
      for(double x = 0; x <= 1000; x += 50){
          j = x/h;
          C = (2*k*(j+k) + Math.sqrt(Math.pow(2*k*(j+k),2) - 4*(Math.pow(j+k,2) + 1)*(k*k - 1)))/(2*(Math.pow(j+k,2) + 1));
          a = (r * Math.tan(Math.acos(C)) + h/C - h)/r - Math.acos(C);
          System.out.println(" x = " + df.format(x) + " a = " + df.format(a) + " b = " + df.format(Math.acos(C)));
      }
  }
}

Open in new window

drum-final.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HainKurtSr. System Analyst

Commented:
ok, whats the question :)
Mike EghtebasDatabase and Application Developer

Author

Commented:
Question: Could you make animation in JApplet or in a java desktop to perform (animate( this operation.

Mike EghtebasDatabase and Application Developer

Author

Commented:
I am new to java and this will be a great start for me to learn more about animation. Also this is not school work.

mike
Mike EghtebasDatabase and Application Developer

Author

Commented:
Also, FYI, the above code is from rrz@871311 who has been helping me on this in last few questions in math and java TA.
HainKurtSr. System Analyst

Commented:
have a look at these sample codes :)

http://java.sun.com/products/java-media/2D/samples/suite/
Commented:
Try this.
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

public class Drum extends JApplet implements ActionListener, ChangeListener {
	int drumX = 20; // location of drum left side
	int drumY = 20; // location of drum top
	int diameter = 30; // drum radius times two
	int pull = drumX + diameter; // horizontal location of bottom of rope where it being pulled
	int speed = 6; // pixels per time interval set by timer
	Animator animator; // the applet's content pane
	JSlider speedValue, dia;
	JLabel speedLabel, radiusLabel;
	JButton start, stop;
	Timer timer; // fires ActionEvent
	private void createGUI() {
		start = new JButton("Start");
		start.addActionListener(this);
		stop = new JButton("Stop");
		stop.addActionListener(this);
		JPanel contentPane = new JPanel(new BorderLayout());
		JPanel controlPanel = new JPanel(new FlowLayout());
		setContentPane(contentPane);
		animator = new Animator();
		animator.setOpaque(true);
		animator.setBackground(Color.WHITE);
		contentPane.add(animator, BorderLayout.CENTER);
		speedLabel = new JLabel("Speed");
		radiusLabel = new JLabel("Radius");
		speedValue = new JSlider(JSlider.HORIZONTAL, 1, 20, 6);
		speedValue.addChangeListener(this);
		dia = new JSlider(JSlider.HORIZONTAL, 20, 60, 40);
		dia.addChangeListener(this);
		controlPanel.add(speedLabel);
		controlPanel.add(speedValue);
		controlPanel.add(radiusLabel);
		controlPanel.add(dia);
		controlPanel.add(start);
		controlPanel.add(stop);
		contentPane.add(controlPanel, BorderLayout.NORTH);
	}
	public void init() {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					createGUI();
				}
			});
		} catch (Exception e) {
			System.err.println("createGUI didn't successfully complete:");
			e.printStackTrace();
		}
		timer = new Timer(100, this);
	}
	public void stateChanged(ChangeEvent e) {
		JSlider source = (JSlider) e.getSource();
		if (!source.getValueIsAdjusting()) {
			int value = (int) source.getValue();
			if (source == speedValue) {
				speed = value;
				animator.repaint();
			}
			if (source == dia) {
				diameter = value;
				pull = drumX + diameter;
				animator.repaint();
			}
		}
	}
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == start) {
			pull = drumX + diameter;
			animator.repaint();
			timer.start();
		} else if (source == stop) {
			timer.stop();
		} else {
			pull = pull + speed;
			animator.repaint();
		}
	}
	public class Animator extends JPanel {
		DecimalFormat df = new DecimalFormat("00.00");
		Graphics2D g2d;
		double b = 0; // angle on drum where rope makes contact		
		double C = 0; // equals cos(b)
		double a = 0; // angle the drum has been rotated
		double h = 200 - drumY - diameter / 2; // height of the center of the drum from base line
		double x = pull - drumX - diameter; // distance that rope was pulled along horizontal base line
		public Animator() {
			super();
		}
		private void calculate() {
			h = 90 - drumY - diameter / 2;
			x = pull - drumX - diameter;
			double j = x / h;
			double k = (diameter/2) / h;
			C = (k * (j + k) + Math.sqrt(j * j + 2 * j * k + 1))
					/ ((j + k) * (j + k) + 1);
			b = Math.acos(C);
			a = (diameter/2 * Math.tan(b) + h/C - h) / (diameter/2) - b;
		}
		protected void paintComponent(Graphics g) {
			g2d = (Graphics2D) g;
			super.paintComponent(g2d);
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			calculate();
			g2d.setColor(Color.YELLOW);
			g2d.fillOval(drumX, drumY, diameter, diameter); // drum
			g2d.setColor(Color.RED);
			//g2d.setStroke(stroke);
			g2d.drawLine(drumX + diameter/2 + (int)((diameter / 2) * Math.cos(b)), 
					     drumY + (int) diameter/2 - (int)((diameter / 2) * Math.sin(b)),
					     pull, 
					     90); // rope
			g2d.setColor(Color.BLACK);
			g2d.fillArc(drumX + 2, drumY + 2, diameter - 4, diameter - 4, -(int)(a * 57.3), 25); // drum marker
			g2d.drawString("Speed is " + (speedValue.getValue()) * 10 + " pixels/second", 30, 110);
			g2d.drawString("Radius is " + diameter/2 + " pixels", 230, 110);
			g2d.drawString("b is " + df.format(b) + " radians", 30, 125);
			g2d.drawString("a is " + df.format(a) + " radians", 250, 125);
			g2d.drawString("h is " + df.format(h), 30, 140);
			g2d.drawString("x is " + df.format(x) + " pixels", 250, 140);
			g2d.setColor(Color.CYAN);
			g2d.drawLine(5, 90, 1000, 90);
			g2d.drawLine(5, 145, 500, 145);
		}
	}
	public void stop() {
		timer.stop();
	}
}

Open in new window

Here is the HTML.
<html>
<head>
    <style type="text/css">
           body{ background-color:#d6eff7;}     
    </style>
<title>Drum Applet</title>
</head>
<body>
<applet code="Drum.class" width="800" height="300"></applet>
</body>
</html>

Open in new window

Mike EghtebasDatabase and Application Developer

Author

Commented:
Amazing. Thank you very much.

Mike
Very nice, rrz. Good working with you.
Mike EghtebasDatabase and Application Developer

Author

Commented:
Paul, Thanks to your valuable contribution as well,


regards,

Mike
You're welcome. Interesting project. Hope to see your actual cam when you complete it.

Commented:
Thanks for the points.

Thanks, phoffric.  

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial