★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/GravSim/gravsim.java.htmlNouvelle recherche | Portail wanayoo
internet.com logo The Java Boutique

 APPLETS
by Category
by Date
by Name
Hall of Fame
Archive
Submit
Source Code
Servlets and JSP
 ARTICLES
Tutorials
Reviews
Glossary
 FORUM
FAQ
Users Poll
Discussion Group
Contact Us
 OTHER RESOURCES
Custom Applets
Java@Work
Java News
Jini Watch
Affiliate Programs
Top Searches
chat - counter
menu - countdown

To Webdeveloper.com
internet.com
Internet News
Internet Stocks/VC
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International

Search internet.com
Advertising Info
Corporate Info

internet.commerce
Be a Commerce Partner
Register Domains
Survey your eMarket
Talk to Your Users
Database Web Tools
Build Your Intranet
Offer Free Email
get e-biz answers
Offer Online Payment
Accept Credit Cards
Download Solutions

WebDeveloper Network
BrowserWatch
Developer Forums
Developer News
JavaBoutique
JavaScriptSource
ScriptSearch
SearchEngineWatch
StreamingMediaWorld
The WDVL
WebDeveloper.com
WebDevelopersJournal WebReference.com
The XML Files
 [ Looking for a Site Map? ]

Free Internet Marketing info!

GravSim Source Code


File gravsim.java:

/*
        Trivial applet that displays two object that move perpendicular to each other.
        
*/

import java.awt.*;
import java.applet.*;
import java.math.*;

public class gravsim extends Applet implements Runnable
{                                       //Initianlise member variables
        Thread mThread; //new Thread
        Image mBuffer;  // image Buffer
        Rectangle mBackground; //Reactagles
        Rectangle mBodyRect1;
        Rectangle mBodyRect2;
//      Image mbody1;   //Images
//      Image mbody2;
        Button mStart, mStop, mReset; //Buttons
        Checkbox mTrace = new Checkbox("Trace");
        boolean bIsDrawing = false;  //Booleans
        boolean bOver = true;
        boolean bTrace = false;
        int mRep;
        double mRatio = 10e8;
        double mXpos1 = 0, mYpos1 = 0;
        double mXpos2 = 0;
        double mYpos2 = 0;
        double mXacc1 = 0, mXacc2 = 0;
        double mYacc1 = 0, mYacc2 = 0;
        double mbody2mass = 6e24, mbody1mass = 2e30;
        double mTime = 3.6e-4;
        int mSleepValue = 10;  //Sleep value
        double mDistancex;
        double mDistancey;
        double mDistanceF;  
        
        public void init() 
        {                                                       //init the process and construct variables
                Dimension d = size();
                mBuffer = createImage(d.width, d.height);
//              mbody1 = getImage(getDocumentBase(), "star.gif");
//              mbody2 = getImage(getDocumentBase(), "star.gif");
                mStart = new Button("start");
                mStop = new Button("pause");
                mReset = new Button("reset");
                mReset.disable();
                mStop.disable(); 
                MediaTracker mt = new MediaTracker(this); // Create Media Tracker..
//          mt.addImage(mbody2,0); // Add the images
//      mt.addImage(mbody1,0);
        add(mStart);
        add(mStop);
        add(mReset);
        add(mTrace);
        mRep = 1;
                mDistanceF = 1;
        mDistancex = 1;
                mDistancey = 1;
                mBodyRect1 = new Rectangle(d.width/2,d.height/2,2,2);
                mBodyRect2 = new Rectangle((d.width/2),(d.height/2+150),1,1);
                try
                {
                        mt.waitForID(0);
                }
                        catch(Exception e)
                {
                        e.printStackTrace();
                }
        repaint();
        }
        
        public void movebodies() //Function to actually move the bodys
        {
                Dimension d = size();
                mYpos1 += mYacc1; 
                mXpos1 += mXacc1;
                mXpos2 += mXacc2; 
                mYpos2 += mYacc2;  
                mBodyRect1.move((int)((mXpos1/mRatio)+(d.width/2)),(int)((mYpos1/mRatio)+d.height/2));
                mBodyRect2.move((int)((mXpos2/mRatio)+(d.width/2)),(int)((mYpos2/mRatio)+d.height/2));
        }
        
        public void distance() // Get the distance points
        {
                mDistancex = ((mXpos1)-(mXpos2));
                mDistancey = ((mYpos1)-(mYpos2));
                mDistanceF = Math.sqrt((Math.pow(mDistancex,2.0))+(Math.pow(mDistancey,2.0)));
        }
        
        public void gravity1()   //New class called Gravity
        { 
        double mgravconst = 6.67e-11;
        mYacc1 +=((mDistancey)*(mbody2mass))*mgravconst*mTime/(Math.pow(mDistanceF,2.0));
        mXacc1 +=((mDistancex)*(mbody2mass))*mgravconst*mTime/(Math.pow(mDistanceF,2.0));
        }
        
        public void gravity2()   //New class called Gravity 
        {
        double mgravconst = 6.67e-11;
        mXacc2 += ((mDistancex)*(mbody1mass))*mgravconst*mTime/(Math.pow(mDistanceF,2.0));
        mYacc2 += ((mDistancey)*(mbody1mass))*mgravconst*mTime/(Math.pow(mDistanceF,2.0));
        }
        
