Loading:


    Zmiana tekstu poprzez ściemnienie i rozjaśnienie

    Skrypt wyświetla aplet w którym przykładowy tekst zmienia się na inny poprzez ściemnienie i rozjaśnienie.

    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>
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <APPLET
            code      = "kodJava.class"
            text      = "Przykładowy tekst..."
            text1     = "Przykładowy tekst..."
            text2     = "Jeszcze jeden tekst..."
            text3     = "Kolejny przykład tekstu..."
            textsNo   = "3"
            fontsize  = "30"
            speedstep = "1"
            sleepInterval = "10"
            width     = "600"
            height    = "100">
    </APPLET>
    <BODY>
    </HTML>



    //Kod Java kodJava.class

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

    public
    class Aplet extends Applet implements Runnable
    {
      int x, y, speedStep, sleepInterval;
      int appWidth, appHeight;
      int strWidth, strHeight;
      String textsArr[], napis;
      Font fontTimesRoman;
      Graphics gDC;
      boolean stopped = false;
      public void init()
      {
        String strTextsNo;
        int textsNo;

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

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

        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;
        }

        napis = textsArr[0];

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

        setBackground(Color.white);

        fontTimesRoman = new Font ("TimesRoman", Font.BOLD, fontSize);
        gDC = getGraphics();
        gDC.setFont(fontTimesRoman);
      }
      public void start()
      {
        Thread thread = new Thread (this);
        thread.start();
      }
      public void stop()
      {
        stopped = true;
      }
      public void setupText(int textNo)
      {
        napis = textsArr[textNo];

        FontMetrics fm = gDC.getFontMetrics();
        strHeight = fm.getHeight();
        strWidth = fm.stringWidth(napis);

        x = (int)((appWidth - strWidth) / 2);
        y = (int)((appHeight + strHeight) / 2);
      }
      public void run()
      {
        Color color;
        int i = 0, j = speedStep;
        int currTextNo = 0;
        setupText(currTextNo);
        while (!stopped){
          color = new Color(i, i, i);
          i += j;
          if(i >= 255 || i <= 0) j = -j;
          gDC.setColor (color);
          gDC.drawString (napis, x, y);
          if(i >= 255){
            if(++currTextNo >= textsArr.length) currTextNo = 0;
            setupText(currTextNo);
          }
          try{
            Thread.sleep(sleepInterval);
          }
          catch (InterruptedException e){
          }
        }
      }
    }
     




    Dodano przez: igor
    Ranga: Administrator serwisu Punktów: 0
    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-2025 v.1.5 | design: diviXdesign & rainbowcolors