All pastes #469144 Raw Edit

Miscellany

public java v1 · immutable
·published 2007-05-03 16:00 UTC
rendered paste body
/*	Applet0a.java	Working with applets.	03May2007*/import java.awt.*;import java.applet.*;import java.util.*;class Applet0a extends Applet implements Runnable {	private Image dbImage;	private Graphics dbg;	private final static int appletW=600, appletH=400;	private final static int DIAMETRO=20, RADIO=10;	private int x1,y1,vx1,vy1;	Random rnd;	Thread th;	public void init() 	{		setBackground(Color.WHITE);		th = new Thread(this);		rnd = new Random();		x1 = rnd.nextInt(appletW);		y1 = rnd.nextInt(appletH);		vx1 = 5;		vy1 = 5;	}	public void start() {		th.start();	}	public void stop() {		th.stop();	}	public void destroy() {		//TODO	}	public void run() {		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);		while (true)		{			moverbala();			repaint();			try			{				Thread.sleep(15);			}			catch (InterruptedException ex)			{				// do nothing			}			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);		}	}	public void moverbala() {		if (x1 > appletW-RADIO)		{			vx1 = -5;		}		if (x1 < RADIO)		{			vx1 = 5;		}		if (y1 > appletH-RADIO)		{			vy1 = -5;		}		if (y1 < RADIO)		{			vy1 = 5;		}		x1 += vx1;		y1 += vy1;	}	public void paint(Graphics g) {		g.setColor(Color.WHITE);		g.fillRect(0,0,appletW,appletH);		g.setColor(Color.BLACK);		g.fillOval(x1-RADIO,y1-RADIO,DIAMETRO,DIAMETRO);	}	public void update(Graphics g) {		// initialize buffer		if (dbImage==null)		{			dbImage = createImage(this.getSize().width,this.getSize().height);			dbg = dbImage.getGraphics();		}		// clear screen in background		dbg.setColor(getBackground());		dbg.fillRect(0,0,this.getSize().width,this.getSize().height);		// draw elements in background		dbg.setColor(getForeground());		paint(dbg);		// draw image on the screen		g.drawImage(dbImage,0,0,this);	}}//EOS