Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to find Object in coordinate system

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to find Object in coordinate system

    Hi2all !!
    Well first for all im new to java and ive trying to get a job in a game-dev company;
    As a test ive got a task to create a puyopuyo game.
    All is goin pretty well with the development, but got some stucks.

    Ok now the question:

    Ive a scene 10px*10px (Example)
    Ive an object with x,y coordinates
    Its located at x=5 , y=5 ;
    Now ive an other object (№2) x=4 , y=5;

    The question is : How to find this object(number 2) ;
    I mean i need a method that will check is there an object in a radius of 1px away from the Object in center;
    Of course assuming that both objects are in one clollection;


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to find Object in coordinate system

    Ok a little bit more explanation;
    The objects have one parameter with some vlaues;
    I need to find objects having same values of a variable and 1px away from each other for example x or y;

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to find Object in coordinate system

    If I understand correctly, there are perhaps dozens of ways to do what you want. You can brute for it - loop through all the objects and find ones with the appropriate values, you could create a Map keyed with a custom object containing those values, you could create an adjacency matrix, you could maintain references to adjacent objects...

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to find Object in coordinate system

    Quote Originally Posted by copeg View Post
    If I understand correctly, there are perhaps dozens of ways to do what you want. You can brute for it - loop through all the objects and find ones with the appropriate values, you could create a Map keyed with a custom object containing those values, you could create an adjacency matrix, you could maintain references to adjacent objects...
    Okay... Here is the code
    Simple PuyoPuyo GAme

    Panel.class
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
     
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    public class Panel extends JPanel{
    	Timer timer1,timer2;
     
    	Timer inputTimer;
     
    	int [][] grid =	new int [12][7];
     
    	int vSpeed=200;
     
    	int dy=0;
     
    	List<Shape> spheres = new ArrayList<Shape>();	
     
    	ArrayList toDelete = new ArrayList<Integer>();
     
    	private boolean spawn = false;
     
    	private boolean removeVertical;
     
    	private boolean removeHorizontal;
     
    	private boolean left;
     
    	private boolean right;	
     
    	int objIndex;
     
    	int SpheresAmount;
     
     
    //----------------------- The panel constructor
    	public Panel ()  
    	{
    		//Init JPANEL 
    		setFocusable(true);
    		setBackground(Color.BLACK);
    		setDoubleBuffered(true);
    		addKeyListener(new MyKeyAdapter());
    		timer1 = new Timer(vSpeed,new ListenerTimer1());
    		timer2 = new Timer (100,new ListenerTimer2());
    		timer1.start();
    		timer2.start();
    		spheres.add(new Shape());
    	}
     
    /* ------------------  Main Method------------------*/
     
    	public void paint (Graphics g)
    	{
    		super.paint(g);
    		Graphics2D g2d= (Graphics2D) g;
    		Iterator<Shape> j = spheres.iterator();
     
    		while (j.hasNext())
    		{
    			Shape s = j.next();	
    			g.drawImage(s.getImage(),s.getY(),s.getX(),null	);
     
    		//-------Border Collision check-------//
    			if (grid[s.getGx()+1][s.getGy()]!=0||s.getGx()==10)
    			{	
    				if (!j.hasNext())
    				{
    					s.setDx(0);
    					grid[s.getGx()][s.getGy()]=s.getColor();
    					spawn=true;
     
    				}			
    			}
    		//-------3 vertical check-------//
    			if (grid[s.getGx()+1][s.getGy()]==s.getColor()&&grid[s.getGx()-1][s.getGy()]==s.getColor())
    			{
    				SpheresAmount=spheres.size();
    					removeVertical=true;
    			}
    		//-------3 horizontal check---
    			if (grid[s.getGx()][s.getGy()+1]==s.getColor()&&grid[s.getGx()][s.getGy()+2]==s.getColor())
    				removeHorizontal=true;
     
     
     
    		//--------END LOOP-------//
    		}
    			if (removeVertical)
    			{
    				deleteVertical();				
    				removeVertical=false;
    			}
     
    			if (removeHorizontal)
    			{
    				deleteHorizontal();
    				removeHorizontal=false;
    			}
     
    			if(spawn)
    			{
    				spheres.add(new Shape());
    				spawn = false;
    			}
    	}
     
     
     
    	private void deleteHorizontal()
    	{
    		Iterator<Shape> j = spheres.iterator();
    		while (j.hasNext()){
    			Shape d = j.next();
     
     
    			if (spheres.size()+2<SpheresAmount){
    				grid[d.getGx()][d.getGy()]=0;
    				grid[d.getGx()][d.getGy()+1]=0;
    				grid[d.getGx()][d.getGy()+2]=0;
    				SpheresAmount=spheres.size();
    			}
    		}
     
    	}
     
     
    	private void deleteVertical() {
    		Iterator<Shape> j = spheres.iterator();
    		while (j.hasNext()){
    			Shape d = j.next();
    			if (grid[d.getGx()+1][d.getGy()]==d.getColor()&&grid[d.getGx()+2][d.getGy()]==d.getColor()){j.remove();}
    			if (grid[d.getGx()-1][d.getGy()]==d.getColor()&&grid[d.getGx()+1][d.getGy()]==d.getColor()){j.remove();}
    			if (grid[d.getGx()-1][d.getGy()]==d.getColor()&&grid[d.getGx()-2][d.getGy()]==d.getColor()){j.remove();}
    			if (spheres.size()+2<SpheresAmount){
     
    				grid[d.getGx()][d.getGy()]=0;
    				grid[d.getGx()+1][d.getGy()]=0;
    				grid[d.getGx()+2][d.getGy()]=0;
    				SpheresAmount=spheres.size();
    			}
     
    		}
     
    	}
     
    	// ---------------- INPUT -------------------//
    	private class MyKeyAdapter extends KeyAdapter
    	{
     
    		public void keyReleased (KeyEvent e){
    			int	key = e.getKeyCode();
    			if(key==KeyEvent.VK_LEFT)left=false;
    			if(key==KeyEvent.VK_RIGHT)right=false;
     
    		}
    		public void keyPressed (KeyEvent e){
     
    			int	key = e.getKeyCode();
    			if(key==KeyEvent.VK_LEFT)left=true;
    			if(key==KeyEvent.VK_RIGHT)right=true;
     
    		}
    	}
     
    	// ------------Action Listener---------------//
    	private class ListenerTimer1 implements ActionListener
    	{
     
    		public void actionPerformed(ActionEvent arg0) 
    		{
    			Iterator <Shape> j  = spheres.iterator();
    			while (j.hasNext())
    			{
    				Shape s = j.next();
    				s.move();
    			}
     
    			repaint();
    			}
     
    	}
    	// ------------Input movement updater-----------//
    	private class ListenerTimer2 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent arg0) 
    		{
     
    			Iterator <Shape> j  = spheres.iterator();
    			while (j.hasNext())
    			{
    				Shape s = j.next();	
    				if (s.dx!=0&&right==true){
    					s.moveRight();repaint();
    				}
    				if (s.dx!=0&&left==true){
    					s.moveLeft();repaint();
    				}
    			}
    		}
     
    	}
    	//-----------------------------------------------//
    }
    Shape.class
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
    import java.util.Random;
     
    import javax.swing.ImageIcon;
     
     
    public class Shape {
     
    int x,y,gx,gy;
    int dx = 32;
    int dy;
    int i;
    int colorCode=1;
     
    Random random = new Random();
    String texture="red.png";
    Image image;
    ImageIcon ii;
     
    boolean  delete ;
     
    /* --------------- Constructor -------------------*/
    	public Shape ()
    	{
    		//colorGen ();
    		ii = new ImageIcon(getClass().getResource(texture));
    		image = ii.getImage();
    	}
     
    	//------The random color generator----
    	public void colorGen ()
    	{
    	int ran = random.nextInt(4);
    		switch (ran){
    		case 0: texture = "red.png";colorCode=1;break;
    		case 1 :texture = "green.png";colorCode=2;break;
    		case 2 : texture = "blue.png";colorCode=3;break;
    		case 3 : texture = "yellow.png";colorCode=4;break;
    		}
    	}
     
    	//-------------Movement-------------
    	public void keyPressed (KeyEvent e){
    	int	key = e.getKeyCode();
    		if(dx!=0&&key==KeyEvent.VK_LEFT)dy=-32;
    		if(dx!=0&&key==KeyEvent.VK_RIGHT)dy=32;
    		System.out.println(dy);
    	}
    	public void keyReleased (KeyEvent e){
    		int	key = e.getKeyCode();
    		if(key==KeyEvent.VK_LEFT)dy=0;
    		if(key==KeyEvent.VK_RIGHT)dy=0;
    	}
    	//-----------------------------------
     
    	public Image getImage ()
    	{
    		return image;
    	}
     
    	public void move () 
    	{ 
    		x+=dx;
    	}
    	public void moveLeft(){
    		y-=32;
    		if (y<=0)y=0;
    	}
    	public void moveRight(){
    		y+=32;
    		if (y>=160)y=160;
    	}
     
    	public void setDx (int dx)
    	{
    		this.dx = dx;
    	}
    	public void setDy (int dy)
    	{
    		this.dy = dy;
    	}
     
    	public void setX(int x)
    	{
    		this.x=x;
    	}
    	public void setY(int y)
    	{
    		this.y=y;
    	}
    	public void setDelete(boolean delete){
    		this.delete=delete;
    	}
    	public boolean getDelete(){
    		return delete;
    	}
    	public int getX(){
    		return x;
    	}
    	public int getY(){
    		return y;
    	}
    	public int getGx(){
    		return gx=x/32;
    	}
    	public int getGy (){
    		return gy=y/32;
    	}
    	public int getColor () {
    		return colorCode;
    	}
     
    }

    Well i know that im doing something terrible wrong but i just cant see it maybe some one can give a fresh look at this situtation or maybe what would be the best aproach in this case to find this spheres.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to find Object in coordinate system

    Quote Originally Posted by Eldarmk2 View Post
    Hi2all !!
    Well first for all im new to java and ive trying to get a job in a game-dev company;
    As a test ive got a task to create a puyopuyo game.
    All is goin pretty well with the development, but got some stucks.

    Ok now the question:

    Ive a scene 10px*10px (Example)
    Ive an object with x,y coordinates
    Its located at x=5 , y=5 ;
    Now ive an other object (№2) x=4 , y=5;

    The question is : ...
    This presents a moral dilemma for us. If this is a test, should you be soliciting help here online? Should we be helping you?

  6. The Following User Says Thank You to Fubarable For This Useful Post:

    copeg (July 9th, 2012)

  7. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to find Object in coordinate system

    Well im allowed to use any source of information to get the test done.
    Im not allowed to copy existing code.

Similar Threads

  1. Clicking a coordinate on my GUI (Not Local Mouse Or Normal Robot.movemouse!)
    By testingjava in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 30th, 2012, 06:28 AM
  2. Having problem to find the User name from a system on a network.
    By call.abhi23 in forum Java Theory & Questions
    Replies: 0
    Last Post: June 22nd, 2012, 07:37 AM
  3. Cannot Find symbol error! On new Object
    By jtrtoday in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 15th, 2012, 01:32 PM
  4. Matcher object .find() method question
    By chronoz13 in forum Java SE APIs
    Replies: 18
    Last Post: September 8th, 2011, 11:18 AM
  5. Replies: 3
    Last Post: May 21st, 2011, 08:47 AM