Simulate robot using JPanels

Hi everyone,

I have a code like this:

Rob.java:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.;
import java.awt.image.BufferedImage;
import java.awt.
;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.*;

public class example extends JFrame implements ActionListener, KeyListener {
private JPanel [][] innerCells;
private JLabel b;
private Robot rob;
private Timer timer;
private int x;
private int y;
JFrame fr;
int[][] multi;

//create 4 array object
//int [] rob00 = {0,0};
//int [] rob01 = {0,1};
//int [] rob10 = {1,0};
//int [] rob11 = {1,1};


private RobPos rob00;
private RobPos rob01;
private RobPos rob10;
private RobPos rob11;


//rob00 = innerCells[0][0];


public example() throws IOException {

	fr = new JFrame("Final Exams");
	

	fr.setSize(500, 500);
	rob = new Robot();
	
	rob00 = new RobPos();
	rob01 = new RobPos();
	rob10 = new RobPos();
	rob11 = new RobPos();

	
	
	rob00.setTileXRobPos(5);
	rob00.setTileYRobPos(5);
	
	rob01.setTileXRobPos(5);
	rob01.setTileYRobPos(6);
	
	rob10.setTileXRobPos(6);
	rob10.setTileYRobPos(5);
	
	rob11.setTileXRobPos(6);
	rob11.setTileYRobPos(6);
	
	addKeyListener(this);
    timer = new Timer(25, this);
    timer.start();
   
   

//for the keyboard event
p.setFocusable(true);
p.addKeyListener(this);
p.repaint();

innerCells = new JPanel[20][15];

HandlerClass handler = new HandlerClass();

for(int i=0;i<20;i++){
    for(int j=0;j<15;j++){
        innerCells[i][j] = new JPanel();
        innerCells[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
        p.add(innerCells[i][j]);
        innerCells[i][j].addKeyListener(this);
        innerCells[i][j].addMouseMotionListener(handler);
        innerCells[i][j].addMouseListener(handler);

    }
    
}

// assign 0 to each of the grid to show that it is empty
// O means never move before
// 1 means move before
// 2 means obstacles
multi = new int[20][15];
for(int row=0; row<20; row++){
    for(int col=0; col<15; col++){
    	multi[row][col] = 0;
    }
}


innerCells[rob.getTileX()][rob.getTileY()].add(rob.getRob());
fr.add(p);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setVisible(true);
fr.setLocationRelativeTo(null);

}
private class HandlerClass implements MouseListener, MouseMotionListener{
public void mouseClicked(MouseEvent event)
{

	}
	@Override
	public void mousePressed(MouseEvent event)
	{
		int x = 0;
		int y = 0;
	
		int xwidth=0;
		int yheight=0;
		
		int xIndex = 0;
		int yIndex = 0;
		
		for(int row = 0 ; row < 20 ; row++){
			for(int col = 0 ; col < 15; col++)
			{//get the reference of the mouse that was clicked
				if(innerCells[row][col] == event.getSource()){
					innerCells[row][col].setBackground(Color.pink);
					x = innerCells[row][col].getX(); //get the mouse position
					y = innerCells[row][col].getY();
					
					xwidth = innerCells[row][col].getWidth();
				
					
					yheight = innerCells[row][col].getHeight();
				
					//convert mouse coordinates to grid position
					yIndex = (int)Math.floor((x)/xwidth); 
					
					xIndex = (int)Math.floor(y/yheight);
					JLabel a = new JLabel("X");
					innerCells[row][col].add(a);
					innerCells[row][col].revalidate();
				}
			}
		}
		System.out.println("Row :" + xIndex + " Col " + yIndex);
	}
	
	public void mouseReleased(MouseEvent event)
	{
		
	}
	
	public void mouseEntered(MouseEvent event)
	{

	}
	
	public void mouseExited(MouseEvent event)
	{

	}
	
	
	public void mouseDragged(MouseEvent event){

	}
	
	public void mouseMoved(MouseEvent event){
		
	}	
	
		
		
		
}




public void keyPressed(KeyEvent e) {
    int keycode = e.getKeyCode();

    if (keycode == KeyEvent.VK_UP) {
 
       
    
    }
 
    if(keycode == KeyEvent.VK_DOWN){

    }
    
    if (keycode == KeyEvent.VK_LEFT){


    }

    if(keycode == KeyEvent.VK_RIGHT){

    }

    for(int row=0; row<20; row++){
        for(int col=0; col<15; col++){
        	if(multi[row][col]==1){
        		System.out.println("Move before"+ " Row:"+ row + " col:"+ col);
        		innerCells[row][col].setBackground(Color.green);
        		
        		fr.invalidate();
        		fr.validate();
        		fr.repaint();
        	}
        }
        
      
}
    System.out.println("-------");


    
}





	
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

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

}

RobPos.java

package robotmax;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class RobPos extends JPanel{
int tilex=0,tiley=0;

public RobPos(){
	this.setBackground(Color.black);
}

 public void paintComponent(Graphics g) 
 {
     super.paintComponent(g);
     g.setColor(Color.BLACK);
 }

 

public int getTileXRobPos()
{
	return tilex;
}
public int getTileYRobPos()
{
	return tiley;
}

public void setTileXRobPos(int x)
{
	tilex= x;
}
public void setTileYRobPos(int y)
{
	tiley = y;
}


public static void main(String[] args) {
	// TODO Auto-generated method stub

}

}

I need to create a robot simulation with grid map and a robot (which takes up 4 grid) and have to simulate it moving in the map.

My idea originally is:
to create 4 robot class object, each with methods like setTileX, setTileY, getTileX, getTileY for the purpose of getting the row and col of the position of the robot.
And for each object, I wanted to set background as yellow (So total there are 4 grid of yellow on the map simulating the robot), and every time “Down” button is pressed (using key listener), the object will move 1 level down.

Pls advise, thank you!

First, please use code tags :slight_smile:
Second, I don’t really understand your code. RobPos is robot position I assume? Why do you need a separate class for that?
Anyways, let me restate your criteria:

