如何使线圈响应计时器?

问题描述 投票:0回答:1

我正在做一个项目,我需要一些帮助来画我正在画的圆圈以响应我的计时器。我想使用插入的滑块使它们变慢和变快。随附的代码如下。 (我知道我的代码将是一团糟,我正在使用教授提供的一些源代码来在屏幕上显示滑块。我很难让滑块/定时器与我的圈子聊天。)

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());
   }
  }
  }
java swing jframe jpanel
1个回答
0
投票

我想使用插入的滑块使它们变慢和变快。

因此,在我看来,您已经具有使每次计时器启动时每个圆圈移动随机距离的逻辑。

因此,只需使用滑块即可更改计时器触发的时间间隔。然后,每当计时器触发时,您便会根据您的随机距离设置新的圆圈位置。

其他问题:

  1. paintComponent(...)方法仅用于绘画。它不应设置类的属性。例如,您不应在该方法中生成随机值。您无法控制何时调用paintComponent()并且不想随意更改要绘制的对象的属性。

  2. 不要使用数组来保存随机值。首先,您不知道要添加多少个对象,因此您不想设置大小限制。而是使用ArrayList。

  3. 创建一个自定义对象以包含要控制的所有属性。这样,您就可以在自定义对象中绘制每个圆圈的大小/位置/颜色等。

有关使用上述建议的示例,请参见:get width and height of JPanel outside of the class

© www.soinside.com 2019 - 2024. All rights reserved.