Chargement et affichage d'un image
Pour charger une image d'abord il faut creer un objet de la classe Image. Pour ca on a besoin de deux location:
L'affichage de l'image se fait par la methode drawImage d'un objet Graphics:
la classe component "implemets" l'interface ImageObserver.
Dans le cas d'execution d'un applet c'est l'apllet qui joue la
role
d'ImageObserver, donc on doit utiliser la reference this.
Avant JDK 1.2, applets pouvaient jouer seulement des fichiers audio
en Sun audio format (.au files). JDK 1.2
a introduit une nouvelle engine son qui support des formats
suivants:
MIDI (type 0 and type 1)--The Musical Instrument Digital Interface, a
digital format for musical instruments.
RMF--The Rich Music Format, an audio file format created by
Headspace, Inc. for online playback through the Beatnik plug-in.
WAVE--The Microsoft Windows audio file format.
AIFF--The Audio Interchange File Format, typically used with Macintosh
and Silicon Graphics computers.
AU--The Sun audio file format.
L'Interface AudioClip permet de représenter des sons. On peut créer une instance de cette classe en téléchargeant un fichier son. Cette opération est réalisée par la méthode getAudioClip (dans la classe Applet) qui peut prend deux formes :
Dans la première forme on donne en paramètres une URL complète, dans la seconde on donne une URL de base et un chemin relatif sous forme de chaîne de caractères. Si le fichier son se trouve dans le même dossier que l'applet on créera un objet son de la classe AudioClip en écrivant :
son=getAudioClip(getCodeBase(),"monSon.au");
Pour faire entendre le son associé à un objet de type AudioClip on dipose des méthodes suivantes :
import java.applet.*;
import java.awt.event.*; import java.awt.*; public class ImgScBarD extends Applet{
ding=getAudioClip(getCodeBase(),"ding.au"); scrollbar = new Scrollbar(Scrollbar.HORIZONTAL,50, 20, 1, 100); //parametres:
orientation,
initValue, sizeBubble, minVal,maxVal
scrollbar.addAdjustmentListener(new
SB());
scrollbarV.addAdjustmentListener(new
SBV());
} class SB
implements
AdjustmentListener {
class SBV
implements
AdjustmentListener {
class SpPlay
implements ActionListener {
|
|
import java.applet.*; import java.awt.event.*; import java.awt.*; public class CardEx extends Applet{ Image Card[] = new Image[5]; Button ply,stp; AudioClip ding,fond; int wh=70,ht=100; int vX[]=new int[Card.length]; int vY[]=new int[Card.length]; boolean back[] = new boolean[Card.length]; boolean ctch[] = new boolean[Card.length]; int helpX,helpY,clicks; public void init(){ setBackground(Color.green); for(int i=0; i<Card.length;i++){ Card[i]=getImage(getCodeBase(), "images/C"+i+".jpg"); vX[i]=20+55*i; vY[i]=30; back[i]=ctch[i]=false; } addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); } class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e){ helpX=e.getX(); helpY=e.getY(); for(int i=0;i<Card.length;i++){ if(chckIn(i,helpX,helpY)){ ctch[i]=true; if(e.getClickCount()==2){ if(back[i]) Card[i]=getImage(getCodeBase(), "images/C"+i+".jpg"); else Card[i]=getImage(getCodeBase(), "images/Cback.jpg"); back[i]=!back[i]; repaint(vX[i],vY[i],wh,ht); } } } clicks=e.getClickCount(); repaint(); } public void mouseReleased(MouseEvent e){ for(int i=0;i<Card.length;i++) ctch[i]=false; repaint(); } } |
class MouseMotionHandler extends MouseMotionAdapter { public void mouseDragged(MouseEvent e){ int x = e.getX(); int y = e.getY(); for(int i=0;i<Card.length;i++) if(ctch[i]) { vX[i]+=(x-helpX); vY[i]+=(y-helpY); repaint(vX[i],vY[i],wh,ht); helpX=x; helpY=y; } } } public void paint(Graphics g) { for(int i=0;i<Card.length;i++){ g.drawImage(Card[i], vX[i], vY[i], wh,ht,this); g.drawRect(vX[i], vY[i], wh,ht); } } boolean chckIn(int ncard, int x, int y){ if( (x>vX[ncard])&& (x< vX[ncard]+wh) && (y> vY[ncard])&& (y< vY[ncard]+ht) ) return true; return false; } } |
import java.applet.*; |
|