  • robot moving 4 cardinal directions on grid map
  • robot represented by yellow tile
  • 1 move per key press
  • 4 robots

This leads to yet more questions, what is the purpose of the rob00/rob01 array? (also, numbering in binary really doesn’t help readability).

There is just so much wrong with how you create and store the new robot(s?). I really can’t even tell what you’re doing, but you shouldn’t have four separate arrays for the same type of object unless they’re doing something way different than one another. Also, you should be storing the position in the robot’s main class, not a new separate class. And why do you have a array of JPanels? No offense, but I’d recommend starting over and reading a book or something because this code is just scary. Nice start though :slight_smile:

Use [nobbc]

[/nobbc] tags to highlight source code.

So

[tr][td]
[nobbc]

[/nobbc]

public void foo()
{
    System.out.println("Foo");
}

[nobbc]

[/nobbc][/td][/tr]

renders as


public void foo()
{
System.out.println(“Foo”);
}


You can also use the [nobbc][icode][/nobbc] tag to highlight inline code.

[tr][td]

You can use [nobbc][icode][/nobbc]System.out.println()[nobbc][/icode][/nobbc] function to print text to console.

[/td][/tr]

Renders as

You can use [icode]System.out.println()[/icode] function to print text to console. This can give better formatting. So to specify two classes, I would use

[icode]Class1.java[/icode]

public class Class1
{
// Class methods
}


[icode]Class2.java[/icode]

public class Class2
{
// Class methods
}


This gives your posts good readability. Good luck.

plonk it in the code tabs give us a full explanation of what you want it to do and we can help :slight_smile:

Are… Are those curly braces on new lines???

You monster!

Anyways, why are you doing this with JPanels? Couldn’t you use rectangles?