[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Animation Question

Posted on 2004-11-18
10
Medium Priority
?
272 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:jason_x1
  • 6
  • 2
  • 2
10 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12622724
The scrolling needs to be done in a separate thread
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12622801
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jason_x1
ID: 12622851
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
 
LVL 13

Assisted Solution

by:petmagdy
petmagdy earned 120 total points
ID: 12622862
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
 
LVL 37

Accepted Solution

by:
zzynx earned 400 total points
ID: 12622868
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12622880
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12622930
actually the while loop in the main method in my code is not neccessary
0
 

Author Comment

by:jason_x1
ID: 12622981
> Could you post the relevant code?

No need actually your 2nd post solved it. Thanks to both of you. You guys are genius.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12623005
Thanks for accepting.

>> You guys are genius
Well, almost ;°)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question