从ChangeListener方法更改ArrayList中所有项的速度

时间:2016-05-12 18:54:46

标签: java jframe

我是java新手,我正在寻求帮助。我遇到的问题是我无法在主类中更改不同类中的速度变量。

我有setter / getter方法,但我遇到的问题是决定哪个"对象"解决setter / getter方法。

滑块用于更改ArrayList中所有海龟的速度。如果我可以得到一些帮助,这将是伟大的。除了速度变化之外,ChangeListener方法的其余部分也可以工作。

主类:



import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class TurtleProgram
{
	private JFrame frame;
	private Canvas canvas;
	private JPanel lowerPanel;
	private JButton addTurtleButton;
	private ArrayList<DynamicTurtle> turtles;
	private JSlider turtleSpeed;
	private int oldSliderState = 100;
	
	public static void main(String[] args)
	{
		new TurtleProgram();
	}

	public TurtleProgram()
	{
		frame = new JFrame();
		canvas = new Canvas();
		lowerPanel = new JPanel();
		lowerPanel.setLayout(new FlowLayout());

		DynamicTurtle referenceTurtle = new DynamicTurtle(canvas , new CartesianCoordinate(400,300));
		
		turtles = new ArrayList<DynamicTurtle>();
		turtles.add(new RandomTurtleB(canvas, 400, 300));		

		addTurtleButton = new JButton("Add A Turtle?");
		lowerPanel.add(addTurtleButton);

		turtleSpeed = new JSlider(0, 100, 100);
		lowerPanel.add(turtleSpeed);

		frame.setTitle("Welcome to Button Simulator!");
		frame.setSize(800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		frame.setVisible(true);		
		frame.add(lowerPanel, BorderLayout.PAGE_END);
		frame.add(canvas);

		addTurtleButton.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent event) 
			{
				turtles.add(new RandomTurtleB(canvas, 400, 300));
			}
		} );

		turtleSpeed.addChangeListener( new ChangeListener()
		{
			public void stateChanged(ChangeEvent event) 
			{
				int currentSliderState = turtleSpeed.getValue();
				int currentSpeed = (turtles.get(i)).getSpeed(); //HERE I CANNOT USE THE TURTLES IN THE ARRAYLIST TO SET SPEED

				if(currentSliderState < oldSliderState)
				{
					System.out.println("is less");
					(turtles.get(i)).setSpeed(currentSpeed - 100); //DECREASES ALL TURTLES SPEED
				}

				else if(currentSliderState > oldSliderState)
				{
					System.out.println("is more");
					(turtles.get(i)).setSpeed(currentSpeed + 100); //INCREASES ALL TURTLES SPEED
				}

				oldSliderState = (turtles.get(i)).getValue();				
			}
		} );		

		gameLoop();
	}

	private void gameLoop() 
	{
		int deltaTime = 20;
		
		while(true)
		{
			for (int i = 0; i < turtles.size(); i++)
			{
				(turtles.get(i)).unDrawTurtle();				
				(turtles.get(i)).wrapPosition((turtles.get(i)).getPositionX(), (turtles.get(i)).getPositionY());
			}

			for (int i = 0; i < turtles.size(); i++)
			{
				(turtles.get(i)).update(1000);	
			}

			for (int i = 0; i < turtles.size(); i++)
			{
				(turtles.get(i)).drawTurtle();				
			}
		
		Utils.pause(deltaTime/2);
		}	
	}	
}
&#13;
&#13;
&#13;

包含setter / getter方法的类:

&#13;
&#13;
class DynamicTurtle extends Turtle
{
	private int speed = 100;
	private int time;
	private double xPos, yPos;
	private CartesianCoordinate myLocation;
	private int Angle = 0;

	DynamicTurtle(Canvas canvas, CartesianCoordinate initLocation)
	{
		super(canvas, initLocation);		
	}

	DynamicTurtle(Canvas canvas, double xPosition, double yPosition) //THE PROBLEM CONSTRUCTOR 
	{
		super(canvas, new CartesianCoordinate(xPosition, yPosition));		
	}

	public int getSpeed()
	{
		return this.speed;
	}

	public void setSpeed(int speed)
	{
		this.speed = speed;
	}

	public void update(int time)
	{
		this.move((speed*100/time));	
		canvas.removeMostRecentLine();
	}
}
&#13;
&#13;
&#13;

谢谢你提前给我的帮助,我没有从Main中删除代码(我意识到它很长),所以你可以看到我在处理主要的ArrayLists方面的逻辑。我知道我不能用这个&#34;得到(i)&#34;。欢呼声。

1 个答案:

答案 0 :(得分:0)

ChangeListener课程内,您可以使用turtles访问该TurtleProgram.this.turtles变量。 TurtleProgram.this会为您提供构建TurtleProgram的{​​{1}}的封闭实例。

但是,正如Savior Self在评论中所说,如果不同的海龟永远不会有相同的速度,只需将速度变量及其get / set方法设为静态。