Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / Java 2D / Simulate robot using JPanels on: 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!
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

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

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

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

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

ctomni231 (60 views)
2014-07-18 06:55:21
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!