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

📄 physics3dframe.java

📁 运用java3D模拟刚体间的碰撞,爆炸及在万有引力作用下的运动轨迹,设置适当的参数可以模拟天体运动等多种物理现象.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		{
			JOptionPane.showMessageDialog(this, "Error opening file for output", "Error!",  JOptionPane.ERROR_MESSAGE);
		}
	
	}
		
	
	
	public void fileMenuOpenActionPerformed(java.awt.event.ActionEvent e) {
						
			try
			{
				chooser = new JFileChooser("Examples");
				
				ExampleFileFilter filter = new ExampleFileFilter();
   				filter.addExtension("p3d");
    			filter.setDescription("Physics 3D Files");
   				chooser.setFileFilter(filter);
				
				//Get the new file
				int fileChooserReturn = chooser.showDialog(this, "Load");
				//If the user said ok load the new file
				if(fileChooserReturn == JFileChooser.APPROVE_OPTION)
				{	
					fileName = chooser.getSelectedFile();
					BufferedReader r = new BufferedReader(new FileReader(chooser.getSelectedFile()));
					Particle3D.removeAll();
					Particle3D.loadAll(r, t);
					r.close();
					this.setTitle("Physics 3D - " + fileName.getName());
				}
				
			}
			//If the file wasn't found, show an error dialog
			catch(FileNotFoundException exc)
			{
				JOptionPane.showMessageDialog(this, "File not Found", "Error!",  JOptionPane.ERROR_MESSAGE);
			}
			catch(IOException exc)
			{
				JOptionPane.showMessageDialog(this, "Corrupt Save File", "Error!",  JOptionPane.ERROR_MESSAGE);
			}
			catch(NumberFormatException exc)
			{
				JOptionPane.showMessageDialog(this, "Corrupt Save File", "Error!",  JOptionPane.ERROR_MESSAGE);

			}
	
	}
	
	public void fileMenuNewActionPerformed(java.awt.event.ActionEvent e) {
		fileName = null;
		this.setTitle("Physics3D");
		Particle3D.removeAll();
	}
	
	public void fileMenuSaveAsActionPerformed(java.awt.event.ActionEvent e) {
		try
		{
			chooser = new JFileChooser();
			//Get the new file
			int fileChooserReturn = chooser.showSaveDialog(this);
			//If the user said ok save the colony to the selected file
			if(fileChooserReturn == JFileChooser.APPROVE_OPTION)
			{
				fileName = chooser.getSelectedFile();
				BufferedWriter b = new BufferedWriter(new FileWriter(chooser.getSelectedFile()));	
				Particle3D.saveAll(b);
				b.close();
				this.setTitle("Physics 3D - " + fileName.getName());
			}
				
		}
		//If there is an error writing to the file, show an error dialog
		catch(IOException exc)
		{
			JOptionPane.showMessageDialog(this, "Error opening file for output", "Error!",  JOptionPane.ERROR_MESSAGE);
		}
	
	}
	
	public void CreatePositionTrackerActionPerformed(java.awt.event.ActionEvent e) {
		Object[] particles = Particle3D.getParticles();
		if(particles.length <= 0)
		{
			JOptionPane.showMessageDialog(this, "Error: You must create an object in order to track it", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		Particle3D p = (Particle3D)JOptionPane.showInputDialog(this, "Select the Particle to Track", "Create Position Tracker",
									            JOptionPane.INFORMATION_MESSAGE, null, particles, 
									            particles[0]);
		if(p == null)
			return;
			
		PositionParticle3DTracker tracker = new PositionParticle3DTracker(p);
	}
	
	public void CreateVelocityTrackerActionPerformed(java.awt.event.ActionEvent e) {
		Object[] particles = Particle3D.getParticles();
		if(particles.length <= 0)
		{
			JOptionPane.showMessageDialog(this, "Error: You must create an object in order to track it", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		Particle3D p = (Particle3D)JOptionPane.showInputDialog(this, "Select the Particle to Track", "Create Velocity Tracker",
									            JOptionPane.INFORMATION_MESSAGE, null, particles, 
									            particles[0]);
		if(p == null)
			return;
			
		VelocityParticle3DTracker tracker = new VelocityParticle3DTracker(p);

	}
	
	public void CreateOrientationTrackerActionPerformed(java.awt.event.ActionEvent e) {
		Object[] particles = Particle3D.getParticles();
		if(particles.length <= 0)
		{
			JOptionPane.showMessageDialog(this, "Error: You must create an object in order to track it", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		Particle3D p = (Particle3D)JOptionPane.showInputDialog(this, "Select the Particle to Track", "Create Position Tracker",
									            JOptionPane.INFORMATION_MESSAGE, null, particles, 
									            particles[0]);
		if(p == null)
			return;
			
		OrientationParticle3DTracker tracker = new OrientationParticle3DTracker(p);
	}
	
	public void CreateAngularVelocityTrackerActionPerformed(java.awt.event.ActionEvent e) {
		Object[] particles = Particle3D.getParticles();
		if(particles.length <= 0)
		{
			JOptionPane.showMessageDialog(this, "Error: You must create an object in order to track it", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		Particle3D p = (Particle3D)JOptionPane.showInputDialog(this, "Select the Particle to Track", "Create Velocity Tracker",
									            JOptionPane.INFORMATION_MESSAGE, null, particles, 
									            particles[0]);
		if(p == null)
			return;
			
		AngularVelocityParticle3DTracker tracker = new AngularVelocityParticle3DTracker(p);

	}
	
	public void CreateDistanceTrackerActionPerformed(java.awt.event.ActionEvent e) {
		Object[] particles = Particle3D.getParticles();
		if(particles.length <= 0)
		{
			JOptionPane.showMessageDialog(this, "Error: You must create an object in order to track it", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		Particle3D p1 = (Particle3D)JOptionPane.showInputDialog(this, "Select the First Particle to Track", "Create Distance Tracker",
									            JOptionPane.INFORMATION_MESSAGE, null, particles, 
									            particles[0]);
		if(p1 == null)
			return;
			
		Particle3D p2 = (Particle3D)JOptionPane.showInputDialog(this, "Select the Second Particle to Track", "Create Distance Tracker",
									            JOptionPane.INFORMATION_MESSAGE, null, particles, 
									            particles[0]);
		if(p2 == null)
			return;
			
		DistanceParticle3DTracker tracker = new DistanceParticle3DTracker(p1, p2);
	
	}
	
	public void CreateMomentumTrackerActionPerformed(java.awt.event.ActionEvent e) {
		Object[] particles = Particle3D.getParticles();
		if(particles.length <= 0)
		{
			JOptionPane.showMessageDialog(this, "Error: You must create an object in order to track it", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		Particle3D p = (Particle3D)JOptionPane.showInputDialog(this, "Select the Particle to Track", "Create Momentum Tracker",
									            JOptionPane.INFORMATION_MESSAGE, null, particles, 
									            particles[0]);
		if(p == null)
			return;
			
		MomentumParticle3DTracker tracker = new MomentumParticle3DTracker(p);

	
	}
	
	
	public void CreateKineticEnergyTrackerActionPerformed(java.awt.event.ActionEvent e) {
		Object[] particles = Particle3D.getParticles();
		if(particles.length <= 0)
		{
			JOptionPane.showMessageDialog(this, "Error: You must create an object in order to track it", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		Particle3D p = (Particle3D)JOptionPane.showInputDialog(this, "Select the Particle to Track", "Create Kinetic Energy Tracker",
									            JOptionPane.INFORMATION_MESSAGE, null, particles, 
									            particles[0]);
		if(p == null)
			return;
			
		KineticEnergyParticle3DTracker tracker = new KineticEnergyParticle3DTracker(p);

	
	}
	
	

	
	public void CreateTimeTrackerActionPerformed(java.awt.event.ActionEvent e) {
		
		TimeParticle3DTracker tracker = new TimeParticle3DTracker();
		
	}
		
	
	//Printing....
	
	
	
	public int print(Graphics g, PageFormat pf, int pageNumber)
	{
		Graphics2D g2d = (Graphics2D)g;
		
		Dimension dPage = new Dimension((int)pf.getImageableWidth(), (int)pf.getImageableHeight());	
		g2d.translate((int)pf.getImageableX(),(int)pf.getImageableY());
		
	
		
		if(pageNumber == 0)
		{
			g2d.drawImage(offScreenCanvas3D.doRender(2*(int)dPage.getWidth(), 2*(int)dPage.getHeight()), 0,0, (int)dPage.getWidth(), (int)dPage.getHeight(), null);
				
		}
		else if(pageNumber == 1)
		{
			
			if(!Particle3DTracker.printAllTrackers(g2d, dPage))
				return NO_SUCH_PAGE;
		}
		else
			return(NO_SUCH_PAGE);
		
		
		return PAGE_EXISTS;
	}
	
	public void fileMenuPrintActionPerformed(java.awt.event.ActionEvent e) {
		
		PrinterJob pj = PrinterJob.getPrinterJob();
		
		pj.setPrintable(this);
		
		if(pj.printDialog())
		{
			try
			{
				pj.print();
			}
			catch(PrinterException exp)
			{
				exp.printStackTrace();
			}

		}
	}
	
	//////////////////////////////
		
	//Creates a x by x grid of particles with random velocities between -1 and 1
	public void CreateRandomParticleGridActionPerformed(java.awt.event.ActionEvent e) {
	
		int magnitude = 2;
		int size = -1;
		
		try
		{
			String sSize = JOptionPane.showInputDialog(this, "Size of Grid?", "3");
			if(sSize == null)
				return;
			
			size = Integer.parseInt(sSize);
		}
		catch(NumberFormatException exp)
		{
			JOptionPane.showMessageDialog(this, "Error You did not enter a number", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
		this.fileMenuNewActionPerformed(e);
		
		int numFromCenterLeft = size / 2;
		int numFromCenterRight = numFromCenterLeft;
		
		if(size % 2 == 0)
			numFromCenterRight--; 
				
		for(int x = 0 - numFromCenterLeft; x <= 0 + numFromCenterRight; x++)
			for(int y = 0 - numFromCenterLeft; y <= 0 + numFromCenterRight; y++)
				for(int z = 0 - numFromCenterLeft; z <= 0 + numFromCenterRight; z++)
				{
					Vector3d velocity = new Vector3d(magnitude * (Math.random() - .5), magnitude * (Math.random() - .5), magnitude * (Math.random() - .5));
					t.addParticle("P" + x + y + z, new Vector3d(x, y, z), velocity, 100, .1, 1);
				}
		
	}	
	/////////////////////////////	
																											
	
	public void HelpAboutActionPerformed(java.awt.event.ActionEvent e) {
		try
		{
			AboutFrame f = new AboutFrame();
			f.initComponents();
			f.setVisible(true);	
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
		}
	}
	
	
	//////////////////
	
	//Mouse Events /////////////////////////////////
	public void mouseEntered(MouseEvent e)
	{
		if(e.getComponent() instanceof JButton && e.getComponent().isEnabled())
			statusLabel.setText(((JButton)e.getComponent()).getToolTipText());
	}
	
	public void mouseExited(MouseEvent e)
	{
		if(startButton.isEnabled())
			statusLabel.setText("Ready");
		else
			statusLabel.setText("Simulating. Press Stop to continue");
	}
	
	public void mouseClicked(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	////////////////////////////////////////
												
}
	
		
	
	

⌨️ 快捷键说明

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