ana sayfa : araştırma : java uygulamaları : javada animasyon, imaj ve ses [ 10 / 12 ] |
|
GERİ | |
10
JAVADA ANİMASYONLAR İMAJLAR, VE SES
Animasyon
gerçekleştirme, java ortamında son derece kolaydır. Fakat standart Java
metodları ile sadece çizgiler, fontlar ve renkler üzerinde işlem yapılabilir.
Daha ilginç animasyonlar için
kullanıcı kendisine sesler ve görüntüler oluşturmalıdır. Basit imajlarda işlem
yapmak Java’da son derece kolaydır. Java awt içerisindeki ımaje sınıfı
genel imaj davranışları ile ilgili metodları içerir. Applet ve graphics
içinde tanımlanan özel metodlar ise kullanıcının appletine istediği
şeyleri kolayca yüklemesini sağlar. 10.1
İmaj Yerleştirme
Applette
imaj görmek için öncelikle bu imajın applete yüklenmesi gerekir. İmajlar
ayrı dosyalarda saklandığından programcı bunları nerede bulabileceğini
belirtmelidir. Applet sınıfı içindeki
getimage metodu imajı yükler ve imaj sınıfına ait bir örneği otomatik
olarak oluşturur. Bunu yapabilmek için Java.awtr.İmage sınıfı kullanılır.
Getİmage uniform kaynak konumlandırıcıya (URL) yüklenmek istenen imaj
aktarılır. Sonraki aşamalar içinse iki yol vardır. (Grup Java,2000) ·
Tek argümanlı getİmage matodu kullanılır. Argüman
URL tipimdeki bir nesnedir. ·
İki argümanlı getİmage() metodu kullanma.
Buradaki argümanlardan birisi taban URL, diğerid e
imajın bulunduğu dosya içindir. (tabana bağlı olacak şekilde) Birinci yol daha kolay görünmesine
karşılık ikinci yol daha esnektir. İkinci yol genelde tercih edilen ve
karşımıza çıkan şekildir. Applet sınıfı da taban URL argümanını
getİmage(); kullanımını da destekleyen iki metod içerir.(Grup
Java,2000) ·
getDocumentBase()
metodu appletin bulunduğu HTML dosyasını gösteren URL nesnesini içerir. Örneğin HTML dosyası http:// www.cypus.com./html/java.htm
ise getDocumentBase() burayı gösteren bir URL’ yi dönüş değeri
olarak verir. ·
getCodeBase()
metodu ise appleti içeren dizini gösteren stringi geri dönüş değeri
olarak verir. Bu HTML dosyasıyla aynı olabilir veya olmayabilir.
Bu tamamen appletet CODEBASE sıfatının kullanılıp kullanılmamasına
bağlıdır. Hangi metodun kullanılacağı
ise imajların html dosyalarına mı yoksa Java dosyalarına mı
adreslendiklerine göre saptanır. Her iki yol da url veya yolun get İmage
metodu içinde belirtilmesinden daha esnektir. Dikkat edilmesi gereken en
önemli nokta URL içinde meydana gelebilecek en ufak bir değişiklik
sonucu Java appletinin tekrar derlenmesi gerektiğidir. Eğer dosyamız kendisine
refere eden applet ile aynı dizindeyse aşağıdaki yapı kullanılmalıdır. İmage img= getİmage(getCodeBase(),”x.jpg”) Genelde çok sayıda imaj
mevcutsa bunlar ayrı bir alt dizinde saklanırlar. Bu durumda ise yine
getCodeBase kullanılır. İmage img= getİmage(getCodeBase(),”images/x.jgif”) Eğer getİmage() belirtilen dosyayı bulamazsa null
geri dönüş değeri verir. Bu durumda ekrana hiçbirşey çizilmez. Java
şu an için JPEG ve GIF formatlarını
desteklemektedir. animas.java import java.awt.*; import java.applet.*; import java.lang.*; public class tree extends Applet implements Runnable
{
Image tree;
Image screen;
boolean music = true;
Image snow;
FontMetrics fm;
Font myFont;
int fontsize;
AudioClip sound;
int msg = 0;
MediaTracker track;
Thread test;
String file = "tree.gif";
String message[] = new String[5]; int height;
int width;
int x[] = new int[30];
int y[] = new int[30];
int temp = 0;
double fly;
int l;
boolean backdrawn = false;
public void init() {
track = new MediaTracker(this);
tree = getImage(getDocumentBase(),file);
track.addImage(tree, 0);
snow = getImage(getDocumentBase(),"snow.gif");
track.addImage(snow,1);
sound = getAudioClip(getDocumentBase(),"sleigh.au");
setBackground(Color.black);
if (screen == null) {screen
= createImage(size().width,size().height);}
width = size().width;
height = size().height;
message[0] = getParameter("line1");
if (message[0] == null) { message[0] = "HAPPY HOLIDAYS";}
message[1] = getParameter("line2");
if (message[1] == null) { message[1] = "Enjoy LIFE";}
message[2] = getParameter("line3");
if (message[2] == null) { message[2] = "Eat, Drink and be Merry!";}
message[3]
= getParameter("line4");
if (message[3] == null) { message[3] =
"http://www.robogames.com";}
message[4] = getParameter("line5");
if (message[4] == null) { message[4] = "The Most Fun
Anywhere!!!";}
myFont = new Font("Arial",Font.BOLD,10);
fm = getFontMetrics(myFont);
}
public synchronized void start() {
if (test == null) {
test = new Thread(this);
test.start();
}
}
public synchronized void stop() {
if (test != null) {
sound.stop();
test.stop();
test = null;
}
}
public void run() {
try track.waitForAll();
catch (InterruptedException e);
while(true) {
if (!backdrawn) { background(); }
if (backdrawn) {snow(); }
update();
try Thread.sleep(1);
catch (InterruptedException e);
}
}
public void update() {
Graphics g = screen.getGraphics();
g.setColor(Color.black);
g.fillRect(0,0,width,height);
g.setColor(Color.yellow);
g.drawImage(tree,(width/2)-35,0,this);
fontsize = fm.stringWidth(message[msg]);
g.setFont(myFont);
g.drawString(message[msg],((width/2)-(fontsize/2)),height-5);
for (l=0;l<30;l++) {
g.drawImage(snow,x[l],y[l],this);
}
g = getGraphics();
g.drawImage(screen,0,0,this);
g.dispose();
}
public void snow() {
temp++;
for (l=0;l<30;l++) {
y[l]+=9;
if (y[l]>100) { y[l]=0;}
}
if (temp>5) {msg++;
if (msg>3) {sound.stop();}
if (msg>4) {msg = 0; }
temp = 0;
}
}
public void background() {
for (l=0;l<30;l++) {
x[l] =(int)(Math.random()*100)+20;
y[l] =(int)(Math.random()*100);
}
if (music) {sound.loop();}
Graphics g = screen.getGraphics();
g.setColor(Color.black);
g.fillRect(0,0,width,height);
g.drawImage(tree,0,0,this);
backdrawn = true;
update();
}
public boolean mouseDown(Event e, int ms, int my) {
if (!music) { music = true; sound.loop(); return true; }
if (music) {music = false; sound.stop(); return true;}
return true;
} }
animas.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
3.2//EN"> <HTML> <HEAD> <TITLE>Merry x-mas-God Jul-</TITLE> </HEAD> <BODY BACKGROUND=""
BGCOLOR="#000000" TEXT="#d20000"
LINK="#d20000" VLINK="#d20000"
ALINK="#d20000"> <BR> <BR> <FONT
SIZE="+2"><CENTER><B><I>For your christmas
page:) tree java in applet15.zip as
christ.zip</I></B></CENTER></FONT> <BR> <BR> <CENTER> <APPLET code = "tree.class" height = 150
width = 150> <PARAM NAME = line1 VALUE = "Merry
Christmas"> <PARAM NAME = line2 VALUE = "From"> <PARAM NAME = line3 VALUE = "GateWay
III"> <PARAM NAME = line4 VALUE =
"Hohohoho"> <PARAM NAME = line5 VALUE =
":------)"> </APPLET> </CENTER> <BR> <BR> <FONT
SIZE="+2"><CENTER><B><I><A
HREF="cafe6.htm">Ho Ho Back To
Cafe8</A></I></B></CENTER></FONT> <BR> <BR> </BODY> </HTML> 10.2
İmajları Ekrana Bastırma
Getİmage()
sadece imajı çağırıp İmage sınıfına ait bir örnek değişkenine
atar. İmajın ekrana bastırılması için Graphics sınıfına ait iki
metoddan birisi kullanılır. Her iki metodda drawİmage() kullanılır. (Grup Java,2000) Birinci
metodda drawİmage dört argüman kullanır. Bunlar bastırılacak imaj,
baskının yapılacağı ekran bölgesinin sol üst köşesine ait x ve y
koordinatları ve this şeklindedir. Public
void paint() { g.drawİmage(img,
10,10,this); } examples.java import java.awt.*; import java.applet.Applet; class CompImage extends Canvas implements Runnable {
private Image
offscreenImage, imgA, imgB;
private int
width, height;
private int
widthA, heightA, widthB, heightB;
private int
xA, yA, xB, yB;
private int
dxA = 1, dyA = 1, dxB = -2, dyB = -2;
private Graphics
offscr;
private Thread
ticker;
private boolean
running = false;
public CompImage (int x, int y, int width, int height, Applet a) {
super();
this.width = width;
this.height = height;
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(imgA = a.getImage(a.getCodeBase(),
"note.gif"), 0);
tracker.addImage(imgB = a.getImage(a.getCodeBase(),
"coffee.gif"), 0);
try { tracker.waitForAll(); } catch (InterruptedException e) { ; }
widthA = imgA.getWidth(this);
heightA = imgA.getHeight(this);
widthB = imgB.getWidth(this);
heightB = imgB.getHeight(this);
xB = (xA = width >> 1) >> 1;
yB = (yA = height >> 1) >> 1; set size
a.add(this);
reshape(x, y, width, height);
offscreenImage = createImage(width, height);
offscr = offscreenImage.getGraphics();
startAnim();
}
public synchronized void startAnim () {
if (ticker == null ||
!ticker.isAlive()) {
running = true;
ticker = new Thread(this, "ticker");
ticker.setPriority(Thread.MIN_PRIORITY + 1);
ticker.start();
}
}
public synchronized void stopAnim () {
running = false;
}
public void run () {
try { ticker.sleep(200); } catch (InterruptedException e) { ; }
while (running) {
synchronized (this) {
xA += dxA; yA += dyA;
xB += dxB; yB += dyB;
if (xA < 0 || xA >= (width -
widthA )) xA += (dxA = dxA);
if (yA < 0 || yA >= (height - heightA))
yA += (dyA = dyA);
if (xB < 0 || xB >= (width -
widthB )) xB += (dxB = -dxB);
if (yB < 0 || yB >= (height - heightB))
yB += (dyB = -dyB);
}
repaint();
try { ticker.sleep(40); } catch (InterruptedException e) { ; }
}
}
public void paint (Graphics g) {
if (offscr != null) {
int x2 = width >> 1;
int y2 = height >> 1;
offscr.setColor(Color.gray);
offscr.fillRect(0, 0, x2, y2);
offscr.fillRect(x2, y2, width - x2, height - y2);
offscr.setColor(Color.white);
offscr.fillRect(x2, 0, width - x2, height - y2);
offscr.fillRect(0, y2, x2, y2);
offscr.drawImage(imgA, xA, yA, this);
offscr.drawImage(imgB, xB, yB, this);
g.drawImage(offscreenImage, 0, 0, this);
}
}
public void update (Graphics g) {
paint(g);
} } public class Example extends Applet {
private CompImage
comp;
public void init() {
super.init();
setLayout(null);
comp = new CompImage(0, 0, size().width, size().height, this);
}
public void start () {
if (comp != null)
comp.startAnim();
}
public void stop () {
if (comp != null)
comp.stopAnim();
} } example.htm <html> <head> <meta
http-equiv="Content-Type" content="text/html;
charset=windows-1254"> <title>New
Page 8</title> <meta
name="GENERATOR" content="Microsoft FrontPage 3.0"> </head> <body> <p> <applet
CODE="Example.class" WIDTH="150" HEIGHT="120"
ALIGN="bottom"> </applet> </p> <p><font
color="#000000"
size="1"><</font><font
color="#800080" size="1">applet </font><font color="#ff0000"
size="1">CODE="</font><font
color="#0000ff"
size="1">Example.class</font><font color="#ff0000"
size="1">" WIDTH="</font><font
color="#0000ff" size="1">150</font><font color="#ff0000"
size="1">" HEIGHT="</font><font
color="#0000ff" size="1">120</font><font color="#ff0000"
size="1">" ALIGN="</font><font
color="#0000ff" size="1">bottom</font><font color="#ff0000"
size="1">"</font><font
color="#000000"
size="1">></font></p> <p><font
color="#000000" size="1"> </p> <p><</font><font
color="#800080" size="1">/applet</font><font
color="#000000"
size="1">></font></p> </body> </html>
Şekil 10.2 Example.html Yukarıdaki
örnek fare ile fincanın yer değiştirmesinden oluşan bir
animasyon appletidir. 10.3
Ses
Windows işletim sisteminin
standart ses dosyaları .WAV cinsinden olan dosyalardır., fakat JAVA bu tür
dosyaları desteklemez. JAVA’nın desteklediği ses dosyaları .AU türünden
olup 8 bit mono ses içerirler. Ses kaliteleri çok iyi değildir. Buna karşılık
boyutları ufak olduğundan İnternet bilgi iletişiminde fazla bir zaman
kaybına neden olmazlar. İnternet üzerinden bulunabilecek çeşitli audio
conversion programları ile .WAV dosyaları .AU dosyalarına da çevirebilir.
kedi.html <HTML> <HEAD> <TITLE>Java
mascot</TITLE> </HEAD> <BODY
BGCOLOR="#C0C0C0"> <H2><I><IMG
SRC="java.gif" ALT="java!"
ALIGN=LEFT></H2></I> <!HR
SIZE=64><TABLE BORDER CELLSPACING=32><TR><TD
WIDTH=500></TR></TD></TABLE> <HR> <H1><FONT
COLOR=#A000A0>J</FONT><FONT
COLOR=#C00000>a</FONT><FONT
COLOR=#A0A000>v</FONT><FONT COLOR=#00A000>a</FONT>
mascot</H1> <CENTER> <APPLET
codebase="classes/" code="mascot.class" width=350
height=120> <param
name=directory
value="classes/"> <param
name=nimgs
value="6"> <!param
name=background_color
value="FFC0C0"> <param
name=background_image
value="bkground.gif"> <param
name=click_sound
value="click_sound.au"> <param
name=bgm
value="bgm.au"> <H2>Java
applet is here. <BR> Please
use Java enabled browser.</H2> </APPLET> </CENTER> <BR> <B>If
you upload this applet and prepare some images, your designed mascot will
walk on your web page!</B><BR><BR> </UL> <HR> <BR> <B>How
to install and setup.</B> <OL>
<LI>Prepare still images<BR>
<UL>
<LI>All images must be same width and same height.
<LI>All images must be GIF format. Background transparent is
better.
<LI>Prepare 8 moving image files and 2 stop image files and 1
clicked image file.
<LI>2 upper moving files must be named "imove1.gif"
and "imove2.gif".
<LI>2 right moving files must be named "imove3.gif"
and "imove4.gif".
<LI>2 lower moving files must be named "imove5.gif"
and "imove6.gif".
<LI>2 left moving files must be named "imove7.gif"
and "imove8.gif".
<LI>2 stop image files must be named "istop1.gif" and
"istop2.gif".
<LI>1 clicked image file must be named "iclick1.gif".
</UL>
<LI>Write your home page's source (include applet tag).
<LI>Put "mascot.class" to same directory as html
file.
<LI>Put image files to same directory as html file. </OL> <CENTER> <CAPTION>Image
data of this page's mascot</CAPTION> <TABLE
BORDER> <TR><TD>imove1.gif</TD><TD>imove2.gif</TD><TD>imove3.gif</TD><TD>imove4.gif</TD><TD>imove5.gif</TD><TD>imove6.gif</TD></TR> <TR><TD><IMG
SRC="classes/imove1.gif"></TD><TD><IMG
SRC="classes/imove2.gif"></TD><TD><IMG
SRC="classes/imove3.gif"></TD><TD><IMG
SRC="classes/imove4.gif"></TD><TD><IMG
SRC="classes/imove5.gif"></TD><TD><IMG
SRC="classes/imove6.gif"></TD></TR> <TR><TD>imove7.gif</TD><TD>imove8.gif</TD><TD>istop1.gif</TD><TD>istop2.gif</TD><TD>iclick1.gif</TD><TD></TD></TR> <TR><TD><IMG
SRC="classes/imove7.gif"></TD><TD><IMG
SRC="classes/imove8.gif"></TD><TD><IMG
SRC="classes/istop1.gif"></TD><TD><IMG
SRC="classes/istop2.gif"></TD><TD><IMG
SRC="classes/iclick1.gif"></TD><TD></TD></TR> </TABLE> </CENTER> <BR> <B>Explanation
of param tag.</B><BR> <UL>
<LI><B>PARAM NAME=directory</B>
<LI><B>PARAM NAME=speed</B>
<LI><B>PARAM NAME=nimgs</B> be "2".>
<LI><B>PARAM NAME=background_color</B>
<LI><B>PARAM NAME=click_sound</B>
<LI><B>PARAM NAME=bgm</B>
</UL> <B>Sample
applet tag.</B><BR><BR> <BLOCKQUOTE> <TT>
<APPLET code="mascot.class" width=450
height=120><BR>
<PARAM NAME=speed
VALUE="4"><BR>
<PARAM NAME=nimgs
VALUE="2"><BR>
<PARAM NAME=background_image
VALUE="bkground.gif"><BR>
<PARAM NAME=click_sound
VALUE="click_sound.au"><BR>
<PARAM NAME=bgm
VALUE="bgm.au"><BR>
</APPLET></TT><BR><BR> </BLOCKQUOTE> <B>Another
information</B><BR><UL>
<LI>If you have some troubles about Java mascot, please see
<A HREF="mascot.java">source</A>.<BR>
<LI> this applet works under httpd, it loads images fast.
</UL> <HR> <B><BLINK><FONT
COLOR=#FF0000> uploaded</FONT></BLINK>
<A HREF="http://www.asahi-net.or.jp/~FX6M-FJMY/">
here</A>.</B> <HR> </BODY> <ADDRESS><B>Java
mascot</B> Copyright(C) 1996. M.Fujimiya. Please e-mail <A
HREF="mailto:GEA03266@niftyserve.or.jp">GEA03266@niftyserve.or.jp</A>
with any comments you might have.</ADDRESS> </HTML>
Şekil 10.3 Kedi.html kedi.java import
java.applet.*; import
java.awt.*; import
java.net.*; public
class mascot extends java.applet.Applet implements Runnable {
Image
imgs[],imgb[];
// Image data
int
nimgs,nstate;
// Number of image, Number of status
int
timeout = 200;
// Wait time
Thread myThread;
// theread
String
dir;
// Image data directory
int
sWidth,sHeight;
// contnet size
int
cWidth,cHeight;
// character size
int
bgWidth,bgHeight;
int
bgX,bgY;
int
bWidth=0,bHeight=0; // button
size
int
catx,caty;
// character address
int
status = 4;
// character status
int
click = 0;
// character status count
int
dt;
// delta_t
int
mousex,mousey;
// mouse address
boolean
isClick = false; // mouse
clicked
boolean
isMouseIn = false; // active applet or ont
boolean
isInitial = true; //
initial or not
boolean
isBackImage = false;
Color
bkColor;
MediaTracker tracker;
Image
offs;
// Offscreen image
Graphics grf;
// Offscreen graph port
URL
docURL;
String
bStr = "Go Home";
String
myURL =
"http://www.asahi-net.or.jp/~FX6M-FJMY/java00e.html";
AudioClip clickSound,bgm;
public void init() {
int
i,w,h,bcol,red,green,blue;
String at,bgf;
// Get parameters
at = getParameter("directory");
dir = (at != null) ? at : "./";
at = getParameter("speed");
timeout = 1000 / ((at == null) ? 4 : Integer.valueOf(at).intValue());
at = getParameter("delta_t");
dt = (at == null) ? 16 : Integer.valueOf(at).intValue();
at = getParameter("nimgs");
nimgs = (at == null) ? 2 : Integer.valueOf(at).intValue();
nstate = nimgs / 2 + 4;
at = getParameter("background_color");
if (at!=null) {
bcol = Integer.valueOf(at,16).intValue();
red = bcol/256/256;
green = (bcol-red*256*256)/256;
blue = bcol-red*256*256-green*256;
bkColor = new java.awt.Color(red,green,blue);
setBackground(bkColor);
} else {
bkColor = new java.awt.Color(64*3,64*3,64*3);
}
bgf = getParameter("background_image");
if (bgf != null) isBackImage = true;
at = getParameter("click_sound");
clickSound = null;
if (at != null) {
clickSound = getAudioClip(getDocumentBase(), dir + "/" +
at);
}
at = getParameter("bgm");
bgm = null;
if (at != null) {
bgm = getAudioClip(getDocumentBase(), dir + "/" + at);
}
// Load Images
tracker = new MediaTracker(this);
imgs = new Image[nimgs+9];
for ( i = 0 ; i < 8 ; i++ ) {
imgs[i] = getImage(getDocumentBase(), dir + "/imove" +
(i+1) + ".gif");
tracker.addImage(imgs[i], 0);
}
for ( i = 8 ; i < 8+nimgs ; i++ ) {
imgs[i] = getImage(getDocumentBase(), dir + "/istop" +
(i-7) + ".gif");
tracker.addImage(imgs[i], 0);
}
imgs[8+nimgs] = getImage(getDocumentBase(), dir +
"/iclick1" + ".gif");
tracker.addImage(imgs[8+nimgs], 0);
if (isBackImage) {
imgb = new Image[1];
imgb[0] = getImage(getDocumentBase(), dir + "/" + bgf);
tracker.addImage(imgb[0], 0);
}
// Off screen bitmap image
offs = createImage(size().width, size().height);
grf
= offs.getGraphics();
// Initial value set
cWidth = cHeight = sWidth = sHeight = 10;
// dummy value set
try {docURL = new URL(myURL);} catch (MalformedURLException e)
{docURL = null;}
}
public void start() {
if (myThread == null) {
myThread = new Thread(this);
myThread.start();
}
}
public void stop() {
if (myThread != null) {
myThread.stop();
myThread = null;
if (bgm != null) bgm.stop();
}
}
public void run() {
while (true) {
try {Thread.currentThread().sleep(timeout);} catch
(InterruptedException e){}
repaint();
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
int
i,j;
// Check image files are loaded
if (isInitial) {
isInitial = false;
g.setColor(Color.yellow);
g.fillRect(0, 0, size().width-1, size().height-1);
getAppletContext().showStatus("Loading now. Please wait!");
g.setColor(Color.black);
g.drawString("Loading now. Please wait!", 5, 25);
tracker.checkID(0,true);
try { tracker.waitForID(0); } catch (InterruptedException e)
{return;}
g.clearRect(0, 0, size().width, size().height);
getAppletContext().showStatus("Java mascot running");
// Initial value set
cWidth =
imgs[0].getWidth(this);
cHeight = imgs[0].getHeight(this);
sWidth = size().width -
2 - cWidth;
sHeight = size().height - 2 - cHeight;
catx = sWidth / 2 ;
caty = sHeight / 2 ;
if (isBackImage) {
bgWidth =
imgb[0].getWidth(this);
bgHeight = imgb[0].getHeight(this);
bgX = size().width/bgWidth + 1;
bgY = size().height/bgHeight + 1;
}
return;
}
}
grf.setColor(bkColor);
grf.fillRect(0, 0, size().width, size().height);
if (isBackImage) {
for (i=0 ; i<bgX ; i++) {
for (j=0 ; j<bgY ; j++) {
grf.drawImage(imgb[0], i*bgWidth, j*bgHeight, this);
}
}
}
// Change icon status and move icon
click++;
if ( click > 1 ) click = 0;
switch (status) {
case 0:
caty -= dt;
if ( caty < 1 ) {
caty = 1;
status = 4;
}
break;
case 1:
catx += dt;
if(catx > sWidth ) {
catx = sWidth;
status = 4;
}
break;
case 2:
caty += dt;
if(caty > sHeight) {
caty = sHeight;
status = 4;
}
break;
case 3:
catx -= dt;
if (catx < 1) {
catx = 1;
status = 4;
}
break;
}
i = status * 2 + click;
if (isClick) {
i = 8 + nimgs;
isClick = false;
}
// Draw icon
grf.drawImage(imgs[i], catx, caty, this);
DrawButton(grf);
grf.setColor(bkColor);
grf.draw3DRect(0, 0, size().width-1, size().height-1, true);
g.drawImage(offs, 0, 0, this);
// chase mouse cursor
if (isMouseIn) {
int dx,dy;
dx = catx + cWidth/2 - mousex;
dy = caty + cHeight/2 - mousey;
if ( Math.abs(dx) + Math.abs(dy) > dt*3 ) {
if( Math.abs(dx) > Math.abs(dy) ) {
if ( dx > 0 ) {
status = 3;
} else {
status = 1;
}
} else {
if ( dy > 0 ) {
status = 0;
} else {
status = 2;
}
}
} else {
if (Math.random() > 0.8 ) {
status = (int)(Math.random()*nimgs/2)+4;
}
}
} else {
// Change status
if (Math.random() > 0.8 ) {
status = (int)(Math.random()*nstate);
}
}
}
public void DrawButton(Graphics g) {
FontMetrics fm = g.getFontMetrics();
bWidth =
fm.stringWidth(bStr);
bHeight = fm.getHeight();
//
g.clearRect(5, sHeight+cHeight-5-bHeight, bWidth, bHeight+1);
g.setColor(Color.blue);
g.drawString(bStr, 5, sHeight+cHeight-5);
g.drawLine(5, sHeight+cHeight-4, 5+bWidth, sHeight+cHeight-4);
}
public boolean mouseUp(java.awt.Event evt, int jx, int jy) {
if (isInsideString(jx, jy)) {
getAppletContext().showDocument(docURL);
}
if
((jx>catx)&&(jy>caty)&&(jx<catx+cWidth)&&(jy<caty+cHeight)){
isClick = true;
if (clickSound != null) clickSound.play();
}
return(true);
}
public boolean mouseEnter(java.awt.Event evt, int jx, int jy) {
mousex = jx; mousey = jy;
isMouseIn = true;
if (bgm != null) bgm.loop();
return(true);
}
public boolean mouseExit(java.awt.Event evt, int jx, int jy) {
isMouseIn = false;
getAppletContext().showStatus("");
if (bgm != null) bgm.stop();
return(true);
}
public boolean mouseMove(java.awt.Event evt, int jx, int jy) {
mousex = jx; mousey = jy;
if (isInsideString(jx, jy)) {
getAppletContext().showStatus(myURL);
} else {
getAppletContext().showStatus("Java mascot running");
}
return(true);
}
public boolean isInsideString(int jx, int jy) {
if
((jx>5)&&(jx<5+bWidth)&&(jy>sHeight+cHeight-5-bHeight)&&(jy<sHeight+cHeight-4))
{
return(true);
}
return(false);
}
}
|