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!