Results 1 to 9 of 9
  1. #1
    Rampletero is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default help in drawing tex

    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:
    Java Code:
    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=tiles[row][col].getX();
    			dy = tiles[row][col].getY();
    	        int xdraw = (int)dx;
    	        int ydraw = (int)dy;
    	        g2.setColor(Color.RED);
    			g2.drawString("1", xdraw+30, ydraw+30);
    			}			
    		}	
    	}
    but didnt work. What im trying to create is a 15 number game. This is my complete code so far

    Java 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 static  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];
    		if(ROWS == 3){
    			CELL_SIZE = 80;
    		}
    		if(ROWS == 4){
    			CELL_SIZE = 70;
    		}
    		if(ROWS == 5){
    			CELL_SIZE = 60;
    		}
    		if(ROWS == 6){
    			CELL_SIZE =50 ;
    		}
    		
    
    		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++;
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: help in drawing tex

    didnt work
    Please explain what "didn't work" means. What does the code do and what do you want the code to do?
    If someone compiled and tried to execute the code, what would happen? If it requires user interaction, what does the user need to do to see the problem?

    One problem with the posted code, it is missing a main method.
    Last edited by Norm; 03-10-2012 at 01:08 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: help in drawing tex


  4. #4
    Rampletero is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: help in drawing tex

    When you compile the code, a board apears, where the user moves the tiles in the board with the arrow keys until the tiles are ordered in a certain place. What I am trying to do is add numbers to those tiles that move, and make the numbers move when the tiles corresponding to that number moves.

    Java 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(1, 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;
    	public String[] numbers;
    	
    	private static  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];
    		numbers = new String[ROWS];
    		
    		if(ROWS == 3){
    			CELL_SIZE = 80;
    		}
    		if(ROWS == 4){
    			CELL_SIZE = 70;
    		}
    		if(ROWS == 5){
    			CELL_SIZE = 60;
    		}
    		if(ROWS == 6){
    			CELL_SIZE =50 ;
    		}
    		
    
    		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();
    				}
    			}			
    		}
    		//creates numbers for the tiles
    		for(int i = 1;i<ROWS*ROWS;i++){
    			  int tilenum = i;
    			  int j = 0;
    			  String aString = Integer.toString(tilenum);
    			  numbers[j]=aString;
    			  j++;
    			 
    			   
    		}
    		
    		
    		
    				
    		
    		
    		
    		      
    		      
    		        
    		      
    			
    	}
    
    	// ===========================================================================================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 < ROWS; c++) {
    				
    				g2.setColor(Color.GRAY);
    				g2.fill(board[r][c]);
    				
    				dx = board[r][c].getX();
    				dy = board[r][c].getY();
    				
    		    
    				int xdraw = (int)dx;
    		        int ydraw = (int)dy;
    		        int i=0;
    		        g2.setColor(Color.RED);
    		        if(r==0 && c ==0){
    		        	g2.drawString("", xdraw+30, ydraw+30);
    		        }
    		        g2.setColor(Color.RED);
    				g2.drawString(numbers[i], xdraw+30, ydraw+30);	
    		        i++;
    				
    			
    			}
    		}
    		
    		
    	}// 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++;
    	}
    }
    main method:

    Java Code:
     import java.util.Scanner;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class PuzzleRunner {
    
    	/**
    	 * This is the main class for the PUZZLE GAME
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		// Asks for the number of player
    
    		// Asks for the name of the player
    
    		//String name = JOptionPane.showInputDialog("Player name: ");
    
    		// Asks the user for the amount of ROWS and COLUMNS.
    		String input = JOptionPane
    				.showInputDialog("Enter the amount of ROWS and COLUMNS:\n(Must be BETWEEN 3 AND 6!!)");
    		int number = Integer.parseInt(input);
    
    		// Sets up a frame
    		JFrame frame = new JFrame();
    		frame.setSize(400, 400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    
    		GUI puzzle = new GUI(number);
    		frame.add(puzzle);
    
    	}
    
    }
    If you run the program you will see that im able to put numbers on the tiles but, only one number apears, and the tile on location [0][0] two numbers apear right on top of he other

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: help in drawing tex

    Try debugging the code by adding printlns to the paintComponent method to show where and when it is drawing the numbers.
    You must have a logic problem that is drawing one number over the other.

  6. #6
    Rampletero is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: help in drawing tex

    I tired what you said and i couldnt find the problem but i did find a solution, but still i cant make it to draw numbers from 1-the number of tiles. It only prints the number of tiles there is. I want it to print 1,2,3,4... till the end of tiles but it does 25,25,25,25...

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: help in drawing tex

    Add more printlns to show what the values of the variables are as they change and are used. The print out will show you what the program is doing.

    For testing everyone one should use the same input to the program. Hard code a value in the code for testing. Some thing like this:
    Java Code:
            int number = 4; //Integer.parseInt(input);   always use the same number for testing

  8. #8
    Rampletero is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: help in drawing tex

    Im not having any luck at all =\

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: help in drawing tex

    Where are your println statements? And what are they printing out?
    Here is a place you should put one: in the paintComponent() method just before this line:
    g2.setColor(Color.RED);

    It should print out the values of: i, r, c, xdraw and ydraw

    The print out from that println will show you one of your problems.

Similar Threads

  1. need help for drawing...
    By nice7 in forum New To Java
    Replies: 4
    Last Post: 12-06-2011, 03:34 PM
  2. Drawing an arc
    By berkeleybross in forum Java 2D
    Replies: 10
    Last Post: 12-09-2010, 01:32 AM
  3. Drawing on Jpanel
    By nonabhai in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 04:46 AM
  4. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 12:26 PM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •