Animation Question

I have a simple animation which scrolls the text upward :

import java.awt.*;
import javax.swing.*;

class Authors extends JPanel {

      String[] headlines = {

      " This program was made by Computer Wizard's :", "",
      "                Jason Dean           ", };

      int y = 76;

      void scroll() {

            while (true) {
            y = y - 1;

            if (y < -650)
            y = 76;

            repaint();

            try {
            Thread.sleep(30);
            } catch (InterruptedException e) {
            }
            }
      }

      public void paintComponent(Graphics comp) {

            Graphics2D comp2D = (Graphics2D) comp;
            Font type = new Font("monospaced", Font.BOLD, 14);
            comp2D.setFont(type);
            comp2D.setBackground(Color.black);
            comp2D.fillRect(0, 0, getSize().width, getSize().height);
            comp2D.setColor(Color.white);
            for (int i = 0; i < headlines.length; i++)
            comp2D.drawString(headlines[i], 5, y + (20 * i));
      }
}

The problem is if I call this in another class for example inside a JInternalFrame, it wont load the
program will freeze.

But if the frame is inside this class it will run perfectly :

import java.awt.*;
import javax.swing.*;

public class Authors extends JFrame {

      Authors2 news = new Authors2();

      public Authors() {

      super("Authors . . .");
      setSize(400, 300);
      JPanel pane = new JPanel();
      pane.setLayout(new GridLayout(1, 1, 15, 15));
      pane.add(news);
      setContentPane(pane);
      setVisible(true);

      news.scroll();
      }

      public static void main(String[] arguments) {

      Authors app = new Authors();

      }
}  

class Authors2 extends JPanel {

      String[] headlines = {

      " This program was made by Computer Wizard's :", "",
      "                Jason Dean           ", };

      int y = 76;

      void scroll() {

            while (true) {
            y = y - 1;

            if (y < -650)
            y = 76;

            repaint();

            try {
            Thread.sleep(30);
            } catch (InterruptedException e) {
            }
            }
      }

      public void paintComponent(Graphics comp) {

            Graphics2D comp2D = (Graphics2D) comp;
            Font type = new Font("monospaced", Font.BOLD, 14);
            comp2D.setFont(type);
            comp2D.setBackground(Color.black);
            comp2D.fillRect(0, 0, getSize().width, getSize().height);
            comp2D.setColor(Color.white);
            for (int i = 0; i < headlines.length; i++)
            comp2D.drawString(headlines[i], 5, y + (20 * i));
      }
}

The scroll(); method is what I call to scoll the message. How can I use this in a different class w/o freezing and
should work w/o any problem, thanks.

From : Jason
jason_x1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
The scrolling needs to be done in a separate thread
0
zzynxSoftware engineerCommented:
1) Have a file Authors2.java containing:

/*
 * Authors2.java
 *
 */

import java.awt.*;
import javax.swing.*;

public class Authors2 extends JPanel implements Runnable {

     String[] headlines = {

     " This program was made by Computer Wizard's :", "",
     "                Jason Dean           ", };

     int y = 76;

     public void run() {

          while (true) {
          y = y - 1;

          if (y < -650)
          y = 76;

          repaint();

          try {
          Thread.sleep(30);
          } catch (InterruptedException e) {
          }
          }
     }

     public void paintComponent(Graphics comp) {

          Graphics2D comp2D = (Graphics2D) comp;
          Font type = new Font("monospaced", Font.BOLD, 14);
          comp2D.setFont(type);
          comp2D.setBackground(Color.black);
          comp2D.fillRect(0, 0, getSize().width, getSize().height);
          comp2D.setColor(Color.white);
          for (int i = 0; i < headlines.length; i++)
          comp2D.drawString(headlines[i], 5, y + (20 * i));
     }
}

2) Have a file Authors.java containing:

/*
 * Authors.java
 *
 */

import java.awt.*;
import javax.swing.*;

public class Authors extends JFrame {

     Authors2 news = new Authors2();

     public Authors() {
         super("Authors . . .");
         setSize(400, 300);
         JPanel pane = new JPanel();
         pane.setLayout(new GridLayout(1, 1, 15, 15));
         pane.add(news);
         setContentPane(pane);
         setVisible(true);
         new Thread(news).run();      // This makes the text scroll. Comment it out to check.
     }

     public static void main(String[] arguments) {

        Authors app = new Authors();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
}  
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

jason_x1Author Commented:
Thanks zzynx that works but I tried adding Authors2 to my JInternalFrame but it wont show why ?
If I click my about menu its freezing and wont show. What do I do thanks ?
0
petmagdyCommented:
use timer instead, try this:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

package forEE;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Authors extends JPanel implements ActionListener {
    String[] headlines = {

    " This program was made by Computer Wizard's :", "",
            "                Jason Dean           ", };

    int y = 76;

    int i = 0;

    Timer tim;

    public void scroll() {
        /*
         * while (true) { y = y - 1;
         *
         * if (y < -650) y = 76;
         *
         * repaint();
         *
         * try { Thread.sleep(30); } catch (InterruptedException e) { } }
         */
        tim = new Timer(300, this);
        tim.start();

    }

    public void paintComponent(Graphics comp) {

        Graphics2D comp2D = (Graphics2D) comp;
        Font type = new Font("monospaced", Font.BOLD, 14);
        comp2D.setFont(type);
        comp2D.setBackground(Color.black);
        comp2D.fillRect(0, 0, getSize().width, getSize().height);
        comp2D.setColor(Color.white);
        //                  for (int i = 0; i < headlines.length; i++)
        comp2D.drawString(headlines[i], 5, y + (20 * i));
    }

    /*
     * (non-Javadoc)
     *
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent arg0) {

        // TODO Auto-generated method stub

        if (i < headlines.length - 1) {
            i++;
        } else {
            i = 0;
        }
        repaint();
        System.out.println("tick");
    }

    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }

    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    public Dimension getMaximumSize() {
        return getPreferredSize();
    }

    protected void finalize() {
        if (tim != null) {
            tim.stop();
            tim = null;
        }
    }

}

/////////////////////////////////////////////////////////////////////////////////

import javax.swing.JFrame;

public class AuthorsFrame extends JFrame {

    protected Authors authers = new Authors();

    public void scroll() {
        authers.scroll();
    }

    public AuthorsFrame() {
        super("Authers demo");
        getContentPane().add(authers);
    }

    public static void main(String args[]) {
        AuthorsFrame mainFrame = new AuthorsFrame();
        mainFrame.pack();
        mainFrame.setVisible(true);
        mainFrame.scroll();
        while (true) {

        }
    }
}
0
zzynxSoftware engineerCommented:
The previous works, but the thread responsible for the scrolling isn't closed when the app is closed.
So, the better code is:

/*
 * Authors2.java
 *
 */

import java.awt.*;
import javax.swing.*;

public class Authors2 extends JPanel implements Runnable {

     private boolean stop = false;                                             // <<<<< added
     String[] headlines = {

     " This program was made by Computer Wizard's :", "",
     "                Jason Dean           ", };

     int y = 76;
     
     public void stopIt() {             // <<<<< added
         stop = true;
     }

     public void run() {

          while (!stop) {         // <<<<< changed
              y = y - 1;

              if (y < -650)
              y = 76;

              repaint();

              try {
              Thread.sleep(30);
              } catch (InterruptedException e) {
              }
          }
     }

     public void paintComponent(Graphics comp) {

          Graphics2D comp2D = (Graphics2D) comp;
          Font type = new Font("monospaced", Font.BOLD, 14);
          comp2D.setFont(type);
          comp2D.setBackground(Color.black);
          comp2D.fillRect(0, 0, getSize().width, getSize().height);
          comp2D.setColor(Color.white);
          for (int i = 0; i < headlines.length; i++)
          comp2D.drawString(headlines[i], 5, y + (20 * i));
     }
}

and

/*
 * Authors.java
 *
 */

package q;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Authors extends JFrame {

     Authors2 news = new Authors2();
     private Thread scroller = new Thread(news);       // <<<<< added

     public Authors() {
         super("Authors . . .");
         setSize(400, 300);
         JPanel pane = new JPanel();
         pane.setLayout(new GridLayout(1, 1, 15, 15));
         pane.add(news);
         setContentPane(pane);
         setVisible(true);
         scroller.start();                         // <<<<< changed
     }
     
     public void stopScrollThread() {  // <<<<< added
         news.stopIt();
     }
     
     public static void main(String[] arguments) {
        final Authors app = new Authors();
        app.addWindowListener(new WindowAdapter() {       // <<<<< added
             public void windowClosing(WindowEvent e) {
                 app.stopScrollThread();
             }
        });
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
Didn't see your last post when posting mine.

>> I tried adding Authors2 to my JInternalFrame but it wont show why?
Could you post the relevant code?
0
petmagdyCommented:
actually the while loop in the main method in my code is not neccessary
0
jason_x1Author Commented:
> Could you post the relevant code?

No need actually your 2nd post solved it. Thanks to both of you. You guys are genius.
0
zzynxSoftware engineerCommented:
Thanks for accepting.

>> You guys are genius
Well, almost ;°)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.