⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 projectileapplet.java

📁 Java applet that simullate the balistic model
💻 JAVA
字号:

package balistic;
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*; 


public class ProjectileApplet extends Applet implements
				ActionListener, BuddyBar.BuddyListener, ItemListener {
	Projectile bullet; 
	PositionGraph arcs; 
	BuddyBar velocityBuddy; 
	BuddyBar massBuddy;
        BuddyBar distBuddy;
	Checkbox airCheckBox;
	Checkbox trailCheckBox;

	
	public void init() {
		ResourceBundle rb;
        String sResString, sUnitString;
        
		String sUseRB = this.getParameter("international");
		rb = null;
		if ((sUseRB == null) || !sUseRB.equalsIgnoreCase("no")) {
			String sLang = this.getParameter("language");
			String sCountry = this.getParameter("country");
			try {
				try {
					if (sLang == null) {
						rb = ResourceBundle.getBundle("ProjectileStrings",Locale.getDefault());
					} else {
						Locale thisloc;
						if (sCountry == null)
							thisloc = new Locale(sLang,"");
						else
							thisloc = new Locale(sLang,sCountry);
						rb = ResourceBundle.getBundle("ProjectileStrings",thisloc);
					}
				} catch (java.lang.ClassFormatError e) {
					System.out.println("Cannot load the ResourceBundle class.  Must be Netscape.");
					System.out.println("Turning off Internationalization.  Sorry.");
					rb = null;
				}
			} catch (MissingResourceException e) {
				System.out.println(e.getMessage());
				rb = null;
			}
		}
		this.setLayout(new BorderLayout(5,5));
		this.setBackground(Color.gray);

		
		bullet = new Projectile();
		
		arcs = new PositionGraph(bullet, rb);
		this.add("Center",arcs);

		Panel p1 = new Panel(new FlowLayout(FlowLayout.RIGHT));
        if (rb != null) sResString = rb.getString("FOC");
        else sResString = "FOC";
		Button b = new Button(sResString);
		b.setActionCommand("FOC");
		b.addActionListener(this);

        if (rb != null) sResString = rb.getString("aer");
        else sResString = "Rezistenta aerului";
		airCheckBox = new Checkbox(sResString,false);
        if (rb != null) sResString = rb.getString("traiectorie");
        else sResString = "Arata traiectoria";
		trailCheckBox = new Checkbox(sResString,true);
		airCheckBox.addItemListener(this);
		trailCheckBox.addItemListener(this);
		p1.add(airCheckBox);
		p1.add(trailCheckBox);

		
        if (rb != null) sResString = rb.getString("viteza");
        else sResString = "Viteza [m/s]";
		velocityBuddy = new BuddyBar(sResString,1,200,0,1000,50);

	velocityBuddy.addBuddyListener(this);

        if (rb != null) sResString = rb.getString("distanta");
        else sResString = "Distanta [m]";
		massBuddy = new BuddyBar(sResString,1,1000,1,1000,100);
		massBuddy.addBuddyListener(this);

		Panel p3 = new Panel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(4,4,4,4);
		c.gridx = 0; c.gridy = 0; c.gridwidth = 3; c.gridheight = 1;
		c.weighty = 0; c.weightx = 0;
		c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.HORIZONTAL;
		p3.add(p1,c);
		c.gridwidth = 1; c.gridx = 3;
		c.fill = GridBagConstraints.HORIZONTAL;
		p3.add(b,c);
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.EAST;
		c.gridx = 0; c.gridy = 1;
		p3.add(velocityBuddy.getLabel(),c);
		c.anchor = GridBagConstraints.CENTER;
		c.gridx = 1;
		p3.add(velocityBuddy.getTextField(),c);
		c.anchor = GridBagConstraints.EAST;
        	c.gridx=0; c.gridy = 3;
		p3.add(massBuddy.getLabel(),c);
		c.anchor = GridBagConstraints.CENTER;
		c.gridx = 1;
		p3.add(massBuddy.getTextField(),c);
		c.fill = GridBagConstraints.HORIZONTAL;
	
		c.gridwidth=2;
		c.gridx = 2; c.gridy = 1; c.weightx = 1;
		p3.add(velocityBuddy.getScrollbar(),c);

		c.gridy = 3;
		p3.add(massBuddy.getScrollbar(),c);


		this.add("South",p3);

        String sParam = this.getParameter("threadsleep");
        if (sParam != null) {
            try {
                int iSleep = Integer.parseInt(sParam);
                bullet.setSleepTime(iSleep);
			} catch (NumberFormatException e) {
			    System.out.println("threadsleep should be an integer number of milliseconds.");
			}
        }
        sParam = this.getParameter("numsteps");
        if (sParam != null) {
            try {
                int nSteps = Integer.parseInt(sParam);
                bullet.setNumSteps(nSteps);
			} catch (NumberFormatException e) {
			    System.out.println("numsteps should be an integer number of timesteps.");
			}
        }

        sParam = this.getParameter("markfrequency");
        if (sParam != null) {
            try {
                int nSteps = Integer.parseInt(sParam);
                bullet.setMarkFrequency(nSteps);
			} catch (NumberFormatException e) {
			    System.out.println("markfrequency should be an integer like 5 or 10.");
			}
        }
	}

	public String getAppletInfo() {
		return "ProjectileApplet ";
	}
	static final String[][] parameterInfo = {
			{"international","yes or no [yes]", "Setting this to \"no\" turns off internationalization "+
					"in case your browser cannot handle it."},
			{"language","two letter language code", "EN for English, FR for French, etc."},
			{"country","two letter country code", "CN for China, IT for Italy.  Not required."},
			{"threadsleep","integer [20]","Number of milliseconds the gas thread should sleep."},
			{"markfrequency","integer [8]","How many timesteps per trail mark."},
			{"numsteps","integer [125]","Number of timesteps for the ball to travel the "+
			        "a full arc."}
		};
		
	public String[][] getParameterInfo() {
		return parameterInfo;
	}

	public void actionPerformed(ActionEvent e) {
		bullet.setVelocity(velocityBuddy.getValue());
	  double nr;
		bullet.setMass(massBuddy.getValue());
                nr=(9.8*massBuddy.getValue()/(velocityBuddy.getValue()*velocityBuddy.getValue()));
                if (nr >1)
                {
                    JOptionPane.showMessageDialog(this,"Unghiul nu poate fi calculat!Nu se poate calcula arcsin din +"+nr);
                    return; 
                }
                else{
         
                bullet.angle=(0.5*Math.asin(9.8*massBuddy.getValue()/(velocityBuddy.getValue()*velocityBuddy.getValue())));
                System.out.println("Value angle "+ bullet.angle+ "Vellocity "+velocityBuddy.getValue()+" mass"+massBuddy.getValue());
		bullet.fire();
                }
                
	}

	public void buddyValueChanged(BuddyBar changer, double dVal)
	{
		if (changer==velocityBuddy) {
			bullet.setVelocity(dVal);

		} else if (changer == massBuddy) {
			bullet.setMass(dVal);
		}
               }

	
	public void itemStateChanged(ItemEvent evt) {
		if (airCheckBox == evt.getSource()) {
			if (airCheckBox.getState())
				bullet.setResistance(0.5);
			else
				bullet.setResistance(0.0);
		} else {
			if (trailCheckBox.getState())
				arcs.setTrails(true);
			else
				arcs.setTrails(false);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -