Seconda parte
/**
* Metodo che si occupa di calcolare un numero che specifica se ci sono degli sfondi speciali o meno
* @return -il numero rappresentante lo sfondo
*/
protected int getSpecial(){
temp = getParameter("background");
if(temp == null){
error = "error: background value=\"" + temp + "\"";
return -1;
}
String background = "";
for(int i = 0; i < temp.length(); i++){
background = background + temp.substring(i, i + 1);
}
if(background.equals("special1")) {
specialBackground = 1;
return 0;
}
if(background.equals("special2")){
specialBackground = 2;
return 0;
}
if(background.equals("special3")){
specialBackground = 3;
return 0;
}
if(background.equals("special4")){
specialBackground = 4;
return 0;
}
if(background.equals("special5")){
specialBackground = 5;
return 0;
}else{
return 1;
}
}
/**
* Metodo che si occupa di creare un'immagine in base al numero di pixel dell'applet
* @return -l'immagine ottenuta
*/
private MemoryImageSource createBackground(){
int appWidth = getSize().width;
int appHeight = getSize().height;
int pixels[] = new int[appWidth * appHeight];
int index = 0;
for(int y = 0; y < appHeight; y++){
for(int x = 0; x < appWidth; x++){
int work = (x * 255) / (appWidth - 1);
switch(specialBackground){
case 5:
pixels[index++] = 0xff000000 | work << 16 | 0x80000;
break;
case 4:
pixels[index++] = 0xffff0000 | work << 8 | 0x800;
break;
case 3:
pixels[index++] = 0xff000000 | work | 8;
break;
case 2:
pixels[index++] = 0xff0000ff | work << 8 | 0x800;
break;
case 1:
pixels[index++] = 0xff000000 | work << 8 | 0x800;
break;
}
}
}
return new MemoryImageSource(appWidth, appHeight, pixels, 0, appWidth);
}
/**
* Metodo che si occupa di settare il font a partire dal parametro html che gli si passa
* @param parm -Il parametro passatogli
* @return -L'indice del font che si trova nell'array denominato fontArray2
*/
protected int getFont(String parm){
temp = this.getParameter(parm);
if(temp == null){
error = "error: " + parm + " value=\"" + temp + "\"";
return -1;
}for(int i = 0; i < fontArray2.length; i++){
int z = temp.compareTo(fontArray2[i]);
if (z == 0) {
return i;
}
}
error = "error: " + parm + " value=\"" + temp + "\"";
return -2;
}
/**
* Metodo che si occupa di restituire un indice dello stile
* @param parm -Stringa html passatagli
* @return -l'indice relativo allo stile oppure -1 se lo stile non c'è
* @details -Questo metodo, in base alla stringa in ingresso controlla se nell'array denominato
* SStyleArray è presente lo stile voluto, e cioè se grassetto, corsivo etc..
*/
protected int getStyle(String parm){
temp = getParameter(parm);
if(temp == null){
error = "error: " + parm + " value=\"" + temp + "\"";
return -1;
}
for(int i = 0; i < SStyleArray.length; i++){
int z = temp.compareTo(SStyleArray[i]);
if(z == 0)
return i;
}
error = "error: " + parm + " value=\"" + temp + "\"";
return -2;
}
/**
* Metodo run del thread
*/
public void run(){
while(thread.isAlive()){
startTime = System.currentTimeMillis();
if(run)
if(ypos < -(startLines + lineSpace * lines)){
ypos = getSize().height - startLines;
} else{
ypos = ypos - 1;
manualPause();
}
checkPause();
repaint();
highlight(mousex, mousey);
long thisTime = System.currentTimeMillis();
diffTime = thisTime - startTime;
if(diffTime < (long)pauseValue)
pause(pauseValue - (int)diffTime);
}
}
/**
* Metodo start dell'applet
*/
public void start(){
if(thread == null)
thread = new Thread(this);
thread.start();
}
private void manualPause(){
if(downTime == 0L){
return;
}
diffTime = System.currentTimeMillis() - downTime;
if(diffTime < (long)manualPause){
ypos = ypos + 1;
return;
} else{
downTime = 0L;
return;
}
}
private void checkPause(){
for(int i = 0; i < lines; i++)
if(pauseLineArray[i] > 0){
int pauseArea = startLines + lineSpace * i;
pauseArea += sizeArray[i] / 3;
pauseArea -= sizeArray[i] - lineSpace;
if(ypos == -pauseArea){
ypos = ypos + 1;
paused = true;
pause(pauseLineArray[i]);
paused = false;
ypos = ypos - 1;
return;
}
}
}
private void pause(int time){
try{
Thread.sleep(time);
return;
}catch(InterruptedException _ex){
return;
}
}
/**
* Metodo che si occupa di far cambiare colore al testo nelle posizioni x e y
* @param x -Parametro indicante la coordinata x
* @param y -Parametro indicante la coordinata y
*
*/
protected synchronized void highlight(int x, int y){
if(!link || mouseexit || linkArray == null){
return;
}
adjYpos = startLines + ypos;
diff = y - adjYpos;
lineSelect = diff / lineSpace;
if(lastLink == lineSelect){
return;
}
if(lastLink >= 0){
offscreeng.setFont(fontArray3[lastLink]);
offscreeng.setColor(colorArray[lastLink]);
offscreeng.drawString(lineArray[lastLink], lineOffsetArray[lastLink], lastlined);
}
adjYpos = startLines + ypos;
if(y < adjYpos || y > (adjYpos + totLines) - 1 || linkArray[lineSelect].equals("")){
if(paused){
repaint();
}
lastLink = -1;
showStatus("");
return;
}
lineDown = lineSpace * lineSelect;
lineDown += startLines;
lined = lineDown + lineSpace;
offscreeng.setFont(fontArray3[lineSelect]);
offscreeng.setColor(linkColor);
offscreeng.drawString(lineArray[lineSelect], lineOffsetArray[lineSelect], lined);
if(paused){
repaint();
}
if(!(linkArray[lineSelect].equals("0"))&&(!(linkArray[lineSelect].equals("")))){
this.showStatus(linkArray[lineSelect]);
this.setCursor(Cursor.getPredefinedCursor(Cursor.H AND_CURSOR));
}else{
this.setCursor(Cursor.getPredefinedCursor(Cursor.D EFAULT_CURSOR));
this.showStatus("");
}
lastLink = lineSelect;
lastlined = lined;
}
/**
* Metodo che ridipinge l'applet
* @param g -l'elemento di grafica sul quale agire
*/
public void paint(Graphics g){
if(checkError){
if(once){
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width, height);
once = false;
}
f = new Font("Verdana", 0, 10);
g.setFont(f);
g.setColor(systec);
g.drawString(error, 0, 20);
return;
} else{
g.drawImage(offscreen, 0, ypos, this);
return;
}
}
/**
* Metodo che aggiorna il disegno dell'applet
* @param g -l'elemento di grafica sul quale agire
*/
public void update(Graphics g){
paint(g);
}
/**
* Questo metodo ci permette di arrivare al'url desiderato
* @param theUrl -l'Url dove si vuole arrivare
*/
public void jumpToUrl(String theUrl){
try{
URL urlToGoto = new URL(theUrl);
getAppletContext().showDocument(urlToGoto, linkFrame);
return;
}catch(MalformedURLException e){
e.printStackTrace();
}
}
/**
* Metodo che è invocato nel momento in cui il cursore del mouse è trascinato
* col pulsante premuto
* @param me -l'evento MouseEvent
*/
public void mouseDragged(MouseEvent me){
}
/**
* Metodo invocato quando il cursore del mouse si muove nel componente
* @param me -l'evento MouseEvent
*/
public void mouseMoved(MouseEvent me){
int y = me.getY();
adjYpos = startLines + ypos;
diff = y - adjYpos;
int lineSelect = diff / lineSpace;
//setta l'aspetto del cursore
try{
if ( (! (linkArray[lineSelect].equals("0"))) && (! (linkArray.equals("")))) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.H AND_CURSOR));
this.showStatus(linkArray[lineSelect]);
}else {
this.setCursor(Cursor.getPredefinedCursor(Cursor.D EFAULT_CURSOR));
this.showStatus("");
}
}catch(ArrayIndexOutOfBoundsException aie){
}
this.mousex = me.getX();
this.mousey = me.getY();
this.highlight(mousex, mousey);
}
/**
* Metodo invocato quando il pulsante del mouse è rilasciato
* @param me -l'evento MouseEvent
*/
public void mouseReleased(MouseEvent me){
if(clicked){
offscreeng.setFont(fontArray3[lineSelect]);
offscreeng.setColor(linkColor);
offscreeng.drawString(lineArray[lineSelect], lineOffsetArray[lineSelect], lineDown + lineSpace);
repaint();
clicked = false;
}
lineDown = 0;
pause(200);
}
/**
* Metodo invocato quando il pulsante del mouse è premuto
* @param me -l'evento MouseEvent
*/
public void mousePressed(MouseEvent me){
if (downTime == 0L) {
downTime = System.currentTimeMillis();
}else{
downTime = 0L;
}
int y = me.getY();
adjYpos = startLines + ypos;
diff = y - adjYpos;
if (y < adjYpos || y > adjYpos + totLines) {
}
int lineSelect = diff / lineSpace;
lineDown = lineSpace * lineSelect;
lineDown += startLines;
if (lineSelect >= linkArray.length || linkArray[lineSelect].equals("")) {
lineDown = 0;
}
try{
if ( (! (linkArray[lineSelect].equals("0"))) && (! (linkArray.equals("")))) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.H AND_CURSOR));
this.showStatus(linkArray[lineSelect]);
}
else {
this.showStatus("");
}
if (me.getClickCount() == linkClick) {
clicked = true;
offscreeng.setFont(fontArray3[lineSelect]);
offscreeng.setColor(Color.lightGray);
offscreeng.drawString(lineArray[lineSelect], lineOffsetArray[lineSelect],
lineDown + lineSpace);
getGraphics().drawImage(offscreen, 0, ypos, this);
jumpToUrl(linkArray[lineSelect]);
}
}catch(ArrayIndexOutOfBoundsException aie){
}
}
/**
* Metodo invocato quando il cursore del mouse esce dall'area del componente
* @param me -l'evento MouseEvent
*/
public void mouseExited(MouseEvent me){
mouseexit = true;
if(!run){
run = true;
}
if(lastLink >= 0){
offscreeng.setFont(fontArray3[lastLink]);
offscreeng.setColor(colorArray[lastLink]);
offscreeng.drawString(lineArray[lastLink], lineOffsetArray[lastLink], lastlined);
lastLink = -1;
repaint();
showStatus("");
}
lineSelect = -1;
lastLink = -1;
}
/**
* Metodo invocato quando il cursore del mouse entra nell'area del componente
* @param me -l'evento MouseEvent
*/
public void mouseEntered(MouseEvent me){
run = false;
mouseexit = false;
}
/**
* Metodo invocato quando il pulsante del mouse è premuto
* @param me -l'evento MouseEvent
*/
public void mouseClicked(MouseEvent me){
}
}
Spero ti sia d'aiuto... ciao![]()

Rispondi quotando