Array Animation

multiple objects from single array

Page 1 of 1

8 Replies - 1248 Views - Last Post: 04 November 2009 - 08:20 PM Rate Topic: -----

#1 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Array Animation

Posted 03 November 2009 - 07:12 PM

i've searched google, and used the search function in this forum and cant quite find what i am looking for...

so,

so far this is the code i have:

public void makeLines(){
		int gap = 5;
		laneMarkers = new Rectangle[13];
		for(int i = 0; i < 13; i ++){
			for(int j = 0; j < 13; j ++){
				gap = j*5;
				if(j == 0){
					gap = 1;
				}
				gap -= 4;
				laneMarkers[i] = new Rectangle(j*30+gap, i*60, 3, 15);
				laneMarkers[i].setBackground(Color.white);
				add(laneMarkers[i], 0);
			}
		}
	}

private class AnimationListener implements ActionListener{
		
		public void actionPerformed (ActionEvent event)
		{		   
			for(Rectangle tmp : laneMarkers){
				for(Rectangle tmp1 : laneMarkers){
					tmp1.setLocation(tmp1.getX(), tmp1.getY() + 5);
					
					if(tmp1.getY() > 600){
						tmp1.setLocation(tmp1.getX(), -60);
					}
				}
			}
	   }
}


Rectangle class simply draws a fillRect with the x,y,w,h that you include in the paramater.
import javax.swing.*;
import java.awt.*;

public class Rectangle extends JComponent  {

	/** post:   getX() == x  and  getY() == y
	 *		  and  getWidth() == w  and getHeight() == h
	 *		  and  getBackground() == Color.black
	 */
	public Rectangle(int x, int y, int w, int h)  {
		super();
		setBounds(x, y, w, h);
		setBackground(Color.black);
	}

	/** post:   this method draws a filled Rectangle
	 *		  and  the upper left corner is (getX(), getY()) 
	 *		  and  the rectangle's dimensions are getWidth() and getHeight()
	 *		  and  the rectangle's color is getBackground()
	 */
	public void paint(Graphics g)  {
		g.setColor( getBackground() );
		g.fillRect(0, 0, getWidth()-1, getHeight()-1);
		paintChildren(g);
   }

}



what i am trying to do, is create rows and columns of these rectangles, and then they should all move down the screen with the timer. however, only the last column of rectangles that is created is moving.

i hope this makes sense...

thankyou in advance.

This post has been edited by Chatterbox: 03 November 2009 - 07:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array Animation

#2 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Array Animation

Posted 03 November 2009 - 07:19 PM

Why don't you just use a GridLayout?
Was This Post Helpful? 0
  • +
  • -

#3 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Array Animation

Posted 03 November 2009 - 07:27 PM

View Posttheautokustomizer, on 3 Nov, 2009 - 06:19 PM, said:

Why don't you just use a GridLayout?


because i have other animations going on at the same which wouldnt fit into a grid layout.
Was This Post Helpful? 0
  • +
  • -

#4 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Array Animation

Posted 03 November 2009 - 08:13 PM

I didn't see them...You could try creating a nested loop to force all to work... I don't know if it will work, but just throwing it out there.

This post has been edited by theautokustomizer: 03 November 2009 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Array Animation

Posted 03 November 2009 - 09:23 PM

View Posttheautokustomizer, on 3 Nov, 2009 - 07:13 PM, said:

I didn't see them...You could try creating a nested loop to force all to work... I don't know if it will work, but just throwing it out there.


ahha np.
i got it working.
in the end i just had to make an array of 169 >.<

works now :) thanx for your ideas tho.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Animation

Posted 03 November 2009 - 10:58 PM

this

add(laneMarkers[i], 0);

adds laneMarkers[i] to what ?
Was This Post Helpful? 0
  • +
  • -

#7 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Array Animation

Posted 04 November 2009 - 05:11 PM

View Postpbl, on 3 Nov, 2009 - 09:58 PM, said:

this

add(laneMarkers[i], 0);

adds laneMarkers[i] to what ?


to the extended jpanel. sorry, didnt specify.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: Array Animation

Posted 04 November 2009 - 07:45 PM

This looked like fun. I agree with others that using layout managers is preferable to attempting to set hard caps.

So, as a proof of concept and maybe to offer some ideas:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class RangeBar extends JComponent  {
	private static final Color bgColor = Color.black;
	private static final Color borderColor = Color.blue;
	private static final Color activeColor = Color.white;
	private int value, min, max, range;
	
	public RangeBar(int min, int max) {
		super();
		this.min = min;
		this.max = max;
		this.range = (max-min) + min;
		setValue(min);
	}
	
	public void setValue(int value) { 
		this.value = value - min;
		repaint();
	}
	
	public int getValue() { return this.value + min; }

	public void paint(Graphics g)  {
		int w = getWidth()-1, h = getHeight()-1;
		g.setColor(bgColor);
		g.fillRect(0, 0, w, h);
		g.setColor(borderColor);
		g.drawRect(0, 0, w, h);
		g.setColor(activeColor);
		int h2 = (max-value) * (h/this.range);
		g.fillRect(0, h2, w, h);
	}
}


class ArrayAnimGroup extends JPanel {
	private RangeBar[] bars;
	
	public ArrayAnimGroup(int barCount, int minValue, int maxValue) {
		java.util.Random rnd = new java.util.Random();
		setLayout(new GridLayout(0,barCount));
		bars = new RangeBar[barCount];
		for(int i=0; i<barCount; i++) {
			bars[i] = new RangeBar(minValue, maxValue);
			this.add(bars[i]);
		}
	}
	public int getBarCount() { return bars.length; }
	public void setValue(int index, int value) { bars[index].setValue(value); }
	public int getValue(int index) { return bars[index].getValue(); }
}


