1,105,371 Community Members

help on drawing text

Member Avatar
rampletero
Newbie Poster
3 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I need help on drawing numbers on top of a rectangle matrix, and have them move together with the rectangle they are on. I tried doing this

public void paintComponent(Graphics g) {
	super.paintComponent(g);
	Graphics2D g2 = (Graphics2D) g;  
	for(int row = 0; row < tiles.length; row++) {
		for(int col = 0; col < tiles[row].length; col++) {
			
			
			g2.setColor(Color.YELLOW);
			g2.fill(tiles[row][col]);
			dx=board[row][col].getX();
			dy = board[row][col].getY();
	        int xdraw = (int)dx;
	        int ydraw = (int)dy;
	        g2.setColor(Color.RED);
			g2.drawString("1", xdraw+30, ydraw+30);
			}			
		}	
	}

but didnt have any succes.

here is the complete code:

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

public class GUI extends JPanel implements ActionListener, KeyListener {

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

	Timer t = new Timer(0, this);

	private static int ROWS;
	private static int COLS;

	// These variables are meant to check if a movement is legal or not.
	// The isDone determine when the user can move a square again
	
	public boolean rlegal, llegal, ulegal, dlegal, isDone = true;

	public int x = 1, c1 = 0, r1 = 0;
	public Rectangle[][] board;
	private int[][]Xcoordinates;
	private int[][]Ycoordinates;
	private static final int CELL_SIZE = 84; // Pixels
	private double dx=0,dy=0;

	// ===========================================================================================x Constructor Class
	/**
	 * Constructor Class for the GUI class.
	 * 
	 * @param rows
	 * @param columns
	 * */
	public GUI(int number) {
		//Sets the amount of ROWS and COLUMNS and creates an array depending on the last 2.

		ROWS = number;
		COLS = number;
		board = new Rectangle[ROWS][COLS];
		Xcoordinates = new int [ROWS][COLS];
		Ycoordinates = new int [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;

		// This paints square by square within the array.
		
		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
	
	/**
	 * This method reacts depending on which key the user has pressed.
	 * */
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

		int code = arg0.getKeyCode();

		// The isDone boolean variable checks to see if the square is done with its translation.
		
		if (isDone) {
			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();
			}
		}
	} // end keyPressed

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

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

	}

	// ===========================================================================================x KeyTyped Method

	/**
	 * NOT NEEDED
	 * */
	
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	// ===========================================================================================x Action Performed Method

	/**
	 * This method is to keep updating the board after every movement.
	 * */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

		// This are meant to call the movement methods and repaint the square as
		// it moves.

		if (rlegal) {
			right();
		}

		if (dlegal) {
			down();
		}

		if (llegal) {
			left();
		}

		if (ulegal) {
			up();
		}

		this.repaint();
	}

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

	/**
	 * All of the methods below this point are meant to check if the empty space
	 * is near the square that the user wishes to move.
	 * */
	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;
					isDone = false;
				}
			}
		}
	}

	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;
					isDone = false;
				}
			}
		}
	}

	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;
					isDone = false;
				}
			}
		}
	}

	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;
					isDone = false;
				}
			}
		}
	}

	// ===========================================================================================x Movement Methods

	/**
	 * This methods are called from the actionPerfomed method and they are meant
	 * to move move the desired square.
	 * */

	public void up() {
		if (x <= CELL_SIZE) {
			board[r1][c1].setLocation(board[r1][c1].x, board[r1][c1].y - 1);
		}

		if (x == CELL_SIZE + 1) {
			board[r1][c1 - 1] = board[r1][c1];
			board[r1][c1] = new Rectangle();
			ulegal = false;
			x = 0;
			isDone = true;
		}
		x++;
	}

	public void down() {
		if (x <= CELL_SIZE) {
			board[r1][c1].setLocation(board[r1][c1].x, board[r1][c1].y + 1);
		}

		if (x == CELL_SIZE + 1) {
			board[r1][c1 + 1] = board[r1][c1];
			board[r1][c1] = new Rectangle();
			dlegal = false;
			x = 0;
			isDone = true;
		}
		x++;
	}

	public void left() {
		if (x <= CELL_SIZE) {
			board[r1][c1].setLocation(board[r1][c1].x - 1, board[r1][c1].y);
		}

		if (x == CELL_SIZE + 1) {
			board[r1 - 1][c1] = board[r1][c1];
			board[r1][c1] = new Rectangle();
			llegal = false;
			x = 0;
			isDone = true;
		}
		x++;
	}

	public void right() {

		if (x <= CELL_SIZE) {
			board[r1][c1].setLocation(board[r1][c1].x + 1, board[r1][c1].y);
		}

		if (x == CELL_SIZE + 1) {
			board[r1 + 1][c1] = board[r1][c1];
			board[r1][c1] = new Rectangle();
			rlegal = false;
			x = 0;
			isDone = true;
		}
		x++;
	}
}
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
1
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article