Les fourmis amoureuses

Voici le code Java permettant de dessiner x carrés imbriqués.

package org.intro;
 
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
 
public class FourmisAmoureuses implements GLEventListener
{
	Point<Float> a,b,c,d;
 
	public static void main(String[] args)
	{
		new FourmisAmoureuses();
	}
 
	public FourmisAmoureuses()
	{
		JFrame frame = new JFrame("Les fourmis amoureuses");
		GLCanvas canvas = new GLCanvas();
 
		this.create();
 
		canvas.addGLEventListener(this);
 
		frame.add(canvas);
		frame.setSize(600,600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
 
	public void create()
	{
		a = new Point<Float>(-0.9f, 0.9f);
		b = new Point<Float>(0.9f, 0.9f);
		c = new Point<Float>(0.9f, -0.9f);
		d = new Point<Float>(-0.9f, -0.9f);
	}
 
	public void display(GLAutoDrawable drawable)
	{
		this.create();
 
		int i = 0;
		float factor = 0.008f;
		float X=0, Y=0, x, y;
 
		GL gl = drawable.getGL();
 
	    gl.glEnable(GL.GL_BLEND);
	    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
	    gl.glEnable(GL.GL_LINE_SMOOTH);
 
		gl.glColor3f(1f, 1f, 1f);
		gl.glClear(GL.GL_COLOR_BUFFER_BIT); //On efface le contenu
 
		while(i++ < 1200)
		{
 
			gl.glBegin(GL.GL_LINE_LOOP);
				gl.glVertex2d(a.getX(), a.getY());
				gl.glVertex2d(b.getX(), b.getY());
				gl.glVertex2d(c.getX(), c.getY());
				gl.glVertex2d(d.getX(), d.getY());
			gl.glEnd();
 
			x = a.getX();
			y = a.getY();
 
			X = (b.getX() - a.getX())*factor;
			Y = (b.getY() - a.getY())*factor;
			a.setX(a.getX()+X);
			a.setY(a.getY()+Y);
 
			X = (c.getX() - b.getX())*factor;
			Y = (c.getY() - b.getY())*factor;
			b.setX(b.getX()+X);
			b.setY(b.getY()+Y);
 
			X = (d.getX() - c.getX())*factor;
			Y = (d.getY() - c.getY())*factor;
			c.setX(c.getX()+X);
			c.setY(c.getY()+Y);
 
			X = (x - d.getX())*factor;
			Y = (y - d.getY())*factor;
			d.setX(d.getX()+X);
			d.setY(d.getY()+Y);
 
		}
	}
 
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2)
	{
		System.err.println("DISPLAY CHANGE !!");
	}
 
	public void init(GLAutoDrawable drawable)
	{
		System.err.println("INIT !!");
		GL gl = drawable.getGL();
 
		gl.glClearColor(0f, 0f, 0f, 0f); //Quand on efface, on met cette couleur !!
	}
 
	public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4)
	{
		System.err.println("RESHAPE !!");
	}
 
	public class Point<T>
	{
		private T x;
		private T y;
 
		public Point(T x, T y)
		{
			this.x = x;
			this.y = y;
		}
 
		/**
		 * @return the x
		 */
		public T getX()
		{
			return x;
		}
 
		/**
		 * @param x the x to set
		 */
		public void setX(T x)
		{
			this.x = x;
		}
 
		/**
		 * @return the y
		 */
		public T getY()
		{
			return y;
		}
 
		/**
		 * @param y the y to set
		 */
		public void setY(T y)
		{
			this.y = y;
		}
	}
}

Les listes

	public void createSquare(float cote, GL gl)
	{
		le_carre = gl.glGenLists(1);
		gl.glNewList(le_carre, GL.GL_COMPILE);
		gl.glBegin(GL.GL_LINE_LOOP);
		for(int i; i<4; i++)
		{
			gl.glVertex2f(s[i].x, s[i].y);
		}
		gl.glEnd();
		gl.glEndList();
	}
 
java/jogl/fourmis_amoureuses.txt · Dernière modification: 2008/08/13 13:56 (édition externe)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki