Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Anyone familiar with the swing timer?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Anyone familiar with the swing timer?

    Hello everyone.
    I am trying to use the swing timer control to make an image change ever so often.

    this is the code to show the picture
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
     
     
    public class LoadImageApp extends Component {
     
        BufferedImage img;
     
        public void paint(Graphics g) {
            g.drawImage(img, 0, 0, null);
        }
     
        public LoadImageApp() {
           try {
               img = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/image.jpg"));
           } catch (IOException e) {
           }
     
        }
     
        public Dimension getPreferredSize() {
            if (img == null) {
                 return new Dimension(100,100);
            } else {
               return new Dimension(img.getWidth(null), img.getHeight(null));
           }
        }
     
        public static void main(String[] args) {
     
            JFrame f = new JFrame("Load Image Sample");
     
            f.addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
     
            f.add(new LoadImageApp());
            f.pack();
            f.setVisible(true);
        }
    }
    I just don't know where i can stick in a timer to make the picture change at given intervals?
    does anyone know where i might start?
    Last edited by helloworld922; December 27th, 2010 at 01:44 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Anyone familiar with the swing timer?

    First, I'd recommend having your class extend a JPanel (or at least JComponent) and do all your painting by overriding the paintComponent method (make sure to call super.paintComponent). Next, see How to Use Swing Timers, in particular create a ActionListener and change the image the paintComponent paints on each call.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Anyone familiar with the swing timer?

    ok I can follow what you are saying a little, but the examples in the link you sent me mostly show applet use and i am trying to figure out if i can use a timer inside main on an app or if it has to go outside main somehow?
    bare in mind that i am very new at Java

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Anyone familiar with the swing timer?

    public class ExampleClass implements ActionListener{
       private static final REFRESH_RATE_SECS = 10;
       private Timer myTimer;
     
       public ExampleClass(){
          myTimer = new Timer(REFRESH_RATE_SECS*1000, this); 
          myTimer.start();
       }
     
       public static void main(String[] args){
          new ExampleClass();
       }
     
       public void actionPerformed(ActionEvent ae){
          // Code executed every tick of the timer
       }
    }

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Anyone familiar with the swing timer?

    Great example! thanks a million Freaky Chris!

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Anyone familiar with the swing timer?

    You're very welcome!

    Chris

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. Timer Class help
    By Deadbob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 23rd, 2010, 12:18 AM
  3. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  4. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM