Loading:


    Kolory przepływające przez tekst

    Skrypt wyświetla aplet w którym różne kolory przepływają przez przykładowy tekst.

    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"
            text      = "http://funkcje.net"
            fontsize  = "30"
            speedstep = "1"
            sleepInterval = "5"
            width     = "600"
            height    = "150">
    </APPLET>
    </BODY>
    </HTML>



    //Kod Java kodJava.class

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

    public
    class Aplet extends Applet implements Runnable
    {
      int x, y, speedStep;
      int appWidth, appHeight;
      int strWidth, strHeight, strMaxDescent;
      int sleepInterval;
      int colorTable[];
      String napis;
      Font fontTimesRoman;
      BufferedImage img;
      Graphics gDC, mDC;
      boolean stopped = false;
      public void init()
      {
        if((napis = getParameter("text")) == null)
          napis = "Brak parametru text!";

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

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

        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();

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

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

        x = (int)((appWidth - strWidth) / 2);
        y = (int)((appHeight + strHeight) / 2);

        colorTable = new int[512 + 2 * strWidth];
        for(int i = 0; i < 255; i++){
          colorTable[strWidth + i] = i * 65536 + 0 * 256 + i;
          colorTable[strWidth + 255 + i] = (255 - i) * 65536 + 0 * 256 + 255 - i;
        }
        for(int i = 0; i < strWidth; i++){
          colorTable[i] = 0;
          colorTable[512 + strWidth + i] = 0;
        }
      }
      public void start()
      {
        Thread thread = new Thread (this);
        thread.start();
      }
      public void stop()
      {
        stopped = true;
      }
      public void run()
      {
        Color color = new Color(0, 0, 0);
        mDC.setColor (color);
        mDC.setFont (fontTimesRoman);
        mDC.clearRect(0, 0, appWidth, appHeight);
        int backgroundColor = img.getRGB(0, 0);
        int arrPos = 0, k = 1;
        mDC.drawString (napis, x, y);
        int color1 = 0;
        while (!stopped){
          for (int i = 0; i < strWidth; i++){
            color1 = colorTable[arrPos + i];
            for (int j = 0; j < strHeight; j++){
              int posX = x + i;
              int posY = y - strHeight + strMaxDescent + j;
              if (img.getRGB(posX, posY) != backgroundColor){
                img.setRGB(posX, posY, color1);
              }
            }
          }
          arrPos += k;
          if(arrPos >= colorTable.length - strWidth || arrPos <= 0) k = -k;
          gDC.drawImage (img, 0, 0, this);
          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