public class ArrayAnim extends JFrame {
	private static final int BAR_COUNT = 20;
	private static final int BAR_MIN = 1;
	private static final int BAR_MAX = 64;
	private ArrayAnimGroup arrayPanel;
	private JButton bubbleButton;
	private java.util.Random rnd = new java.util.Random();
	

	public ArrayAnim() {
		arrayPanel = new ArrayAnimGroup(BAR_COUNT, BAR_MIN, BAR_MAX);
		
		setLayout( new BorderLayout() );
		add(arrayPanel, BorderLayout.CENTER);
		
		bubbleButton = new JButton("Bubble"); 
		bubbleButton.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) { 
				bubbleButton_actionPerformed(e); 
			} 
		}); 
		add(bubbleButton, BorderLayout.SOUTH);

		setDefaultCloseOperation(EXIT_ON_CLOSE);          
		setSize(400,300);
		randFill();
	}
	
	private int getRandDigit() { return rnd.nextInt(BAR_MAX-BAR_MIN+1)+BAR_MIN; }
	
	private void randFill() {
		for(int i=0; i<arrayPanel.getBarCount(); i++) {
			arrayPanel.setValue(i, getRandDigit());
		}
	}
	
	private void bubbleButton_actionPerformed(ActionEvent e) { 
		for(int i=0; i<arrayPanel.getBarCount()-1; i++) {
			int temp = arrayPanel.getValue(i+1);
			if (temp<arrayPanel.getValue(i)) {
				arrayPanel.setValue(i+1, arrayPanel.getValue(i));
				arrayPanel.setValue(i, temp);
			}
		}
	} 
	

	public static void main(String[] args) {
		ArrayAnim frame = new ArrayAnim();
		frame.setVisible(true);
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#9 Chatterbox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-August 09

Re: Array Animation

Posted 04 November 2009 - 08:20 PM

View Postbaavgai, on 4 Nov, 2009 - 06:45 PM, said:

This looked like fun. I agree with others that using layout managers is preferable to attempting to set hard caps.

So, as a proof of concept and maybe to offer some ideas:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class RangeBar extends JComponent  {
	private static final Color bgColor = Color.black;
	private static final Color borderColor = Color.blue;
	private static final Color activeColor = Color.white;
	private int value, min, max, range;
	
	public RangeBar(int min, int max) {
		super();
		this.min = min;
		this.max = max;
		this.range = (max-min) + min;
		setValue(min);
	}
	
	public void setValue(int value) { 
		this.value = value - min;
		repaint();
	}
	
	public int getValue() { return this.value + min; }

	public void paint(Graphics g)  {
		int w = getWidth()-1, h = getHeight()-1;
		g.setColor(bgColor);
		g.fillRect(0, 0, w, h);
		g.setColor(borderColor);
		g.drawRect(0, 0, w, h);
		g.setColor(activeColor);
		int h2 = (max-value) * (h/this.range);
		g.fillRect(0, h2, w, h);
	}
}


class ArrayAnimGroup extends JPanel {
	private RangeBar[] bars;
	
	public ArrayAnimGroup(int barCount, int minValue, int maxValue) {
		java.util.Random rnd = new java.util.Random();
		setLayout(new GridLayout(0,barCount));
		bars = new RangeBar[barCount];
		for(int i=0; i<barCount; i++) {
			bars[i] = new RangeBar(minValue, maxValue);
			this.add(bars[i]);
		}
	}
	public int getBarCount() { return bars.length; }
	public void setValue(int index, int value) { bars[index].setValue(value); }
	public int getValue(int index) { return bars[index].getValue(); }
}


public class ArrayAnim extends JFrame {
	private static final int BAR_COUNT = 20;
	private static final int BAR_MIN = 1;
	private static final int BAR_MAX = 64;
	private ArrayAnimGroup arrayPanel;
	private JButton bubbleButton;
	private java.util.Random rnd = new java.util.Random();
	

	public ArrayAnim() {
		arrayPanel = new ArrayAnimGroup(BAR_COUNT, BAR_MIN, BAR_MAX);
		
		setLayout( new BorderLayout() );
		add(arrayPanel, BorderLayout.CENTER);
		
		bubbleButton = new JButton("Bubble"); 
		bubbleButton.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) { 
				bubbleButton_actionPerformed(e); 
			} 
		}); 
		add(bubbleButton, BorderLayout.SOUTH);

		setDefaultCloseOperation(EXIT_ON_CLOSE);          
		setSize(400,300);
		randFill();
	}
	
	private int getRandDigit() { return rnd.nextInt(BAR_MAX-BAR_MIN+1)+BAR_MIN; }
	
	private void randFill() {
		for(int i=0; i<arrayPanel.getBarCount(); i++) {
			arrayPanel.setValue(i, getRandDigit());
		}
	}
	
	private void bubbleButton_actionPerformed(ActionEvent e) { 
		for(int i=0; i<arrayPanel.getBarCount()-1; i++) {
			int temp = arrayPanel.getValue(i+1);
			if (temp<arrayPanel.getValue(i)) {
				arrayPanel.setValue(i+1, arrayPanel.getValue(i));
				arrayPanel.setValue(i, temp);
			}
		}
	} 
	

	public static void main(String[] args) {
		ArrayAnim frame = new ArrayAnim();
		frame.setVisible(true);
	}
	
}



wow! baavgai! :P ty haha. looks like u put a lot of effort into that!
i'll try and incorporate it into my code :) ty
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1