2

I am learning how to use Java and I want to make a text scroller for my website.

I have found a similar scroller that I want to add but I want to look at the code to see how it was done.

The applet can be found here: http://www.javascriptkit.com/java/java20.shtml

The problem is when I try to open the class file within notepad the encoding or text shows up with strange characters instead of showing me the code.

Please can someone let me know if there is any possible way of me seeing the code for this class.

Thanks

Sam
  • 1,207
  • 4
  • 26
  • 50
  • 1
    Java is a fine language, but java applets for websites are not really used anymore. There are better technologies. If you're making a serious website (rather than using the website as a project to work on to learn Java) you'd be better using something else. – Oliver Jan 11 '12 at 11:43
  • @Oliver okay thanks, just wanted to see how the scrolling text was done. But thanks. – Sam Jan 11 '12 at 11:46
  • You must find java source file of the applet. Or you should decompile it to see source code. Otherwise you can not see clear code from a compiled file. – Baatar Jan 11 '12 at 11:46
  • 1) People generally detest scrolling text on web sites. 2) If you insist on doing it, it can be achieved using JavaScript. – Andrew Thompson Jan 11 '12 at 11:54
  • @Ricky, serach for scrollbars using javascript, you will find plenty of excellent examples with copy and paste code – Johnydep Jan 11 '12 at 12:03
  • possible duplicate of [How can I open Java .class files in a human-readable way?](http://stackoverflow.com/questions/76314/how-can-i-open-java-class-files-in-a-human-readable-way) – Orion Edwards Jan 11 '12 at 21:12

2 Answers2

3

That is a class file meaning it is compiled code so you can not see it's source. In order to see the source, you need the .java file which is the file which you compile to get the byte code.

It seems the site is just providing the compiled class, and you never know using it they might have some hidden functionality in the class as well e.g to send information to the owner servers etc.

EDIT:

So here is the de-compiled code of applet written by ProScroll version 2.3 by Slava Pestov

import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

public class ProScroll extends Applet
  implements Runnable
{
  private Thread thread;
  private Image image;
  private int scrollLength;
  private int scrolled;
  private int speed = 10;
  private int imgsWidth;
  private URL url;
  private String target;
  private Color bgColor;

  public void init()
  {
    int i = 12; int j = 0;

    String str2 = getParameter("TEXT");
    String str3 = getParameter("TEXTURL");

    if ((str2 == null) && (str3 == null)) {
      str2 = "No TEXT or TEXTURL parameter specified";
    }
    if (str3 != null)
    {
      localObject = new StringBuffer();
      try
      {
        InputStream localInputStream = new URL(str3)
          .openConnection().getInputStream();
        while (true)
        {
          int k = localInputStream.read();
          if (k == -1) break;
          if (k != 10) {
            if (k == 9) { ((StringBuffer)localObject).append(' '); continue; }
            ((StringBuffer)localObject).append((char)k);
          }
        }
        localInputStream.close();

        str2 = ((StringBuffer)localObject).toString();
      }
      catch (Exception localException1)
      {
        if (str2 == null) {
          str2 = "Error loading text from URL: " + localException1;
        }
      }
    }
    String str1 = getParameter("FONT");
    if (str1 == null) str1 = "TimesRoman";

    this.target = getParameter("TARGET");
    if (this.target == null) this.target = "_self";

    try
    {
      i = Integer.parseInt(getParameter("SIZE"));
    }
    catch (Exception localException2)
    {
    }

    Object localObject = getParameter("STYLE");
    if ("bold".equals(localObject)) j = 1;
    else if ("italic".equals(localObject)) j = 2;
    else if ("bolditalic".equals(localObject)) j = 3;

    String str4 = getParameter("SPEED");
    if ("slow".equals(str4)) this.speed = 20;
    else if ("medium".equals(str4)) this.speed = 15; else {
      this.speed = 10;
    }
    try
    {
      this.url = new URL(getDocumentBase(), getParameter("URL"));
    }
    catch (Exception localException3)
    {
    }

    this.bgColor = parseColorName(getParameter("BGCOLOR"), Color.black);

    Enumeration localEnumeration = parseAndLoadImages(getParameter("IMAGES"));

    Font localFont = new Font(str1, j, i);

    FontMetrics localFontMetrics = getToolkit().getFontMetrics(localFont);

    this.image = createImage(localFontMetrics.stringWidth(str2) + this.imgsWidth + size().width, size().height);

    this.scrolled = (-size().width);

    parseAndDrawText(this.image.getGraphics(), str2, localFontMetrics, localFont, localEnumeration);
  }

  private void parseAndDrawText(Graphics paramGraphics, String paramString, FontMetrics paramFontMetrics, Font paramFont, Enumeration paramEnumeration)
  {
    paramGraphics.setFont(paramFont);
    paramGraphics.setColor(this.bgColor);
    paramGraphics.fillRect(0, 0, this.image.getWidth(this), this.image.getHeight(this));
    paramGraphics.setColor(Color.white);

    StringBuffer localStringBuffer = new StringBuffer();
    int i = 0; int j = 0;

    for (int k = 0; k < paramString.length(); k++)
    {
      char c = paramString.charAt(k);

      if ((c == '#') && (j == 0))
      {
        if (i != 0)
        {
          paramGraphics.setColor(parseColorName(localStringBuffer.toString(), Color.white));
          localStringBuffer.setLength(0);
          i = 0;
        }
        else
        {
          i = 1;
        }
      }
      else if ((c == '$') && (i == 0) && (j == 0))
      {
        try
        {
          Image localImage = (Image)paramEnumeration.nextElement();
          paramGraphics.drawImage(localImage, this.scrollLength, 0, this);
          this.scrollLength += localImage.getWidth(this);
        }
        catch (Exception localException)
        {
        }
      }
      else if ((c == '/') && (j == 0))
      {
        j = 1;
      }
      else if (i != 0)
      {
        localStringBuffer.append(c);
      }
      else
      {
        if (j == 1) j = 0;
        paramGraphics.drawString(String.valueOf(c), this.scrollLength, paramFontMetrics.getAscent());
        this.scrollLength += paramFontMetrics.charWidth(c);
      }
    }
  }

  private Color parseColorName(String paramString, Color paramColor)
  {
    if ("red".equals(paramString)) return Color.red;
    if ("green".equals(paramString)) return Color.green;
    if ("blue".equals(paramString)) return Color.blue;
    if ("yellow".equals(paramString)) return Color.yellow;
    if ("orange".equals(paramString)) return Color.orange;
    if ("white".equals(paramString)) return Color.white;
    if ("lightGray".equals(paramString)) return Color.lightGray;
    if ("gray".equals(paramString)) return Color.gray;
    if ("darkGray".equals(paramString)) return Color.darkGray;
    if ("black".equals(paramString)) return Color.black;
    if ("cyan".equals(paramString)) return Color.cyan;
    if ("magenta".equals(paramString)) return Color.magenta;
    if ("pink".equals(paramString)) return Color.pink;
    return paramColor;
  }

  private Enumeration parseAndLoadImages(String paramString)
  {
    if (paramString == null) return null;

    int i = 0;
    Vector localVector = new Vector();
    StringTokenizer localStringTokenizer = new StringTokenizer(paramString);
    MediaTracker localMediaTracker = new MediaTracker(this);

    while (localStringTokenizer.hasMoreTokens())
    {
      try
      {
        Image localImage = getImage(new URL(getDocumentBase(), localStringTokenizer.nextToken()));
        localVector.addElement(localImage);
        localMediaTracker.addImage(localImage, i);
        localMediaTracker.waitForID(i++);
        this.imgsWidth += localImage.getWidth(this);
      }
      catch (Exception localException)
      {
      }
    }

    return localVector.elements();
  }

  public void start()
  {
    this.thread = new Thread(this);
    this.thread.start();
  }

  public void stop()
  {
    this.thread = null;
    this.scrolled = (-size().width);
  }

  public void run()
  {
    while (Thread.currentThread() == this.thread)
    {
      long l = System.currentTimeMillis();

      if (++this.scrolled > this.scrollLength) this.scrolled = (-size().width);

      repaint();
      try
      {
        Thread.sleep(Math.max(this.speed - (System.currentTimeMillis() - l), 0L));
      }
      catch (InterruptedException localInterruptedException)
      {
      }
    }
  }

  public boolean mouseEnter(Event paramEvent, int paramInt1, int paramInt2)
  {
    if (this.url != null) getAppletContext().showStatus("Link: " + this.url.toString());
    return true;
  }

  public boolean mouseExit(Event paramEvent, int paramInt1, int paramInt2)
  {
    if (this.url != null) getAppletContext().showStatus("");
    return true;
  }

  public boolean mouseUp(Event paramEvent, int paramInt1, int paramInt2)
  {
    if (this.url != null) getAppletContext().showDocument(this.url, this.target);
    return true;
  }

  public void update(Graphics paramGraphics)
  {
    paramGraphics.setColor(this.bgColor);
    if (this.scrolled < 0)
    {
      paramGraphics.setColor(this.bgColor);
      paramGraphics.fillRect(0, 0, -this.scrolled, size().height);
    }
    paramGraphics.drawImage(this.image, -this.scrolled, 0, this);
  }

  public void paint(Graphics paramGraphics)
  {
    update(paramGraphics);
  }

  public String getAppletInfo()
  {
    return "ProScroll version 2.3 by Slava Pestov";
  }
}
Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • *"never know using it they might .. send information to the owner server"* Use a network packet sniffer like [Ethereal](http://www.ethereal.com/) to find out. – Andrew Thompson Jan 11 '12 at 11:52
  • @AndrewThompson, sure but assuming the users who will be using this applet, not all have checks on what is going on. For a newbie like me, would be simply using the applet and running it on my site, that's all. – Johnydep Jan 11 '12 at 12:02
0

a java .class file is a compiled file that you cannot read with notepad.

if you want the source code you should try to ask it to the author of the article or you could decompile it.

pbanfi
  • 1,885
  • 2
  • 19
  • 23
  • http://stackoverflow.com/questions/76314/how-can-i-open-java-class-files-in-a-human-readable-way?lq=1 – Sam Dec 21 '14 at 22:31