Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Need a little help.

Options
  • 23-11-2005 7:23pm
    #1
    Closed Accounts Posts: 3


    Hey all,

    I'm doing a little college work and I'm looking for Java scrolling text, ie, text that goes from the right side of the screen to the left. I've searched Java.Sun.com high and low and they haven't given me jack. Anybody have any ideas where I can find a link to something to help me out. I've found a couple of simple ones that go up the screen but have no idea how to tweek them to go across. Here's one. It's called ScrollingTextComponent. Please help!



    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.Timer;

    /**
    * @author ad
    */
    public class ScrollingTextComponent extends JComponent implements ActionListener{

    private String[] text;
    private Timer t;

    int start = 80;

    private boolean firstTime = true;
    /**
    * Create a new Scrolling Text Component
    * Either Up for Left.
    */
    public ScrollingTextComponent(String[] text) {
    this.text = text;
    this.t = new Timer(20, this);
    this.t.start();
    }


    /**
    * paint
    * @see javax.swing.JComponent#paint(java.awt.Graphics)
    */
    public void paint(Graphics g) {
    if(this.firstTime) {
    this.start = g.getClipBounds().height;
    this.firstTime = false;
    }

    FontMetrics fm = g.getFontMetrics();
    int yLocation = this.start + fm.getHeight();
    int stopCount = 0;
    for(int i = 0; i < this.text.length; i++) {
    if(yLocation > 0) {
    g.drawString(this.text, 0, yLocation);
    }else {
    stopCount++;
    }
    yLocation += + fm.getHeight();
    }
    //g.drawImage(this.image, 0, y, null);

    if(stopCount == this.text.length){
    this.start = g.getClipBounds().height;
    }
    this.start--;
    }


    /**
    * actionPerformed
    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
    */
    public void actionPerformed(ActionEvent e) {
    /**
    * Force a Repaint.
    */
    this.repaint();
    }

    public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(640,480));
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(new ScrollingTextComponent(new String[] {"Andrew", "Peter", "Dunn"}));
    frame.show();

    }
    }


Comments

  • Closed Accounts Posts: 779 ✭✭✭homeOwner


    I think you might finding it difficult to find examples because you are looking for "scrolling". When I first read your post I assumed you were looking for up-down scrolling which is what most java developers would think of when they read scrolling (probably because of the scrollpane component in swing). Or maybe not.....

    Anyway, i did a quick search for Ticker Tape examples, because that is the usual word used to describe text going across the screen and there are loads of hits.

    Try this site
    http://javaboutique.internet.com/javasource/t.html
    There are a couple of Ticker examples. I didnt look at them but they may have what you want.

    Put "java examples ticker tape text" into google and you'll get lots of others.


  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    a simple solution would be to add a narrow html window to your form and use the html tag </marquee>.


  • Closed Accounts Posts: 3 hatty


    Ok, I've seen examples of this before but the problem is that it can't be an applet. I basically has to be code in a program. I'm putting it into a JInternalFrame. I have'nt found any Ticker Tape examples of that either. Is there any way to tweek that to put it in a program?


  • Closed Accounts Posts: 3 hatty


    Ok, I've found another example of this but I can't create an instance of it or create a main in this file. How can I make this run?

    import java.applet.Applet;
    import java.awt.*;
    import java.lang.*;
    import java.net.*;


    public class TickerTape extends Applet implements Runnable {

    private final static int NUM_TEXT = 10;

    private int delay = 100;
    private int offset = 0;
    private int width;
    private int twidth = 0;
    private int height;
    private String text = null;
    private String[] url;
    private int[] url_off;
    private int url_count = 0;
    private Thread thread = null;
    private int baseline;
    private Image image;
    private Font font;
    private int step = 3;

    public void init() {
    int fsize = 18; // default font size
    String str, arg;
    int count;

    setBackground(Color.black);
    setForeground(Color.white);

    // get parameters
    if ((str = super.getParameter("size")) != null)
    fsize = Integer.valueOf(str).intValue();
    if ((str = super.getParameter("font")) == null)
    str = "TimesRoman";
    this.font = new Font(str, Font.PLAIN, fsize);
    if ((str = super.getParameter("delay")) != null)
    this.delay = Integer.valueOf(str).intValue();
    if ((str = super.getParameter("step")) != null)
    this.step = Integer.valueOf(str).intValue();
    if ((str = super.getParameter("count")) != null)
    count = Integer.valueOf(str).intValue();
    else
    count = NUM_TEXT;

    // get height and width
    this.width = super.size().width;
    this.height = super.size().height;

    // set up secondary image
    this.image = createImage(this.width, this.height);

    // set up objects
    this.url = new String[count];
    this.url_off = new int[count];
    FontMetrics fm = image.getGraphics().getFontMetrics(this.font);
    this.baseline = fm.getHeight() - fm.getDescent();

    // read in text lines
    for (int i = 0; i < count; i++) {
    arg = "text" + i;
    if ((str = super.getParameter(arg)) != null) {

    // save text
    if (this.text == null)
    this.text = str;
    else
    this.text = this.text + str;

    // get the pixel width of the string
    this.twidth = fm.stringWidth(text);
    this.url_off[this.url_count] = twidth;

    // get the url address
    arg = "url" + i;
    this.url[this.url_count] = super.getParameter(arg);

    this.url_count++;
    }
    }
    }

    public final synchronized void update(Graphics g) {
    int off;

    // make sure there is text
    if (this.text == null)
    return;
    // draw
    Graphics gfx = this.image.getGraphics();
    gfx.setFont(this.font);
    gfx.setColor(super.getBackground());
    gfx.fillRect(0, 0, this.width, this.height);
    gfx.setColor(super.getForeground());
    gfx.drawString(this.text, this.width - this.offset, this.baseline);
    g.drawImage(this.image, 0, 0, null);

    // move
    this.offset += this.step;
    if (this.offset >= this.twidth + this.width)
    this.offset = 0;
    }

    public boolean mouseDown(Event evt, int x, int y) {

    int off = (-(this.width - this.offset)) + x;
    if (off < 0 || off > this.twidth)
    return true;
    int entry = 0;
    int begin = 0;
    for (int i = 0; i < this.url_count; i++)
    if (off >= begin && off < this.url_off) {
    entry = i;
    begin = this.url_off;
    }
    if (url[entry] != null) {
    try {
    super.getAppletContext().showDocument(new URL(url[entry]));
    } catch(MalformedURLException e) {
    System.out.println("Invalid address " + url[entry]);
    }
    if (url[entry].substring(0,6).compareTo("mailto") != 0)
    this.stop();
    }
    return true;
    }

    public void run() {
    while(this.thread != null) {
    try { Thread.sleep(this.delay); }
    catch (InterruptedException e) { }
    super.repaint();
    }
    }

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

    public void stop() {
    if (this.thread != null) {
    this.thread.stop();
    this.thread = null;
    }
    }

    public String getAppletInfo() {
    return "yet another TickerTape.java";
    }

    public String[][] getParameterInfo() {
    String[][] i = {
    {"font", "string", "Font (def TimesRoman)" },
    {"size", "int", "Font Size (def 18)" },
    {"delay", "int", "Scroll delay in ms (def 100)" },
    {"step", "int", "Scroll pixel step (def 3)" },
    {"count", "int", "Number of text messages (def 10)" },
    {"text0", "string", "Text message 0" },
    {"textn", "string", "Text message #n" },
    {"url0", "string", "URL 0 associated with text0" },
    {"urln", "string", "URL #n associated with textn" }
    };

    return i;
    }
    }


Advertisement