Faire defiler une image

Contenu du snippet


Source / Exemple :


import java.applet.Applet;
import java.awt.*;

public class defil extends Applet implements Runnable
{

private Image my_image;

private Thread my_thread;

private int my_position = 500;
private int position_start = 500;
private int position_limit = -500;
private MediaTracker mt;

private Image dbBuffer;
private Graphics dbGraphics;
private Color dbColor;

public static String imageName = "image.gif";

public static int moveIncrement = 2;

public void init()
{
	setBackground(Color.white);
	my_image = getImage(getDocumentBase(), imageName);
	mt = new MediaTracker(this);
	mt.addImage(my_image, 0);
	dbColor = Color.white;
}

public void start()
{
	try
	{
		mt.waitForID(0);
	}
        catch (InterruptedException e)
        { }

	System.out.println("Veuillez patienter...");
	Dimension d = getSize();
	position_start = d.width;
	my_position = position_start;
	position_limit = -(my_image.getWidth(this));
	dbBuffer = createImage(d.width, d.height);
	dbGraphics = dbBuffer.getGraphics();
	dbGraphics.setColor(dbColor);
	if (my_thread == null)
        {
		my_thread = new Thread(this);
		my_thread.start();
	}
}

public void stop()
{
	if (my_thread != null)
	{
		my_thread.interrupt();
		my_thread = null;
	}
}

public void run()
{
	Dimension d = getSize();
	while(true)
	{
		if (my_position > position_limit)
			my_position -= moveIncrement;
		else
			my_position = position_start;

		synchronized(dbBuffer)
	        {
			dbGraphics.fillRect(0, 0, d.width, d.height);
			dbGraphics.drawImage(my_image, my_position, 0, this);
		}

		repaint();

		try
		{
			if (my_thread.isInterrupted()) return;
				Thread.sleep(25);
		}
		catch(InterruptedException e)
		{
			return;
		}
	}
}

public void update (Graphics g)
{
	paint(g);
}

public void paint (Graphics g)
{
	synchronized (dbBuffer)
        {
		g.drawImage(dbBuffer, 0, 0, this);
	}
}

}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.