Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

source code for particle applet (warning: a bit long)

Options
  • 14-05-2001 11:08am
    #1
    Registered Users Posts: 2,281 ✭✭✭


    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class ParticleApplet extends DBApplet implements Runnable, ActionListener, ItemListener, AdjustmentListener
    {
    
    	public Button begin, pause;
    	public Universe universe;
    	public boolean paused = false;
    	public boolean created = false;
    	public Scrollbar gravSlider, bounceSlider, partSlider, trailSlider;
    	public Label gravLabel, bounceLabel, partLabel, trailLabel;
    	public Checkbox gravBox, bounceBox, setPositionRandom, setPositionFixed;
    	public CheckboxGroup randomBoxGroup;
    	public Color change = new Color (255,255,255);
    	
    	Particle p[];
    	long delay = 50;
    	
    	// Thread
    	Thread animator = new Thread();
    	
    	
    	public void init()
    	{
    		
    		// g = 9.8065
    		// UNIVERSE VARIABLES: PARTS  TRAILS, MAX SPEED,
    		universe = new Universe(10, 7);
    		
    		setSize(360,550);
    		setBackground(Color.white);
    		setLayout(null);
    		createComponants();		
    		
    		// Create no patricles
    		p = null;
    }
    	
    	
    	/**
         * This method is called when the applet becomes visible on
         * the screen. Create a thread and start it.
         */
        public void start() {
    	animator = new Thread(this);
    	animator.start();
        }
    	
    	
        public void run() {
    		while (Thread.currentThread() == animator) {
    			if(paused == false && p != null && created == true){
    				for(int i = 0; i  < p.length; i++){
    					p[i].update();
    				}
    			}
    			repaint();
    			try{Thread.sleep(delay);}catch(InterruptedException e){break;}
    		}
    	}
    	
    
    
    	public void createComponants(){
    		// BEGIN BUTTON	
    		begin = new Button("start");
    		begin.setBounds(235,420,45,40);
    		begin.addActionListener(this);
    		this.add(begin);
    		
    		// pause BUTTON	
    		pause = new Button("pause");
    		pause.setBounds(295,420,45,40);
    		pause.addActionListener(this);
    		this.add(pause);
    		
    		// Gravity
    		gravBox = new Checkbox();
    		gravBox.setBounds(120,420,20,20);
    		gravBox.setState(false);
    		gravBox.addItemListener(this);
    		this.add(gravBox);
    		
    		gravLabel = new Label("Gravity: Off");
    		gravLabel.setBounds(140,420,80,20);
    		this.add(gravLabel);
    		
    		gravSlider = new Scrollbar();
    		gravSlider.setMaximum(210); 
    		gravSlider.setMinimum(0);
    		gravSlider.setOrientation(Scrollbar.HORIZONTAL);
    		gravSlider.setUnitIncrement(1);
    		gravSlider.setValue((int)(universe.getGravValue()*10));
    		gravSlider.setEnabled(false);
    		gravSlider.setBounds(120,440,100,20);
    		gravSlider.addAdjustmentListener(this);
    		this.add(gravSlider);
    		
    		// Particle Slider
    		partSlider = new Scrollbar();
    		partSlider.setMaximum(50); 
    		partSlider.setMinimum(1);
    		partSlider.setOrientation(Scrollbar.HORIZONTAL);
    		partSlider.setUnitIncrement(1);
    		partSlider.setValue(universe.getTrail());
    		partSlider.setEnabled(true);
    		partSlider.setBounds(125,495,100,20);
    		partSlider.addAdjustmentListener(this);
    		this.add(partSlider);
    		
    		// Part Label
    		partLabel = new Label("Particles: "+universe.getParts());
    		partLabel.setBounds(125,475,100,20);
    		this.add(partLabel);
    		
    		// Trail Slider
    		trailSlider = new Scrollbar();
    		trailSlider.setMaximum(50); 
    		trailSlider.setMinimum(1);
    		trailSlider.setOrientation(Scrollbar.HORIZONTAL);
    		trailSlider.setUnitIncrement(1);
    		trailSlider.setValue(universe.getTrail());
    		trailSlider.setEnabled(true);
    		trailSlider.setBounds(230,495,100,20);
    		trailSlider.addAdjustmentListener(this);
    		this.add(trailSlider);
    		
    		// Trail Label
    		trailLabel = new Label("Trail: "+universe.getTrail());
    		trailLabel.setBounds(230,475,100,20);
    		this.add(trailLabel);
    		
    		// Bounce
    		bounceBox = new Checkbox();
    		bounceBox.setBounds(10,420,20,20);
    		bounceBox.setState(false);
    		bounceBox.addItemListener(this);
    		this.add(bounceBox);
    		
    		bounceLabel = new Label("Bounce: Full");
    		bounceLabel.setBounds(30,420,80,20);
    		this.add(bounceLabel);
    		
    		bounceSlider = new Scrollbar();
    		bounceSlider.setMaximum(110); 
    		bounceSlider.setMinimum(0);
    		bounceSlider.setOrientation(Scrollbar.HORIZONTAL);
    		bounceSlider.setUnitIncrement(1);
    		bounceSlider.setValue((int)(universe.getBounce()*100));
    		bounceSlider.setEnabled(false);
    		bounceSlider.setBounds(10,440,100,20);
    		bounceSlider.addAdjustmentListener(this);
    		this.add(bounceSlider);
    		
    		randomBoxGroup = new CheckboxGroup();
    		setPositionRandom = new Checkbox("Random Postion", randomBoxGroup, true);
    		setPositionRandom.setBounds(10,475,100,20);
    		setPositionFixed = new Checkbox("Fixed Position", randomBoxGroup, false);
    		setPositionFixed.setBounds(10,495,100,20);
    		this.add(setPositionRandom);
    		this.add(setPositionFixed);
    
     
    	}
    	
    	
    	public void updateGravity(){
    		if(gravBox.getState() == false){
    			universe.setGrav(0);
    			gravSlider.setEnabled(false);
    			gravLabel.setText("Gravity: Off");
    		}
    		if(gravBox.getState() == true){
    			gravSlider.setEnabled(true);
    			universe.setGrav(((double)gravSlider.getValue())/10);
    			gravLabel.setText("Gravity: " + universe.getGravValue());			
    		}
    	}
    	
    	public void updateBounce(){
    		if(bounceBox.getState() == false){
    			universe.setBounce(1);
    			bounceSlider.setEnabled(false);
    			bounceLabel.setText("Bounce: Full");
    		}
    		if(bounceBox.getState() == true){
    			bounceSlider.setEnabled(true);
    			universe.setBounce(((double)(bounceSlider.getValue())/100));
    			bounceLabel.setText("Bounce: " + universe.getBounce());			
    		}
    	}
    	
    	public void createParticles(){
    		created = false;
    		p = new Particle[universe.getParts()];
    		int xp = (int)(Math.random()*340);
    		int yp = (int)(Math.random()*400);
    		for(int i = 0; i < p.length; i++){
    			if (setPositionRandom.getState() == true){
    				p[i] =  new Particle(Math.random()*540, Math.random()*400, universe.randSpeed(), universe.randSpeed(), universe);
    			}else{
    				p[i] =  new Particle(xp, yp, universe.randSpeed(), universe.randSpeed(), universe);
    			}
    		}
    		created = true;
    	}
    	
    	
    	public void paint(Graphics g){
    		g.clearRect(0,0,550,520);
    		g.setColor(Color.black);
    		g.drawRect(5,415,220,50);
    		g.drawRect(230,415,55,50);
    		g.drawRect(290,415,55,50);
    		g.drawRect(5,470,110,50);
    		g.drawRect(120,470,225,50);
    		if (p != null){
    			for(int i = 0; i  < p.length; i++){
    				for (int k = (p[0].getCurrentTrail()-1); k  >= 0; k--){
    					setNewColor(k);
    					g.setColor(change);
    					g.fillOval( (int)p[i].getX(k), (int)p[i].getY(k), 3, 3);
    				}
    			}
    		}
    	}
    	
    	public void setNewColor(int k){
    		int c = (int)(255/p[0].getCurrentTrail())*k;
    		change = new Color(c,c,c);
    	}
    	
    	
    	
    	public void actionPerformed(ActionEvent e){
    		if (e.getSource() == pause && paused == false){
    			paused = true;
    			pause.setLabel("play");
    		}else if (e.getSource() == pause && paused == true){
    			paused = false;
    			pause.setLabel("pause");
    		}
    		if (e.getSource() == begin){
    			createParticles();
    			begin.setLabel("restart");
    		}
    	}
    	
    	public void itemStateChanged(ItemEvent e){
    		if (e.getSource() == gravBox){
    			updateGravity();
    		}
    		if (e.getSource() == bounceBox){
    			updateBounce();
    		}
    	}
    	
    	
    	public void adjustmentValueChanged(AdjustmentEvent e){
    		if (e.getSource() == gravSlider){
    			updateGravity();
    		}
    		if (e.getSource() == bounceSlider){
    			updateBounce();
    		}
    		if (e.getSource() == partSlider){
    			universe.setParts(partSlider.getValue());
    			partLabel.setText("Particles: "+universe.getParts());
    		}
    		if (e.getSource() == trailSlider){
    			universe.setTrail(trailSlider.getValue());
    			trailLabel.setText("Trail: "+universe.getTrail());
    		}
    	}
    }
    
    public class Particle
    {
    
    	public GPoint[] p;
    	public Vector d;
    	int currentTrail;
    	// public double radius;
    	// public double mass;
    	public Universe universe;
    	
    	public Particle(double x, double y, double i, double j, Universe universe){
    		p = new GPoint[universe.getTrail()];
    		for (int k = 0; k <p.length; k++){
    			p[k] = new GPoint(x,y);
    		}
    		d = new Vector(i,j);
    		this.universe = universe;
    		currentTrail = universe.getTrail();
    	}
    	
    	public void update() {
    		for (int k = p.length-1; k >0; k--){
    			p[k] = new GPoint(p[k-1].getDoubleX(),p[k-1].getDoubleY());
    		}
    		p[0].setX(p[0].getDoubleX() + d.geti());	
    		p[0].setY(p[0].getDoubleY() + d.getj());
    		d.setj(universe.getGrav().getj() + d.getj())
    			;
    		if(p[0].getX() > 340 && d.geti() > 0){
    			d.seti(d.geti() * (universe.getBounce()*-1));
    		}
    		if(p[0].getX() < 0   && d.geti() < 0){
    			d.seti(d.geti() * (universe.getBounce()*-1));
    		}
    		if(p[0].getY() > 400 && d.getj() > 0){
    			d.setj(d.getj() * (universe.getBounce()*-1));
    		}
    		if(p[0].getY() < 0   && d.getj() < 0){
    			d.setj(d.getj() * (universe.getBounce()*-1));
    		}		
    	}
    	
    	public static void checkHits(Particle[] parts){
    		for (int i = 1; i < parts.length; i++){
    			for (int j = 0; j < i; j++){
    				 // compare particleArray[i] with particleArray[j]
    			}
    		}
    	}
    	
    	public double getX(int k){
    		return p[k].getX();
    	}
    	public double getY(int k){
    		return p[k].getY();
    	}	
    	public int  getCurrentTrail(){
    		return this.currentTrail;
    	}
    		
    }
    import java.applet.Applet;
    import java.awt.*;
    
    public class DBApplet extends Applet
        {
    
    	Image offScreenBuffer;
    
    public void update(Graphics g)
        {
        Graphics gr; 
    	if (offScreenBuffer==null || (! (offScreenBuffer.getWidth(this) == this.getSize().width && offScreenBuffer.getHeight(this) == this.getSize().height)))
            {
            offScreenBuffer = this.createImage(getSize().width, getSize().height);
            }
        gr = offScreenBuffer.getGraphics();
        paint(gr);
        g.drawImage(offScreenBuffer, 0, 0, this);
        }
    }
    
    


    - Dead Bank Clerk -
    "Build a man a fire, and he'll
    be warm for a day. Set a man on
    fire, and he'll be warm for the
    rest of his life."


