Loading:


Tekst płynący w górę

Skrypt wyświetla aplet w którym tekst wpłynie w górę.

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


// Kod HTML

<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET
        code      = "kodJava.class"
        text1     = "Przykładowy tekst..."
        text2     = "Jeszcze jeden tekst..."
        text3     = "Kolejny przykład tekstu..."
        textsNo   = "3"
        fontsize  = "30"
        speedstep = "1"
        sleepInterval = "10"
        width     = "640"
        height    = "400">
</APPLET>
</BODY>
</HTML>


//Kod Java

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

public
class FlowString extends Applet implements Runnable
{
  int speedStep;
  int appWidth, appHeight;
  int strHeight, strVSpace, strMaxDescent;
  int imgHeight;
  int sleepInterval;
  String[] textsArr;
  Font fontTimesRoman;
  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;
    }

    String strSleepInterval;
    if((strSleepInterval = getParameter("sleepInterval")) == null)
      strSleepInterval = "1";
    try{
        sleepInterval = Math.abs(Integer.parseInt(strSleepInterval));
    }
    catch(Exception e){
      sleepInterval = 1;
    }

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

    setBackground(Color.white);

    fontTimesRoman = new Font ("TimesRoman", Font.BOLD, fontSize);
    gDC = getGraphics();
    gDC.setFont(fontTimesRoman);

    FontMetrics fm = gDC.getFontMetrics();
    strHeight = fm.getHeight();
    strMaxDescent = fm.getMaxDescent();

    imgHeight = (strHeight + strVSpace) * textsArr.length + strMaxDescent;
    img = createImage(appWidth, imgHeight);
    mDC = img.getGraphics();
    mDC.setFont(fontTimesRoman);

    int mY = strHeight;
    for(int i = 0; i < textsArr.length; i++){
      mDC.drawString(textsArr[i], 1, mY);
      mY += strVSpace + strHeight;
    }
  }
  public void start()
  {
    Thread thread = new Thread (this);
    thread.start();
  }
  public void stop()
  {
    stopped = true;
  }
  public void run()
  {
    int y = appHeight;
    int x = 0;
    while (!stopped){
      if(y < -imgHeight){
        y = appHeight;
      }
      try{
        Thread.sleep(sleepInterval);
      }
      catch (InterruptedException e){
      }
      y -= speedStep;

      gDC.drawImage (img, x, y, 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