Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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 1503 times)
0 Members and 1 Guest are viewing this topic.
Offline lalaandwonder

Innocent Bystander





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

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[j] = new JPanel();
            innerCells[j].setBorder(BorderFactory.createLineBorder(Color.black));
            p.add(innerCells[j]);
            innerCells[j].addKeyListener(this);
            innerCells[j].addMouseMotionListener(handler);
            innerCells[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!
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-09-18 05: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: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2013-09-18 11: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
Online SHC
« Reply #3 - Posted 2013-09-18 11:28:39 »

Use [code] tags to highlight source code.

So

[code]
public void foo()
{
    System.out.println("Foo");
}
[/code]

renders as

1  
2  
3  
4  
public void foo()
{
    System.out.println("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
System.out.println()
function to print text to console. This can give better formatting. So to specify two classes, I would use

Class1.java

1  
2  
3  
4  
public class Class1
{
    // Class methods
}

Class2.java

1  
2  
3  
4  
public class Class2
{
    // Class methods
}

This gives your posts good readability. Good luck.

Offline lcass
« Reply #4 - Posted 2013-09-19 23: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 Wizard


Medals: 75
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #5 - Posted 2013-09-20 06: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?

Check out #JGO on EsperNet IRC! | Check out the MERCury 2D Java Game Library! | Also, Check out My Site
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.

pw (14 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!