        public boolean action(Event evt, Object what) //Event trapping
    {
        if(evt.target == mStart)
                {
                        if(bOver)
                        {
                                resetOrbit();
                                }
                startOrbit();
                        }
        if(evt.target == mStop)
                {
                pause();        
                }
        if(evt.target == mReset)
                {
                        resetOrbit();
                        repaint();
                }
                if(evt.target == mTrace)
                                {
                                if(!mTrace.getState())
                                        {
                                        mTrace.setState(false);
                                        bTrace = false;
                                        }
                                else{   
                                        mTrace.setState(true);
                                        bTrace = true;
                                        }
                                }
    return false;
    }
    
    public void pause()  //if stop is pushed
    {
        mStart.enable();
        mReset.enable();  
        mThread.stop();
        mStart.enable();
        mStop.disable();
        }
        
        public void startOrbit() //Start the applet when Start is pushed
        {
                mStart.disable();
                mReset.disable();
                mStop.enable();
                bOver = false;
                mThread = new Thread(this);
                mThread.setPriority(Thread.MIN_PRIORITY);
                mThread.start();
    }
    
    public void resetOrbit() // called when button or interception occurs
    {
                Dimension d = size();
                mYacc1 = 0;
                mXacc1 = 0;
                mYacc2 = 0;
                mXacc2 = 0.2e9;
                mXpos1 = 0;
                mBodyRect1.x = /*(int)(mXpos1/mRatio)+*/ (d.width/2);
                mYpos1 = 0;
                mBodyRect1.y = /*(int)(mYpos1/mRatio)+*/ (d.height/2);
                mXpos2 = 0;
                mBodyRect2.x = (d.width/2)+ (int)(mXpos2/mRatio);
                mYpos2 = 1.5e11;
                mBodyRect2.y = (d.height/2)+ (int)(mYpos2/mRatio);
                mRep = 1;
                distance();
                repaint();
    }

        public void stop()
    {
          bOver = true;
          mStart.enable();
          if(mThread != null)
          {
              mThread.stop();
          }
    }
    
        public void update(Graphics g)
    {
                paint(g);
    }
    
    public void paint( Graphics g ) 
        {
                
                Graphics bg = mBuffer.getGraphics();
                Dimension d = size();
        //      bg.clearRect(0,0,d.width,d.height);
                bg.setColor(Color.black);
        //      bg.drawRect(0, 0,d.width,d.height);
                if(!bTrace || mRep < 2)
                {
                        bg.setColor(Color.black);
                        bg.fillRect(0, 0,d.width,d.height);
                }
                bg.setColor(Color.red);
                bg.drawRect(mBodyRect1.x,mBodyRect1.y,mBodyRect1.width,mBodyRect1.height);
                bg.setColor(Color.blue);
                bg.drawRect(mBodyRect2.x,mBodyRect2.y,mBodyRect2.width,mBodyRect2.height);
//              bg.drawImage(mbody1,mbodyRect1.x,mbodyRect1.y,this);
//              bg.drawImage(mbody2,mbodyRect2.y,mbodyRect2.x,this);
                bg.setColor(Color.black);
                bg.fillRect(30,30,140,100);
                bg.setColor(Color.green);
                bg.drawRect(29,19,143,72);
                bg.setColor(Color.white);
                bg.drawString("Rep-"+mRep, 30, 90 );
                bg.drawString("GravSim 1.0", 30, 30 );
                bg.drawString("Sleep- "+mSleepValue, 30,40);
        //      bg.drawString("Distance-"+mDistancex+","+mDistancey+","+mDistanceF,30,50);
        //      bg.drawString("Pos1-"+mXpos1+","+mYpos1,30,60);
        //      bg.drawString("Pos2-"+mXpos2+","+mYpos2,30,70);
                bg.drawString("Location-"+mBodyRect1.x+","+mBodyRect1.y+"."+mBodyRect2.x+","+mBodyRect2.y,30,50);
                bg.drawString("Mass1-"+mbody1mass,30,60);
                bg.drawString("Mass2-"+mbody2mass,30,70);
                bg.drawString("Raw Distance-"+((mBodyRect2.x)-(mBodyRect1.x))+","+((mBodyRect2.y)-(mBodyRect1.y)),30,80);
        //      bg.drawString("Velocity-"+mXacc1+","+mYacc1+"."+mXacc2+","+mYacc2,30,120);
                g.drawImage(mBuffer,0,0,null);
                bIsDrawing = false;
        }
        
        public void run()
        {
                Dimension d = size();
                while (true)
                {
                        distance();
                        gravity1();
                        gravity2();
                        movebodies();
                        mRep++;
                try
                        {
                        if(mSleepValue != 0)
                        {
                        mThread.sleep(mSleepValue);     //slow the whole thing down
                        }
                }
                        catch(Exception e)
                                {
                        System.out.println(e);
                        }
        if(!bIsDrawing)
                {
                        bIsDrawing = true;
                        repaint();
                } 
                                        
                }
        
        }
}
Author: Luke Andrews-Hakken

Homepage: http://newmedia.slc.edu/~luke/astro


Web Marketers' Online Facts



Copyright © 1999 internet.com LLC
All Rights Reserved. Legal Notices.