ashsha91
ashsha91

Reputation: 11

Java - inserting line break in scrolling text

Java Newbie. I am trying to display scrolling text using Jpanel and timer, and it works, but I tried to insert a line separator using the system line separatorthe text displays without line breaks, why?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Participants extends JPanel
{
     private int x;
     private int y;
     private String text;

     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    public Participants  ()
    {
        x = 600;
        y = 1200;
        String eol = System.getProperty("line.separator");  

        text = "Story Director/Producer:"+eol+"Mr. SMith" +
             "Technical Director:" +  eol + 
              "Mr. T" +  eol + eol;

        setSize(1200, 900);
    }

public void paint(Graphics g)
{
    super.paint(g);
    g.setColor(Color.white);
    g.fillRect(0, 0, 1200, 900);
    g.setColor(Color.black);
    g.drawString(text,x, y);


}

public void start() throws InterruptedException
{
    while(true)
    {
        while(y >= 0)
        {
            x = getWidth() / 2;
            y--;
            repaint();
            Thread.sleep(10);
        }
    }
}

public static void main (String [] args) throws InterruptedException
{
    JFrame frame = new JFrame("Participants  ");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Participants participants  = new Participants ();
    frame.getContentPane().add(participants  );
    frame.setSize(1200, 900);
    frame.setVisible(true);
    participants  .start();
}

}

Upvotes: 0

Views: 439

Answers (1)

Ravi Trivedi
Ravi Trivedi

Reputation: 2360

Line breaks(\n) works only for console writing. It does not work in swing. Just use separate Draw methods to write different text.

Regards, Ravi

Upvotes: 1

Related Questions