hello this is my first time posting so sorry if I make any errors, im currently trying to ad a sliding movement to a tile game im trying to do, the tiles currently move but they move in a jumping motion, ive been trying to use the coordintes of the rectangles i use but i have had no succes. I belive it should be done using the coordinates so i can also use the drawSting method to put numbers on top of all the rectangles and make the numbers slide at the same times as the rectangles slide. This is what i have so far. Any help, tips i would appriciate.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class another extends JPanel implements ActionListener, KeyListener {

	// ======================================x instance Variables
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Timer t = new Timer(5, this);

	private static int ROWS;
	private static int COLS;
	
	public boolean rlegal, llegal, ulegal, dlegal;

	public int x = 1, y = 1, c1 = 0, r1 = 0;
	public double velx = 0, vely = 0;
	public Rectangle[][] board;
	private static final int CELL_SIZE = 84; // Pixels

	// ========================================x Constructor Class
	/**
	 * Constructor Class for the GUI class.
	 * 
	 * @param rows
	 * @param columns
	 * */
	public another(int number) {
		ROWS = number;
		COLS = number;
		board = new Rectangle[ROWS][COLS];

		t.start();
		addKeyListener(this);
		setFocusable(true);
		setFocusTraversalKeysEnabled(false);

		// This 2 for loops store a rectangle in each element of the 2D array.
		for (int r = 0; r < ROWS; r++) {
			for (int c = 0; c < COLS; c++) {
				board[r][c] = new Rectangle(r * CELL_SIZE + 2, c * CELL_SIZE
						+ 2, CELL_SIZE - 4, CELL_SIZE - 4);

				if (r == ROWS - 1 && c == COLS - 1) {
					board[r][c] = new Rectangle();
				}
			}
		}
	}

	// =======================================x method paintComponent

	/**
	 * Paint Component used to update any changes in the board.
	 * */
	public void paintComponent(Graphics g) {

		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;

		for (int r = 0; r < ROWS; r++) {
			for (int c = 0; c < COLS; c++) {

				g2.setColor(Color.gray);
				g2.fill(board[r][c]);
			}
		}
	}// end paintComponent

	// ===============================================x KeyEvent Methods
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

		int code = arg0.getKeyCode();

		if (code == KeyEvent.VK_UP) {
			Ulegal();
		}

		else if (code == KeyEvent.VK_DOWN) {
			Dlegal();
		}

		else if (code == KeyEvent.VK_RIGHT) {
			Rlegal();
		}

		else if (code == KeyEvent.VK_LEFT) {
			Llegal();
		}

	}

	// ====================================================x KeyReleased Method

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	// =====================================================x KeyTyped Method
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	// =====================================================x Action Performed
	// Method
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
		if(rlegal)
		{
			for(int i = 0; i < 84; i++)
			{
				board[r1][c1].translate(1, 0);
				repaint();
			}
			board[r1 + 1][c1] = board[r1][c1];
			board[r1][c1] = new Rectangle();
			rlegal = false;
		}

		if(dlegal)
		{
			for(int i = 0; i < 84; i++)
			{
				board[r1][c1].translate(0, 1);
				repaint();
			}
			board[r1][c1 + 1] = board[r1][c1];
			board[r1][c1] = new Rectangle();
			dlegal = false;
		}
		
		if(llegal)
		{
			for(int i = 0; i < 84; i++)
			{
				board[r1][c1].translate(-1, 0);
				repaint();
			}
			board[r1 - 1][c1] = board[r1][c1];
			board[r1][c1] = new Rectangle();
			llegal = false;
		}
		
		if(ulegal)
		{
			for(int i = 0; i < 84; i++)
			{
				board[r1][c1].translate(0, -1);
				repaint();
			}
			board[r1][c1 - 1] = board[r1][c1];
			board[r1][c1] = new Rectangle();
			ulegal = false;
		}

		repaint();
	}

	// ========================================================x Legal Methods

	public void Rlegal() {
		rlegal = false;
		
		for (int r = 1; r < ROWS; r++) {
			for (int c = 0; c < COLS; c++) {
				if (board[r][c].isEmpty()) {
					r1 = r - 1;
					c1 = c;
					rlegal = true;
				}
			}
		}
	}

	public void Llegal() {
		llegal = false;
		
		for (int r = 0; r < ROWS - 1; r++) {
			for (int c = 0; c < COLS; c++) {
				if (board[r][c].isEmpty()) {
					r1 = r + 1;
					c1 = c;
					llegal = true;
				}
			}
		}
	}

	public void Ulegal() {
		ulegal = false;
		
		for (int r = 0; r < ROWS; r++) {
			for (int c = 0; c < COLS - 1; c++) {
				if (board[r][c].isEmpty()) {
					r1 = r;
					c1 = c + 1;
					ulegal = true;
				}
			}
		}
	}

	public void Dlegal() {
		dlegal = false;
		
		for (int r = 0; r < ROWS; r++) {
			for (int c = 1; c < COLS; c++) {
				if (board[r][c].isEmpty()) {
					r1 = r;
					c1 = c - 1;
					dlegal = true;
				}
			}
		}
	}

	// ==================================================x

	public void up() {
		vely = -1.0;
		velx = 0.0;
	}

	public void down() {
		vely = 1.0;
		velx = 0;
	}

	public void left() {
		velx = -1.0;
		vely = 0;
	}

	public void right() {
		velx = 1.0;
		vely = 0;
	}

}

I guess this is where you try to slide?

for(int i = 0; i < 84; i++)
{
board[r1][c1].translate(0, -1);
repaint();
}

Here's your problem. That loop is run in your actionPerformed, which means it runs on the Swing Event Dispatch Thread (EDT). Swing is single-threaded, so there will be no other Swing activity (including screen repaints) until your actionPerformed terminates.
When your method returns, Swing then repaints the screen, so you just see your rect in its final position.
The only way to do this kind of animation is with your Timer, so on each call to actionPerformed you do a small translate then return to allow the repaint. That's going to need some status/mode variables that define what's needed each time actionPerformed is called.

Sorry, but can you explain a little bit more, I'm not quite understanding what you mean. Like you said, I only see the final final position.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.