Results 1 to 12 of 12
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default Make animation pause

    Hi, guys! I tried to make animated images moving in order one behind the other. I have made 5 images like that. I would like to make 3 of them to go on 3 information desks one by one, to wait a little and then to disappear. I have posted like this in To make moving ball (Swing / AWT / SWT / JFace forum at JavaRanch) but I didn't got help, so I ask you.

    Here is the code of JPanel (on which is my work drawn):

    Java Code:
      
    package org.me.log;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class LogPanel2 extends JPanel {
    
      // timer delay: equivalent to the Thread.sleep time
      private static final int TIMER_DELAY = 1;
    
      // how far to move in x dimension with each loop of timer
         int DELTA_X = 1;
    
      // the images x & y locations
      private int x2 = 130;
      private int y2 = 330;
      private int y21 = y2-50;
      private int y22=y2-100;
      private int y23=y2-150;
      private int y24=y2-200;
    
      public LogPanel2() {
        try {
        } catch (Exception e) {
          e.printStackTrace();
        }
    
        // create and start the Swing Timer
        new Timer(TIMER_DELAY, new TimerListener()).start();
      }
    
      // all paintComponent does is paint -- nothing else
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawString("1", 42, 20);
        g.drawString("2", 137, 20);
        g.drawString("3", 232, 20);
        g.setColor(new Color (178, 34, 34));
    
        g.fillOval(30, 50, 30, 30);
        g.fillOval(125, 55, 30, 30);
        g.fillOval(220, 50, 30, 30);
    
        setBackground (Color.white);
        g.setColor(new Color (220,220,220));
    
        g.fillRect(130,100,20,250);
        g.setColor(new Color (205,92,92));
           if (y2 <= 330 && y2 >= 85) {
                g.fillOval( x2, y2, 20, 20);
                g.fillOval( x2, y21, 20,20);
                g.fillOval( x2, y22, 20,20);
                g.fillOval( x2, y23, 20,20);
                g.fillOval( x2, y24, 20,20);
    
      }
            }
      private class TimerListener implements ActionListener {
        // is called every TIMER_DELAY ms
        public void actionPerformed(ActionEvent e) {
                y2 -= DELTA_X;
                y21 -= DELTA_X;
                y22 -= DELTA_X;
                y23 -= DELTA_X;
                y24 -= DELTA_X;
    
           if (y2<80){
              y2=330;
           }
               if (y21<80){
              y21=330;
          }
                 if (y22<80){
              y22=330;
          }
                     if (y23<80){
              y23=330;
          }
                         if (y24<80){
              y24=330;
          }
            
    
          // ask JVM to paint this JPanel
          LogPanel2.this.repaint();
        }
      }
    
      // call Swing code in a thread-safe manner
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
          }
        });
      }
    
    }
    I guess I should add Threads, but I don't know how and where.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Hard to tell without an executable program.

  3. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Ok, here it comes:

    Java Code:
    package org.me.log;
    
    import javax.swing.JApplet;
    
    public class LogAplet extends JApplet {
    
        @Override
        public void init() {
            try {
                java.awt.EventQueue.invokeAndWait(new Runnable() {
                    public void run() {
                        initComponents();
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
                }
        }
        // TODO overwrite start(), stop() and destroy() methods
       private void initComponents() {
    
            panel1 = new org.me.log.LogPanel1();
            slider1 = new javax.swing.JSlider();
            slider4 = new javax.swing.JSlider();
            slider2 = new javax.swing.JSlider();
            slider5 = new javax.swing.JSlider();
            slider3 = new javax.swing.JSlider();
            slider6 = new javax.swing.JSlider();
            label1 = new javax.swing.JLabel();
            label2 = new javax.swing.JLabel();
            label3 = new javax.swing.JLabel();
            label4 = new javax.swing.JLabel();
            label5 = new javax.swing.JLabel();
            label6 = new javax.swing.JLabel();
            panel2 = new org.me.log.LogPanel2();
    
            panel1.setBackground(new java.awt.Color(255, 255, 255));
            panel1.setBorder(new javax.swing.border.LineBorder(javax.swing.UIManager.getDefaults().getColor("Slider.foreground"), 2, true));
    
            javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 288, Short.MAX_VALUE)
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 367, Short.MAX_VALUE)
            );
    
            label1.setFont(new java.awt.Font("Tahoma", 1, 14));
            label1.setText("jLabel1");
    
            label2.setFont(new java.awt.Font("Tahoma", 1, 14));
            label2.setText("jLabel2");
    
            label3.setFont(new java.awt.Font("Tahoma", 1, 14));
            label3.setText("jLabel3");
    
            label4.setFont(new java.awt.Font("Tahoma", 1, 14));
            label4.setText("jLabel4");
    
            label5.setFont(new java.awt.Font("Tahoma", 1, 14));
            label5.setText("jLabel5");
    
            label6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            label6.setText("jLabel6");
    
            panel2.setBackground(new java.awt.Color(255, 255, 255));
            panel2.setBorder(new javax.swing.border.LineBorder(javax.swing.UIManager.getDefaults().getColor("Slider.foreground"), 2, true));
    
            javax.swing.GroupLayout panel2Layout = new javax.swing.GroupLayout(panel2);
            panel2.setLayout(panel2Layout);
            panel2Layout.setHorizontalGroup(
                panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 288, Short.MAX_VALUE)
            );
            panel2Layout.setVerticalGroup(
                panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 367, Short.MAX_VALUE)
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(slider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(slider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1)
                        .addComponent(label2)
                        .addComponent(label3)
                        .addComponent(slider3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(label6)
                        .addComponent(label5)
                        .addComponent(label4)
                        .addComponent(slider5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(slider4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(slider6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(panel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(panel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(label1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(slider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(label2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(slider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(label3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(slider3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(label4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(slider4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(label5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(slider5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(label6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(slider6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
        }// </editor-fold>
    
        // Variables declaration
        public javax.swing.JLabel label1;
        public javax.swing.JLabel label2;
        public javax.swing.JLabel label3;
        public javax.swing.JLabel label4;
        public javax.swing.JLabel label5;
        public javax.swing.JLabel label6;
        public javax.swing.JPanel panel1;
        public javax.swing.JPanel panel2;
        static public javax.swing.JSlider slider1;
        static public javax.swing.JSlider slider2;
        static public javax.swing.JSlider slider3;
        static public javax.swing.JSlider slider4;
        static public javax.swing.JSlider slider5;
        static public javax.swing.JSlider slider6;
        // End of variables declaration
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Ok, now I need the code for LogPanel1.

    If I use two LogPanel2 objects, I get a two panels with 3 red dots with a stream of smaller red dots moving up towards the middle dot.
    Now what?

  5. #5
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Sorry, i forgot on LogPanel1. Here it is:

    Java Code:
    package org.me.log;
    
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.Graphics;
    
    public class LogPanel1 extends JPanel {
    
                        static int value1;
                        static int value2;
                        static int value3;
    
      public LogPanel1()
      {
      }
    
        @Override
      public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
    
         g.setColor(Color.blue);
    
         value1=LogAplet.slider1.getValue();
         value2=LogAplet.slider2.getValue();
         value3=LogAplet.slider3.getValue();
    
          g.setColor(Color.BLACK);
    
          g.drawString("1", 42, 20);
          g.drawString("2", 137, 20);
          g.drawString("3", 232, 20);
    
          g.setColor(new Color (220,220,220));
    
          g.fillRect(35,100,20,230);
          g.fillRect(130,100,20,230);
          g.fillRect(225,100,20,230);
    
          g.setColor(new Color (178, 34, 34));
    
          g.fillOval(30, 50, 30, 30);
          g.fillOval(125, 50, 30, 30);
          g.fillOval(220, 50, 30, 30);
    
          g.setColor(new Color (205,92,92));
    
          g.fillOval(35, 250, 20, 20);
          g.fillOval(35, 300, 20, 20);
          g.fillOval(35, 200, 20, 20);
          g.fillOval(35, 150, 20, 20);
          g.fillOval(35, 100, 20, 20);
    
          g.fillOval(130, 250, 20, 20);
          g.fillOval(130, 300, 20, 20);
          g.fillOval(130, 200, 20, 20);
          g.fillOval(130, 150, 20, 20);
          g.fillOval(130, 100, 20, 20);
    
          g.fillOval(225, 250, 20, 20);
          g.fillOval(225, 300, 20, 20);
          g.fillOval(225, 200, 20, 20);
          g.fillOval(225, 150, 20, 20);
          g.fillOval(225, 100, 20, 20);
    
          repaint();
    }
        }
    I forgot it because it isn't very important (for now) for me because I try to make things in LogPanel2 work. LogPanel2 should be a (simple) animation showing people (smaller red balls) which come to information desk (bigger red balls). When they come to the end of waiting line, they should choose free information desk, come to it and make little delay to simulate completing their job. Is it any way to do that?

    Also, I have flickering problem. If you look at the LogPanel2 for 4 or 5 cycles of moving balls, it flickers for a moment (they all desapear and come back).

    How to deal all this?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    LogPanel2 for 4 or 5 cycles of moving balls, it flickers for a moment
    Add some debugging code to see what it happening. For example:
    if (y2 <= 330 && y2 >= 85) {
    g.fillOval( x2, y2, 20, 20);
    g.fillOval( x2, y21, 20,20);
    g.fillOval( x2, y22, 20,20);
    g.fillOval( x2, y23, 20,20);
    g.fillOval( x2, y24, 20,20);

    }else {
    System.out.println("y2= " + y2); // Show the value that fails if test
    }

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The problem with flickering comes down to the way you draw your animation. It draws a picture on the screen, clears the screen and paints again. This is solved by using double buffering. The way you do this, is do all your drawing to an Image, and when all drawing is done, you draw the image on screen, this way you achieve a smoother transition that doesn't flicker.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  8. #8
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Thank you, my friends. The output said that error was at y2's valuer from 80 to 84. I changed the condition in actionPerformed method (test all y values < 85) and the flickering gone :)

    Do you have any idea about my other problem with trying to set and control these ball?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mneskovic View Post
    Do you have any idea about my other problem with trying to set and control these ball?
    Can you describe in greater detail just what effect you're trying to achieve? Also start small. Create a simple app with just one window with an animation and let's work with the smaller simpler code.

  10. #10
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I was trying to make one line of moving drawn balls simulating people moving in line. In front of the line are 3 information desks and they should serve people. Each should come to the free desk. I have succeed to find out how to do it. I wrote (many) if statements and defined position for each of those balls when they come to the end of line.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Couple of suggestions: Put labels on the small red balls so you can track them.
    Slow down the animation rate. TIMER_DELAY = 1; is way too fast. The system can't respond in 1 millisecond (and neither can you). Try a bigger number that will allow you to see what is happening.
    For queuing/servicing display you'll need a class for the servicing desks to include a flag if busy and how long to process this customer.
    You'll need logic to test the desks to see which is free and then to move customer from head of queue to that desk.

    Looks like an interesting project. I didn't have PC GUI when I took Queuing Theory.

  12. #12
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

Similar Threads

  1. is there a way to pause java?
    By zhangster in forum New To Java
    Replies: 4
    Last Post: 04-24-2010, 07:38 PM
  2. How to pause
    By ravjot28 in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-31-2010, 06:09 AM
  3. Make Java Pause
    By FlyNn in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 10:18 AM
  4. pause until JFrame is closed.
    By Tamu in forum Advanced Java
    Replies: 8
    Last Post: 11-30-2008, 09:46 PM
  5. Help with Pause
    By trill in forum Java Applets
    Replies: 2
    Last Post: 09-28-2007, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •