Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simulate robot using JPanels  (Read 1816 times)
0 Members and 1 Guest are viewing this topic.
Offline lalaandwonder

Innocent Bystander

« Posted 2013-09-18 03:37:04 »

Hi everyone,

I have a code like this:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
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();

        timer = new Timer(25, this);
    //for the keyboard event
    innerCells = new JPanel[20][15];
   HandlerClass handler = new HandlerClass();
    for(int i=0;i<20;i++){
        for(int j=0;j<15;j++){
            innerCells[j] = new JPanel();

    // 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;

   private class HandlerClass implements MouseListener, MouseMotionListener{
      public void mouseClicked(MouseEvent event)
      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()){
                  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");
         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++){
                 System.out.println("Move before"+ " Row:"+ row + " col:"+ col);



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

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


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(){
    public void paintComponent(Graphics g)

   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!
Offline Jimmt
« League of Dukes »

JGO Kernel

Medals: 143
Projects: 4
Exp: 3 years

« Reply #1 - Posted 2013-09-18 03:50:08 »

First, please use code tags Smiley
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).
Offline opiop65

JGO Kernel

Medals: 161
Projects: 7
Exp: 4 years

JumpButton Studios

« Reply #2 - Posted 2013-09-18 09:03:47 »

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 Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2013-09-18 09:28:39 »

Use [code] tags to highlight source code.


public void foo()

renders as

public void foo()

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

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

Renders as

You can use
function to print text to console. This can give better formatting. So to specify two classes, I would use

public class Class1
    // Class methods

public class Class2
    // Class methods

This gives your posts good readability. Good luck.

Offline lcass
« Reply #4 - Posted 2013-09-19 21:35:05 »

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

JGO Kernel

Medals: 125
Projects: 5
Exp: 4 years

// No Comment.

« Reply #5 - Posted 2013-09-20 04:34:26 »

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

You monster!

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

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.


Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (20 views)
2014-12-28 10:39:27

CopyableCougar4 (20 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (116 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!