0 Replies Latest reply: Oct 20, 2011 12:46 PM by 895523 RSS

    swing timer, setDealy issue in bouncing balls

    895523
      Hi!

      I have a problem with an assignment with bouncing footballs. There are three classes; footBall which picks a football (soccer)from referencelibrary and here I have a swing timer. The next class is footballfield(fotBollsPlan) that also has a timer in it and an arraylist for the footballs. Here I also have a mouseListener, so every time I click on the footballfield I create a new football. And finally a main-class. So, I click on the panel and create footballs that starts to bounc around inside the frame. Here is the code:

      Football class:
      code

      package uppgift2;
      import java.awt.*;

      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.lang.Thread;
      import java.net.URL;
      import javax.swing.*;

      public class FotBoll implements ActionListener{
           private final int storlek;
           private int x;
           private int xfart;
           private int y;
           private int yfart;
           private FotBollsPlan plan;
           
           public FotBoll (int x, int y, int xfart, int yfart, int storlek){

                this.storlek = storlek;
                this.x = x;
                this.y = y;
                this.xfart = xfart;
                this.yfart = yfart;
                plan = new FotBollsPlan();
                Timer t = new Timer(100, this);
                t.start();
           }

           public void actionPerformed(ActionEvent ae){

                     if (x < 0 || x > 700)
                          xfart = - xfart;
                     if (y < 0 || y > 500)
                          yfart = - yfart;
                     x = xfart + x;
                     y = yfart + y;
                     
                }

           public void rita(Graphics g){

                ClassLoader cl = this.getClass().getClassLoader();
                URL url = cl.getResource("fotboll" + storlek + ".png");
                Image im = (new javax.swing.ImageIcon(url)).getImage();

                g.drawImage(im, x, y, null );
                }
      }

      Footballfield class:code
      package uppgift2;
      import java.awt.*;

      import java.lang.Thread;

      import java.awt.event.*;

      import static javax.swing.JLabel.*;
      import javax.swing.*;

      public class FotBollsPlan extends JPanel implements MouseListener, ActionListener{
           
           private java.util.List<FotBoll>fotbollsObjekt;
           private int nrOfBalls;
           private int delay;
           private int loopslot;
           private Timer t;
           private JLabel label = new JLabel();
           
           public FotBollsPlan(){
           
           fotbollsObjekt = new java.util.ArrayList<FotBoll>();
           delay = 50;
           t = new Timer(delay, this);

           t.start();
                
           addMouseListener(this);
           }
           
      public void paintComponent(Graphics g){
                
                super.paintComponent(g);

                
                for (FotBoll fb : fotbollsObjekt){
                     fb.rita(g);
                     }
                System.out.println("rita : " + fotbollsObjekt.size());
           }
           
           public void actionPerformed(ActionEvent ae){
                // An ActionEvent from the timer so we let the timer repaint
                
                loopslot++;
                System.out.println("loopslot: " + loopslot);
                
                if(loopslot == 50){
                     //t.stop();
                     delay = 3000;
                     t.setDelay(delay);
                     t.restart();
                     System.out.println("DELAY: " + t.getDelay());
                     repaint();
                     loopslot = 0;
                     System.out.println("XXXX - if sats: " + loopslot);
                     //t.stop();
                     }
                     if(delay == 3000){
                          delay = 50;
                          }
                     t.setDelay(delay);
                     
                     
                     t.restart();
                     System.out.println("DELAY: " + t.getDelay());
                     //System.out.println("InitialDELAY: " + t.getInitialDelay());
                     repaint();
           }
           
           public void mouseClicked(MouseEvent me){
                
                int xfart = (int)(Math.random()*50) - 2;
                int yfart = (int)(Math.random()*50) - 2;
                int storlek = (int)(Math.random()*10) + 1;
                if (me.getButton()== MouseEvent.BUTTON1){
                     FotBoll fb = new FotBoll(me.getX(), me.getY(),xfart, yfart, storlek);
                     fotbollsObjekt.add(fb);
                     }
           }
           
           public void mouseEntered(MouseEvent me) {}
           public void mouseExited(MouseEvent me) {}
           public void mousePressed(MouseEvent me) {}
           public void mouseReleased(MouseEvent me) {}
      }
      code

      code
      Main:
      package uppgift2;
      import java.awt.*;
      import java.awt.event.*;
      import java.net.URL;

      import javax.swing.*;

      public class FotBollMain extends JFrame {
           private FotBoll fb;
           private FotBollsPlan plan1;
           
      public FotBollMain(){
                
                setTitle("Test av FotBollsPlan");

                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ClassLoader cl = this.getClass().getClassLoader();
                URL url = cl.getResource("fotbollsplan.png");
                          JLabel label = new JLabel(new ImageIcon(url));
                label.setLayout(new GridLayout(1,1));
                
                          setSize(800, 600);
                setLocation(25, 25);

                          plan1 = new FotBollsPlan();
                plan1.add(label);
                          add(plan1, BorderLayout.CENTER);
                
                          setVisible(true);
                
           }          
           
           public static void main(String[] args)
           {
                          FotBollMain test = new FotBollMain();
           }
      }
      code

      Everything works fine when clicking on the panel and the balls starts to bounce around. But in evenly intervalls the panel should "freeze" for a couple of seconds - showing(painting ) all the balls and then go back again bouncing around. I try to setDelay to 3000 ms which means that for this period of time the panel is "frozen" and then setDelay 50 ms, ie the balls should start bouncing around again. But this doesn't work out. This is the actionPerformed method in the class FotBollsPlan. As you understand I've tried different scenarios but I just can't figure out how to get this to work. The way the code works right now is that if I create four balls, they will all be representing an event and I seem to setDelay for every event not the whole Timer. Does anyone understand this?
      By the way, I don't understand the codetags here.

      Anders