Article View: cn.bbs.comp.lang.java
Article #24358��ʾ��С���JAVAԴ����
From: wooce
Date: Thu, 26 Mar 1998 00:00
Date: Thu, 26 Mar 1998 00:00
314 lines
9939 bytes
9939 bytes
import java.awt.*;
import java.util.StringTokenizer;
/** Applet dessinant une balle qui rebondit sur les parois.
* Vos remarques a vincent.zimmermann@nctech.fr
* Utilisable librement.
*
* Exemple d'utilisation dans une page HTML :
* <APPLET CODE="BalleVZ.class" WIDTH=400 HEIGHT=100>
* <PARAM NAME="ballSize" VALUE="25">
* <PARAM NAME="speed" VALUE="2">
* <PARAM NAME="initialX" VALUE="10">
* <PARAM NAME="initialY" VALUE="5">
* <PARAM NAME="bgColor" VALUE="yellow">
* <PARAM NAME="fgColor" VALUE="black">
* <PARAM NAME="timeSleep" VALUE="15">
* Il aurait du y avoir une applet ici si votre Browser avait supporte Java...
* </APPLET>
*
* Explications :
* bgColor & fgColor : white, black, lightGray, gray, darkGray, red, green,
* blue, yellow, magenta, cyan, pink, orange
* (RGB) : 255:255:255
* (RGB) : 255,255,255
* (RGB) : 255.255.255
*
*/
public class BalleVZ extends java.applet.Applet implements Runnable
{
// On cree un Thread pour l'applet
Thread runner;
// Les coordonnees de la balle, et son vecteur deplacement
int x;
int y;
int depX;
int depY;
int initialX;
int initialY;
// L'unite de deplacement en pixels
int speed;
// Temps d'attente entre 2 plans de l'animation
int timeSleep;
// Couleur du fond et de la balle
Color bgColor;
Color fgColor;
// Grosseur de la balle
int ballSize;
// Taille de l'ecran
int width;
int height;
// Pour le double buffering (eviter les scintillements)
Image offscreenImg;
Graphics offscreenG;
// Pour la gestion des couleurs RGB
String rgbDelimiter = ":,.";
StringTokenizer st;
boolean threadSuspended = false;
public void aleatoir()
{
// Nombre entier aleatoire entre -3 et 3
depX = ((int)Math.floor(Math.random() * 7)) - 3;
depY = ((int)Math.floor(Math.random() * 7)) - 3;
if ((depX == 0) || (depY == 0)) aleatoir();
}
public void init()
{
// Recuperation des parametres de l'Applet
String s;
Integer intObj;
// Pour obtenir la taille de l'Applet en pixels.
try
{
intObj = new Integer(getParameter("width"));
width = intObj.intValue();
}
catch (Exception e)
{
width = 300;
}
try
{
intObj = new Integer(getParameter("height"));
height = intObj.intValue();
}
catch (Exception e)
{
height = 100;
}
try
{
intObj = new Integer(getParameter("ballSize"));
ballSize = intObj.intValue();
}
catch (Exception e)
{
ballSize = 20;
}
try
{
intObj = new Integer(getParameter("initialX"));
initialX = intObj.intValue();
}
catch (Exception e)
{
initialX = 0;
}
try
{
intObj = new Integer(getParameter("initialY"));
initialY = intObj.intValue();
}
catch (Exception e)
{
initialY = 0;
}
try
{
intObj = new Integer(getParameter("speed"));
speed = intObj.intValue();
}
catch (Exception e)
{
speed = 2;
}
try
{
intObj = new Integer(getParameter("timeSleep"));
timeSleep = intObj.intValue();
}
catch (Exception e)
{
timeSleep = 2;
}
s = getParameter("bgColor");
if (s != null) st = new StringTokenizer(s, rgbDelimiter);
if (s == null)
bgColor = Color.lightGray;
else if (s.equalsIgnoreCase("white"))
bgColor = Color.white;
else if (s.equalsIgnoreCase("black"))
bgColor = Color.black;
else if (s.equalsIgnoreCase("gray"))
bgColor = Color.gray;
else if (s.equalsIgnoreCase("darkGray"))
bgColor = Color.darkGray;
else if (s.equalsIgnoreCase("red"))
bgColor = Color.red;
else if (s.equalsIgnoreCase("green"))
bgColor = Color.green;
else if (s.equalsIgnoreCase("blue"))
bgColor = Color.blue;
else if (s.equalsIgnoreCase("yellow"))
bgColor = Color.yellow;
else if (s.equalsIgnoreCase("magenta"))
bgColor = Color.magenta;
else if (s.equalsIgnoreCase("cyan"))
bgColor = Color.cyan;
else if (s.equalsIgnoreCase("pink"))
bgColor = Color.pink;
else if (s.equalsIgnoreCase("orange"))
bgColor = Color.orange;
else if (st.countTokens() == 3) {
Integer r = new Integer(st.nextToken());
Integer g = new Integer(st.nextToken());
Integer b = new Integer(st.nextToken());
bgColor = new Color(r.intValue(), g.intValue(), b.intValue());
} else
bgColor = Color.lightGray;
s = getParameter("fgColor");
if (s != null) st = new StringTokenizer(s, rgbDelimiter);
if (s == null)
fgColor = Color.lightGray;
else if (s.equalsIgnoreCase("white"))
fgColor = Color.white;
else if (s.equalsIgnoreCase("black"))
fgColor = Color.black;
else if (s.equalsIgnoreCase("lightGray"))
fgColor = Color.lightGray;
else if (s.equalsIgnoreCase("gray"))
fgColor = Color.gray;
else if (s.equalsIgnoreCase("darkGray"))
fgColor = Color.darkGray;
else if (s.equalsIgnoreCase("red"))
fgColor = Color.red;
else if (s.equalsIgnoreCase("green"))
fgColor = Color.green;
else if (s.equalsIgnoreCase("blue"))
fgColor = Color.blue;
else if (s.equalsIgnoreCase("magenta"))
fgColor = Color.magenta;
else if (s.equalsIgnoreCase("cyan"))
fgColor = Color.cyan;
else if (s.equalsIgnoreCase("pink"))
fgColor = Color.pink;
else if (s.equalsIgnoreCase("orange"))
fgColor = Color.orange;
else if (st.countTokens() == 3) {
Integer r = new Integer(st.nextToken());
Integer g = new Integer(st.nextToken());
Integer b = new Integer(st.nextToken());
fgColor = new Color(r.intValue(), g.intValue(), b.intValue());
} else
fgColor = Color.yellow;
setBackground(bgColor);
offscreenImg = createImage(width, height);
offscreenG = offscreenImg.getGraphics();
x = initialX;
y = initialY;
aleatoir();
}
public void start()
{
if (runner == null);
{
runner = new Thread(this);
runner.start();
}
}
public void stop()
{
if (runner != null)
{
runner.stop();
runner = null;
}
}
public void run()
{
while (true)
{
if (x >= (width - ballSize) || x <= 0)
{ depX = -depX; x += depX * speed; }
else if (y >= (height - ballSize) || y <= 0)
{ depY = -depY; y += depY * speed; }
else { x += depX * speed; y += depY * speed; }
repaint();
try { Thread.sleep(timeSleep); }
catch (InterruptedException e) { }
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
offscreenG.setColor(bgColor);
offscreenG.fillRect(0, 0, width, height);
offscreenG.setColor(fgColor);
offscreenG.fillOval(x, y, ballSize, ballSize);
g.drawImage(offscreenImg, 0, 0, this);
}
public boolean mouseDown(Event e, int x, int y)
{
if (threadSuspended)
runner.resume();
else
runner.suspend();
threadSuspended = !threadSuspended;
return true;
}
public String getAppletInfo()
{
return "BalleVZ, april 1997, Vincent Zimmermann at NCTech, France";
}
}
--
[m[1;32m�� ��Դ:���������� BBS bbs.nease.net��[FROM: 202.96.174.34][m
Technical Details
Message-ID:
<3NdTSh$84h@bbs.nease.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f6cb0,d95b1a10f0ed095a,start X-Google-Attributes: gidf6cb0,public From: wooce.bbs@bbs.nease.net (wooce) Subject: ��ʾ��С���JAVAԴ���� Date: 1998/03/26 Message-ID: <3NdTSh$84h@bbs.nease.net> X-Deja-AN: 337776647 X-Filename: Java/M.890886763.A Organization: Net Ease Newsgroups: cn.bbs.comp.lang.java,