0

I am in the middle of doing a project and I need some help with make the circles that I am drawing to respond to my timer. I want to make them go slower and faster using my slider that I put in. Attached is the code below. (I know that my code is going to be a jumbled mess, I am using some source code that our professor provide to get the slider on my screen. I am just having a hard time getting the slider/timer talk to my circles.)

import java.util.ArrayList;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing. *;
import javax.swing.event.*;
import SpeedControlPanel.SlideListener;
public class DotsPanel extends JPanel
{
private final int SIZE = 6;  // radius of each dot
private static final long serialVersionUID = 1L;
private ArrayList<Point> pointList;
private Timer timer;
private JLabel lLabel; 
private JSlider sSlider;
private JPanel pPanel;
private Circle bouncingBall;
int sSliderHt;
private int moveX, moveY;
AnimationListener animationList;
//-----------------------------------------------------------------
//  Constructor: Sets up this panel to listen for mouse events.
//-----------------------------------------------------------------
public DotsPanel()
{
 timer = new Timer(30, new AnimationListener());
 this.setLayout(new BorderLayout());
  bouncingBall = new Circle(SIZE);
  Random rand = new Random();
  pointList = new ArrayList<Point>();
  addMouseListener (new DotsListener());
  addMouseMotionListener( new DotsListener());

  setBackground(Color.black);
  setPreferredSize(new Dimension(500, 300));

  lLabel= new JLabel("Timer Delay");
  lLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

  sSlider = new JSlider(JSlider.HORIZONTAL, 0, 200, 30);
  sSlider.setMajorTickSpacing(40);
  sSlider.setMinorTickSpacing(10);
  sSlider.setPaintTicks(true);
  sSlider.setPaintLabels(true);
  sSlider.setAlignmentX(Component.LEFT_ALIGNMENT);

  sSlider.addChangeListener(new SlideListener());

  pPanel= new JPanel();
  pPanel.add(lLabel);
  pPanel.add(sSlider);

  add(pPanel, BorderLayout.SOUTH);

  animationList = new AnimationListener();
  animationList.timer.start();


  /*int[] xArray = new int[1000];
  int[] yArray = new int[1000];
  for(int i = 0; i < xArray.length; i++)
  {
  xArray[i] = rand.nextInt(10) + 1;
  yArray[i] = rand.nextInt(10) + 1;
  }*/

  }
 //-----------------------------------------------------------------
 //  Draws all of the dots stored in the list.
 //-----------------------------------------------------------------
 public void paintComponent(Graphics page)
 {
 Random rand = new Random();
 int R = rand.nextInt(256);
 int G = rand.nextInt(256);
 int B = rand.nextInt(256);

 super.paintComponent(page);

 page.setColor(new Color(R%255, (G*3)%255, (B+128)%255));

 for (Point spot : pointList)
     //page.fillOval(spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2);
  page.fillOval(spot.x, spot.y, SIZE*2, SIZE*2);

  page.drawString("Count: " + pointList.size(), 5, 15);

  }

   private class AnimationListener implements ActionListener {
   int xRand[];
   int yRand[];
   Random rand;

   public AnimationListener() {
       rand = new Random();

       xRand = new int[1000];
       yRand = new int[1000];

       //Filling random values between 0 to 10
       for (int i = 0; i < 1000; i++) {
           xRand[i] = rand.nextInt(10) + 1;
           yRand[i] = rand.nextInt(10) + 1;
       }
     }


    private Timer timer = new Timer(50, this);

    @Override
    public void actionPerformed(ActionEvent e) {
        //Put here for the bounce off of the wall
        Rectangle window = getBounds();

        for (int i = 0; i < pointList.size(); i++) {
            Point spot = pointList.get(i);
            spot.x += xRand[i];
            spot.y += yRand[i];

            if (spot.x <= 0) {
                xRand[i] = Math.abs(xRand[i]);
            } else if (spot.x >= window.width - (SIZE*2)) {
                xRand[i] = -Math.abs(xRand[i]);
            }

            if (spot.y <= 0) {
                yRand[i] = Math.abs(yRand[i]);
            } else if (spot.y >= window.height - (SIZE*2)) {
                yRand[i] = -Math.abs(yRand[i]);
            }
        }

        bouncingBall.move(moveX, moveY);
        // change direction if ball hits a side
        int x = bouncingBall.getX();
        int y = bouncingBall.getY();
        if (x < 0 || x >= WIDTH - SIZE)
        {
        moveX = moveX * -1;
        }
        if (y <= 0 || y >= HEIGHT - SIZE)
        {
        moveY = moveY * -1;
        }
        sSliderHt =sSlider.getSize().height;
        repaint();

        repaint();

    }

  }

  //*****************************************************************
  //  Represents the listener for mouse events.
  //*****************************************************************
  private class DotsListener implements MouseListener, MouseMotionListener
  {
  //--------------------------------------------------------------
  //  Adds the current point to the list of points and redraws
  //  the panel whenever the mouse button is pressed.
  //--------------------------------------------------------------
  public void mousePressed(MouseEvent event)
  {  
  pointList.add(event.getPoint());
     repaint();
  }

  public void mouseDragged(MouseEvent event) {
   pointList.add(event.getPoint());
  repaint();
  }

  //--------------------------------------------------------------
  //  Provide empty definitions for unused event methods.
  //--------------------------------------------------------------
  public void mouseClicked(MouseEvent event) {}
  public void mouseReleased(MouseEvent event) {}
  public void mouseEntered(MouseEvent event) {}
  public void mouseExited(MouseEvent event) {}
   }

  private class SlideListener implements ChangeListener
  {
  // ------------------------------------------------
  // Called when the state of the slider has changed;
  // resets the delay on the timer.
  // ------------------------------------------------
   public void stateChanged (ChangeEvent event)
   {
    //int sSliderHt =sSlider.getSize().height;
    timer.setDelay(sSlider.getValue());
   }
  }
  }
Slaw
  • 37,820
  • 8
  • 53
  • 80

1 Answers1

0

I want to make them go slower and faster using my slider that I put in.

So it looks to me like you already have logic to make each circle move a random distance each time the Timer fires.

So, just use the slider to change the interval at which the timer fires. Then each time the Timer fires you set the new location of the circles based on your random distance.

Other issues:

  1. The paintComponent(...) method is for painting only. It should NOT set properties of the class. For example you should not be generating random values in the method. You can't control when the paintComponent() is invoked and you don't want to randomly change the properties of the objects you are painting.

  2. Don't use an Array to hold random values. First of all you don't know how many object will be added so you don't want to set a size limit. Instead use an ArrayList.

  3. Create a custom object to contain all the properties you want to control. So you would have the size/location/color etc for each circle you want to paint in a custom object.

See: get width and height of JPanel outside of the class for an example using the above suggestions.

camickr
  • 321,443
  • 19
  • 166
  • 288