Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how to implement marquee??

cherry1cherry1 Posts: 19Member
i want to implement text which moves from say left to right in my interface...sort of like a marquee.

i think the best way to do that would be through a thread but i dont know a thing about threads. can anyone tell me how i can implement moving text in interface to my application?

thanx in advance.

Comments

  • arb123arb123 Posts: 696Member
    : i want to implement text which moves from say left to right in my interface...sort of like a marquee.
    :
    : i think the best way to do that would be through a thread but i dont know a thing about threads. can anyone tell me how i can implement moving text in interface to my application?
    :
    : thanx in advance.
    :

    If you're using Swing:
    1) use a JPanel that implements ActionListener as your canvas (override paint)
    2) use a javax.swing.Timer to move your text (it'll call the ActionListener)
    3) use font metrics to calculate your text size

    See javadoc + tutorials on Sun website for more on these

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • cherry1cherry1 Posts: 19Member
    [b][red]This message was edited by cherry1 at 2005-6-23 0:40:57[/red][/b][hr]
    hi!
    here is vat i managed to do...but it has an error which i dont know how to remove, so it wont run.
    [code]
    public class MyMarquee extends JFrame implements ActionListener
    {
    private JTextField marquee;
    private Timer tick;

    public MyMarquee ()
    {
    //so that i dont need to implement windowListener to close
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout (new FlowLayout());

    //"scrolling..." is the text i want to move left 2 right
    marquee = new JTextField ("scrolling...");
    marquee.addActionListener(this);
    c.add(marquee);

    setSize(100, 400);
    show();
    //Timer(delay, ActionListener)
    //Timer.start() to start the timer
    tick = new Timer (1000, this).start();
    }

    public void actionPerformed (ActionEvent e)
    {
    //add spaces to left so that the text appears to move
    marquee.setText(" " + marquee.getText());
    }

    public static void main (String args[])
    {
    MyMarquee app = new MyMarquee();
    }

    } [/code]

    and this is the error that i get:
    [code]MyMarquee.java:27: incompatible types
    found : void
    required: javax.swing.Timer
    tick = new Timer (1000, this ).start();
    ^
    [/code]

    ok, i didnt understand point (3) about using font metrics. plz elaborate.

    and can u tell me some way to determine when the text runs off the label so that i can start all over again?

    also, when (and where) should i stop the timer?

    thanx in advance.
    : ---------------------------------
    : [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :



  • arb123arb123 Posts: 696Member
    The start method returns void; it is the last method call in the expression that is used in the assignment.

    [code]
    tick = new Timer (1000, this);
    tick.start();
    [/code]

    A JLabel will be better than a JTextField to see the scroll effect you're using.

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • cherry1cherry1 Posts: 19Member

    : A JLabel will be better than a JTextField to see the scroll effect you're using.
    :
    according to the java docs....JLabel doesnt react to input events and when i try to addActionListener to it, i get a "cannot resolve symbol" error.
    also, u r right, the text doest appear to be scrolling.
    vat do i do now? :(
    : ---------------------------------
    : [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :

  • arb123arb123 Posts: 696Member
    re: the text doest appear to be scrolling.

    The text is being prepended with space characters, but the cursor is set to the end of the field, so you don't see them. The component doesn't have to support the ActionListener; its just needed for the Timer.

    [code]
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    /**
    * This class is not Thread safe; make calls on the UI Thread
    * using SwingUtilities.invokeXXX() calls.
    */
    public class MyMarquee extends JPanel implements ActionListener {

    private Timer tick;

    private int offset;

    private String text = null;

    private int decrement;

    private int textWidth = 0;

    private int textHeight = 0;

    private Font font;

    /**
    * @param delay time between ticks
    * @param decrement how much the offset changes at each tick
    */
    public MyMarquee(int delay, int decrement) {
    if ((delay < 0) || (decrement < 0)) {
    throw new IllegalArgumentException();
    }

    this.decrement = decrement;

    tick = new Timer(delay, this);
    tick.start();

    font = new Font("Dialog", Font.BOLD, 20);
    }

    public void actionPerformed(ActionEvent event) {
    if (offset < (-1 * textWidth)) {
    offset = getWidth();
    } else {
    offset -= decrement;
    }

    repaint();
    }

    public void paint(Graphics g) {
    super.paint(g);

    if (text != null) {
    g.setFont(font);
    textWidth =
    g.getFontMetrics().charsWidth(
    text.toCharArray(),
    0,
    text.length());
    textHeight = g.getFontMetrics().getHeight();
    g.drawString(text, offset, textHeight);
    } else {
    textWidth = 0;
    textHeight = 0;
    }
    }

    public String getText() {
    return text;
    }

    public void setText(final String string) {
    text = string;
    }

    public static void main(String[] args) {
    MyMarquee marquee = new MyMarquee(100, 5);
    marquee.setText("Hello, World!");

    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    frame.setContentPane(marquee);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }

    }
    [/code]

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

Sign In or Register to comment.