bessamira
Messages postés9Date d'inscriptionsamedi 22 décembre 2007StatutMembreDernière intervention 1 septembre 2008
-
22 juil. 2008 à 14:07
Twinuts
Messages postés5375Date d'inscriptiondimanche 4 mai 2003StatutModérateurDernière intervention14 juin 2023
-
29 juil. 2008 à 09:14
Bonjour a tout le monde,
j'ai fait une applet java d'un ticker j'ecrit le code mais toujours ne marche plus,je travaille sous eclipse,mon code est le suivant quand je fait le run je trouve des erreurs
s'il vous plait est ce quelqu'un peut m'aider et merci d'avance.
/************************************************************/
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.Event;
import java.awt.Cursor;
// pour LiveConnect...
//import netscape.javascript.JSObject;
public class aTicker extends java.applet.Applet implements Runnable
{
// Declare the controlling thread for the applet
public Vector dFile = new Vector( 5, 5 );
// Declare the details for the message to be displayed
int s_h ; // Longueur du message
int iMax ; // Nombre de Message
int iActif = 999; // Message Actif
int iCur = -1 ;
int cBk = 0x000000 ; // Nombre de Message
// Variables controlling the display of the message
int x_coord, // present x-position
y_coord, // present y-position
speed, // scrolling amount
iPas, // (integer) pas entre 2 messages
delay; // scrolling speed
// for storing the dimensions of the applet
int aWidth, aHeight;
// for double buffering to prevent flicker
Image offScreenImage;
Graphics offScreen;
// Font control variables
Font wFont1; // Message
Font wFont2; // Message Actif
int cT1 = 0xFFFFFF ; // Message
int cT2 = 0xFFFF00 ; // Message Actif
FontMetrics wM;
// Applet initialisation routine
public void init() {
// Temporary storage space
String temp;
// get the speed of the scrolling
temp = getParameter("speed");
speed= (temp==null) ? 1 : Integer.parseInt( temp );
// fichier
temp = getParameter("file");
if (temp==null)
temp="mess.txt" ;
wM = getFontMetrics (wFont1);
readFile(temp) ;
} // end of init
public void setFont(int z, String s) {
int j = 0 ;
int k = 0 ;
int i ;
String sF = "" ;
int iS = 10 ;
int iT = 1 ;
int iC = 0xFFFFFF ;
String p ;
while (true) {
i = s.indexOf(',', j) ;
if (i > 0)
p = new String(s.substring(j, i).trim()) ;
else
p = new String(s.substring(j).trim()) ;
if ((i>-1) || (p.length() > 0)) { if ( k 0) sF new String(p) ; if ( k 1) iS Integer.parseInt(p) ; if ( k 2) iT Integer.parseInt(p) ; if ( k 3) iC Integer.parseInt(p) ;
k++ ;
}
if (i == -1) break ;
else j = i + 1 ;
}
Font f = new Font(sF, iT, iS) ; if (f null) f getFont() ;
if (z == 1) {
wFont1 = f ;
cT1 = iC ;
}
else {
wFont2 = f ;
cT2 = iC ;
}
}
public void readFile(String f) {
dFile.removeAllElements() ;
iMax = 0;
// open stream to a file which name is expressed relative to the document URL
DataInputStream fis = null ;
if (bLoc == false)
try {
fis = new DataInputStream( (new URL( getCodeBase(), f )).openStream() ) ;
} catch( Exception e ) {
fis = null ;
bLoc = true ;
}
rp s = null ;
String sS ;
int iL = 0 ;
// parser loop
while ( true ) {
s = new rp();
if (bLoc) {
sS = getParameter(f + iL) ;
iL++ ;
}
else
try {
sS = fis.readLine() ;
} catch( Exception e ) {
break ;
}
if (s.get( sS, cSep, "_new", 0xFFFFFF, 0xFFFF00 )) {
if (s.sM != null ) {
dFile.addElement( s );
iMax++ ;
}
}
else
break ;
}
// control the starting of the applet
public void start() {
// start the thread
scrollmessage = new Thread(this);
scrollmessage.start();
} // end of start
// control the stopping of the applet
public void stop() {
scrollmessage.stop();
} // end of stop
// control the running of the applet
public void run() {
// set the priority of the thread to low
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// loop continuously
while(true) {
// values for working out the constant delay
long thisTick, waitTick;
// calculate the tick to wait for
waitTick = System.currentTimeMillis() + delay;
// update the screen
x_coord -= speed;
if (x_coord<s_h) {
iActif++; // Load next line
x_coord += -s_h + iPas; if (iActif >iMax) iActif 0 ;
public boolean mouseMove(Event e, int x, int y){
int i = iActif ;
int l = x_coord ;
int lt ;
int iZ = -1 ;
while (true) {
lt = wM.stringWidth( ((rp)dFile.elementAt(i)).sM ) ;
if ((x > l) && (x < (l+lt))) {
iZ = i ;
break ;
}
else {
l = l + iPas + lt ;
i++ ; if (i >iMax) i 0 ;
}
if (l > aWidth)
break ;
}
if (iZ != iCur) {
if (iZ == -1)
this.setCursor(new Cursor(0));
else
this.setCursor(new Cursor(12));
iCur = iZ ;
if (iCur == -1) {
showStatus("") ;
}
else {
if (((rp)dFile.elementAt(iCur)).sH == null) {
if (((rp)dFile.elementAt(iCur)).sU != null)
showStatus(((rp)dFile.elementAt(iCur)).sU) ;
}
else
showStatus(((rp)dFile.elementAt(iCur)).sH) ;
}
repaint() ;
}
return true ;
}
public boolean mouseDown(Event e, int x, int y){
if (iCur == -1) return true ;
if ( ((rp)dFile.elementAt(iCur)).sU != null) {
URL clickDest ;
try{
if (((rp)dFile.elementAt(iCur)).sT.equals("_script")){
JSObject.getWindow (this).eval (((rp)dFile.elementAt(iCur)).sU );
}
else {
if ( ((rp)dFile.elementAt(iCur)).sU.charAt(0) == '.')
clickDest = new URL(getCodeBase(), ((rp)dFile.elementAt(iCur)).sU );
else
clickDest = new URL(((rp)dFile.elementAt(iCur)).sU);
getAppletContext().showDocument(clickDest, ((rp)dFile.elementAt(iCur)).sT );
}
public boolean mouseEnter(Event e, int x, int y){
try {
scrollmessage.suspend() ;
} catch (Exception e2) {}
// showStatus(((rd2)dFile.elementAt(iActif)).sU);
return(true);
}
public boolean mouseExit(Event e, int x, int y){
try {
scrollmessage.resume() ;
} catch (Exception e2) {}
iCur = -1 ;
showStatus("");
return(true);
}
// Called when the applet needs to be painted
// calls the flicker free updating system
public void paint (Graphics g) {
update(g);
} // end of paint
// Draw the applet without flicker
public synchronized void update(Graphics g) {
// get the size of the applet
int aW = size().width ;
int aH = size().height ;
l += iPas + wM.stringWidth( ((rp)dFile.elementAt(i)).sM ) ;
i++ ; if (i >iMax) i 0 ;
if (l > aWidth)
break ;
}
} // end of paintApplet
}
// end aTicker
class rp extends Object {
public String sU = null ; // dest URL
public String sM = null ; // message
public String sT = null ; // target
public String sH = null ; // Help
public int l1 ; // Color std
public int l2 ; // Color active
public int lg = -1 ;
public boolean get( String st, char cSep, String starg, int d1, int d2 ) {
int i = 0 ;
int j = 0 ;
int k = 0 ;
String p ;
if (st == null) return false ;
sM = null ;
sU = "" ;
sT = starg ;
sH = null ;
l1 = d1 ;
l2 = d2 ;
lg = -1 ;
while (true) {
i = st.indexOf(cSep, j) ;
if (i > 0)
p = new String(st.substring(j, i).trim()) ;
else
p = new String(st.substring(j).trim()) ;
if ((i>-1) || (p.length() > 0)) { if ( k 0) sM new String(p) ; if ( k 1) sU new String(p) ;
if (p.length() > 0) {
if (p.charAt(0) =='?')
sH = p.substring(1) ;
else { if ( k 2) sT new String(p) ; if ( k 3) l1 Integer.parseInt(p) ; if ( k 4) l2 Integer.parseInt(p) ;
}
}
k++ ;
}
if (i == -1) break ;
else j = i + 1 ;
}
if (sU.equals("--")) sU = null ;
return ( k > 0 ) ;
}
bessamira
Messages postés9Date d'inscriptionsamedi 22 décembre 2007StatutMembreDernière intervention 1 septembre 2008 23 juil. 2008 à 10:07
bonjour Laurent,
merci pour ta réponse bon les erreurs sont les suivants:
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at aTicker.readFile(aTicker.java:207) c'est a dire au niveau" sMess = ((rp)dFile.elementAt(iActif)).sM ;"
et
at aTicker.init(aTicker.java:109)c'est a dire au niveau de la ligne" readFile(temp) ;"
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
et merci pour votre aide.
cs_laurent1024
Messages postés987Date d'inscriptionmardi 31 mai 2005StatutMembreDernière intervention30 août 201225 23 juil. 2008 à 10:29
Cela veux dire que tu veux acceder à la case 0 d'un vecteur qui n'a pas d'éléments. Avant de faire un elementAt(), il faut vérifier la taille de ton vecteur. Tu peux également vérifier ta fonction qui remplit le vecteur
try {
sS = fis.readLine() ;
} catch( Exception e ) {
break ; ==> s'il y a une exception ici, ton vecteur peut ne pas etre remplit, tu arrive ensuite sur la ligne qui te provoque l'erreur,
}
Twinuts
Messages postés5375Date d'inscriptiondimanche 4 mai 2003StatutModérateurDernière intervention14 juin 2023111 23 juil. 2008 à 12:45
Salut,
merci de ne valider que les réponse qui résolvent le problème, et non TOUTES les réponse que l'on te fait... pour rappel si tu flags un message comme étant valide et résolvant ton problème tu auras beaucoup moins de gens qui vont se pencher sur ton problème ...
------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."