0

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

}

ashsha91
  • 11
  • 5
  • 1
    Use a `JTextArea` or equivalent. Apart from the fact that it will support new line characters, it is also highly optimized when it comes to painting – MadProgrammer Apr 11 '13 at 03:50
  • 1
    See [this post](http://stackoverflow.com/questions/4413132/problems-with-newline-in-graphics2d-drawstring) – Reimeus Apr 11 '13 at 03:57

1 Answers1

1

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

Ravi Trivedi
  • 2,340
  • 2
  • 14
  • 20