Comments

  • Registered Users Posts: 7,626 ✭✭✭smoke.me.a.kipper


    nice one Marc. smile.gif

    avatar.gif - Ciaran
    <font face="Verdana, Arial" size="2">"It is hard enough to remember my opinions, without also remembering my reasons for them!"
    -Nietzsche</font>


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    thanks dbc, really appreciate it.


  • Closed Accounts Posts: 218 ✭✭Void


    Nice code! No OpenGL unfortunately.
    I tried to get it working before with Java, but unfortunately to no avail. Mainly because I've never used Java (!). Anyone else tried this?



  • Registered Users Posts: 1,481 ✭✭✭satchmo


    he hasn't listed the Universe class, that's probably one of the reasons it's not working.

    Nice neat code though.


  • Closed Accounts Posts: 1,484 ✭✭✭El_Presidente


    Ummm, doesnt seem to want to work for me. Gives a rake of compile errors, I'm sure I'm just doing something stupid.

    Is the Universe class missing?

    If so, any chance of posting it up so I can see this thing running.


  • Advertisement
  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    you want all the classes? gah... here goes....


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    public class GPoint {
    
    
    /*
    	=============================
    	|       -- GPoint --        |
    	| Graphical Point Class     |
    	|       Version 1.0         |
    	| Written by Marc O'Morain  |
    	|      omorainm@tcd.ie      |
    	=============================
    */
    
    /*
    	========================
    	| Store the x,y values |
    	| as doubles.          |
    	========================
    */
    
    	private double x, y;
    
    
    /*
    	=============================
    	| Constructor - can take in |
    	| both doubles and ints.    |
    	=============================
    */
    
    	public GPoint (double x, double y) {
    		this.x = x;
    		this.y = y;
    	}
    
    	public GPoint (int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    	
    
    /*
    	=============================
    	| distance between 2 points |
    	=============================
    */
    	
    	public double distance(GPoint other){
    		double xsqrd = (other.getDoubleX() - this.getDoubleX())*(other.getDoubleX() - this.getDoubleX());
    		double ysqrd = (other.getDoubleY() - this.getDoubleY())*(other.getDoubleY() - this.getDoubleY());
    		double answer = Math.sqrt(xsqrd + ysqrd);
    		return answer;
    	}
    	
    	
    	
    /*
    	=============================
    	| Set methods - can take in |
    	| both doubles and ints.    |
    	=============================
    */
    
    	public void setX (double x) {
    		this.x = x;
    	}
    
    	public void setY (double y) {
    		this.y = y;
    	}
    		
    
    	public void setX (int x) {
    		this.x = x;
    	}
    
    	public void setY (int y) {
    		this.y = y;
    	}
    
    /*
    	===============================
    	| Get methods - returns the   |
    	| values as ints for graphics |
    	===============================
    */
    
    	public int getX () {
    		return (int)this.x;
    	}
    
    	public int getY () {
    		return (int)this.y;
    	}
    
    
    /*
    	================================
    	| Get double methods - returns |
    	| the values as doubleraphics  |
    	================================
    */
    
    	public double getDoubleX () {
    		return x;
    	}
    
    	public double getDoubleY () {
    		return y;
    	}
    
    }
    public class Vector
    {
    	double i, j;
    	
    	public Vector(double i,double j){
    		this.i = i;
    		this.j = j;
    	}
    	
    	
    	public double geti(){
    		return this.i;
    	}
    	public double getj(){
    		return this.j;
    	}
    	
    	
    	public void seti(double newi){
    		this.i = newi;
    	}
    	public void setj(double newj){
    		this.j = newj;
    	}
    	
    }
    
    import java.awt.*;
    public class Universe
    {
    	
    	public int parts;
    	public Vector gravity;
    	double gravValue;
    	public int trail;
    	public double maxSpeed;
    	public double bounce;
    	
    	public Universe(int parts, double maxSpeed){
    		this.parts = parts;
    		this.gravValue = 0;
    		gravity = new Vector(0, 0);
    		this.trail = 10;
    		this.maxSpeed = maxSpeed;
    		this.bounce = -1;
    		
    		
    	}
    	
    	public int getParts(){
    		return this.parts;
    	}
    	public void setParts(int parts){
    		this.parts = parts;
    	}
    	
    	public Vector getGrav(){
    		return gravity;
    	}
    	
    	public void setGrav(double g){
    		this.gravValue = g;
    		gravity = new Vector(0, g/20);
    	}
    	
    	public double getGravValue(){
    		return gravValue;
    	}
    	
    	public double getBounce(){
    		return bounce * -1;
    	}
    	
    	public void setBounce(double b){
    		this.bounce = b * -1;
    	}
    	
    	public int getTrail(){
    		return this.trail;
    	}
    	
    	public void setTrail(int trail){
    		this.trail = trail;
    	}
    
    	
    	public double getMaxSpeed(){
    		return this.maxSpeed;
    	}
    	
    	public double randSpeed(){
    		double rand = (Math.random()* maxSpeed);
    		double minus = (Math.random() * 100);
    		minus = Math.pow((-1),(int)minus);
    		return rand * minus;	
    	}
    }
    

    poo! i had left this out so that you couldn't rob my code!!

    /me shakes his fist angrilly !!¬!


  • Closed Accounts Posts: 61 ✭✭wish


    robing , coding,
    its all good...

    now do the same in openGL ,
    then tell me how ok....

    but really
    fair play to you for getting it down.


    life is a glitch...
    ...Then you die


Advertisement