Loading:


Różnokolorowe teksty nadjeżdżające od prawej strony

Skrypt tworzy Aplet Java wyświetlający różnokolorowe teksty nadjeżdżające od prawej strony

Aby stworzyć Aplet Java tworzymy najpierw kod Java w pliku plik.class a następnie tworzymy odwołanie do niego w HTML poprzez znacznik <aplet></aplet>



Napisz Artyku³

Listing


// HTML

<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET
        code      = "kodJava.class"
        text1     = "Przykadowy tekst..."
        text2     = "Jeszcze jeden tekst..."
        text3     = "Kolejny przykad tekstu..."
        textsNo   = "3"
        fontsize  = "30"
        speedstep = "1"
        width     = "640"
        height    = "100">
</APPLET>
</BODY>
</HTML>


//KOD kodJava.class

import java.awt.*;
import java.applet.*;
import java.util.*;

public
class FlowString extends Applet implements Runnable
{
  int sX, mX, y, speedStep, currText = 0;
  int appWidth, appHeight;
  int strWidth, strHeight, strMaxDescent;
  String[] textsArr;
  String sText, mText;
  Font fontTimesRoman;
  Color sColor, mColor;
  Random rnd;
  Image img;
  Graphics gDC, mDC;
  boolean stopped = false;
  public void init()
  {
    String strTextsNo;
    int textsNo;

    if((strTextsNo = getParameter("textsNo")) == null)
      strTextsNo = "2";

    try{
        textsNo = Integer.parseInt(strTextsNo);
    }
    catch(Exception e){
      textsNo = 2;
    }

    textsArr = new String[textsNo];

    String str;

    for(int i = 1; i <= textsNo; i++){
      if((str = getParameter("text" + i)) == null)
        str = "Brak parametru text" + i + "!";
      textsArr[i - 1] = str;
    }

    String strFontSize;
    int fontSize;
    if((strFontSize = getParameter("fontsize")) == null)
      strFontSize = "36";

    try{
        fontSize = Integer.parseInt(strFontSize);
    }
    catch(Exception e){
      fontSize = 36;
    }

    String strSpeedStep;
    if((strSpeedStep = getParameter("speedstep")) == null)
      strSpeedStep = "1";

    try{
        speedStep = Math.abs(Integer.parseInt(strSpeedStep));
    }
    catch(Exception e){
      speedStep = 1;
    }

    sText = "";
    mText = textsArr[0];

    appWidth = getWidth();
    appHeight = getHeight();

    setBackground(Color.white);

    fontTimesRoman = new Font ("TimesRoman", Font.BOLD, fontSize);
    img = createImage(appWidth, appHeight);
    mDC = img.getGraphics();
    gDC = getGraphics();

    gDC.setFont(fontTimesRoman);
    FontMetrics fm = gDC.getFontMetrics();
    strHeight = fm.getHeight();
    strWidth = fm.stringWidth(mText);
    strMaxDescent = fm.getMaxDescent();

    sX = 0;
    mX = appWidth;
    y = (int)((appHeight + strHeight) / 2);

    rnd = new Random();
    mColor = new Color(rnd.nextInt(16777215));
    sColor = new Color(rnd.nextInt(16777215));
  }
  public void start()
  {
    Thread thread = new Thread (this);
    thread.start();
  }
  public void stop()
  {
    stopped = true;
  }
  public void run()
  {
    while (!stopped){
      if (mX <= 0){
        sText = mText;
        sColor = mColor;
        mColor = new Color(rnd.nextInt(16777215));
        if(++currText >= textsArr.length)
          currText = 0;
        mText = textsArr[currText];
        FontMetrics fm = gDC.getFontMetrics();
        strHeight = fm.getHeight();
        strWidth = fm.stringWidth(mText);
        strMaxDescent = fm.getMaxDescent();

        sX = 0;
        mX = appWidth;
        y = (int)((appHeight + strHeight) / 2);
      }
      try{
        Thread.sleep(1);
      }
      catch (InterruptedException e){
      }
      mX -= speedStep;

      mDC.clearRect(0, 0, appWidth, appHeight);
      mDC.setColor (sColor);
      mDC.setFont (fontTimesRoman);
      mDC.drawString (sText, sX, y);
      mDC.setColor (mColor);
      mDC.clearRect(mX, y - strHeight + strMaxDescent, appWidth - mX, strHeight);
      mDC.drawString (mText, mX, y);
      gDC.drawImage (img, 0, 0, this);
    }
  }
}

 




Dodano przez: igor
Ranga: Administrator serwisu Punktów: 28716
Komentarze użytkowników
    • Tre¶æ komentarza
      Kod do komentarza (opcjonalnie)
      PHP JavaScript MySQL Smarty SQL HTML CSS ActionScript
      Autor
      Token
      token

       

       








funkcje.net
Wszelkie prawa zastrzeżone©. | Funkcje.net 2008-2024 v.1.5 | design: diviXdesign & rainbowcolors