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 2 of 2

Thread: Having two functional "Rectangle" in same JFrame

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having two functional "Rectangle" in same JFrame

    I am here trying to make a Snake-Food game. In this game Snake tries to eat as much food as he can. It is just a prototype. At the onset I added a Rectangle (emulating Snake) into the JFrame, it is functioning properly, i.e. when I press Up, Down, etc buttons. the rectangle moves to that direction. Then I tried adding a small Rectangle(emulating Food) into the JFrame but it was visible nowhere. I also tried using Box layout to satisfy my need but it bifurcates whole JFrame to give half to Snake and half to Food, having no relation at all. Please suggest what can I do to avoid this mess and how to add relation between Snake and Food. Snake must eat the food, but in my program as Snake reaches near the food, it seems as if it's going behind any surface and disappears inches away from food.

    Main Snake Frame where I am trying to add those two Rectangles

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
     
    public class SnakeFrame extends JFrame implements Runnable
    	{
    		Snake snk ;
    		Increase i ;
    		Decrease d ;
    		int width, height ;
    		boolean foodPresent ;
    		Random rand ;
    		Food food ;
     
    		SnakeFrame()
    			{
    				setSize(500,500) ;
    				setTitle("Snake") ;
     
    				snk = new Snake(0,0) ;
    				this.add(snk) ;
     
    				foodPresent = false ;
    				rand = new Random() ;
     
    				width = this.getSize().width; 
    				height = this.getSize().height ;
     
    				this.addKeyListener(new KeyAdapter()
    					{
    						public void keyPressed(KeyEvent e)
    							{
    								int keyCode = e.getKeyCode() ;
    								if(keyCode == KeyEvent.VK_LEFT)
    										dec(snk,"Left") ;
    								if(keyCode == KeyEvent.VK_RIGHT)
    										inc(snk,"Right") ;
    								if(keyCode == KeyEvent.VK_DOWN)
    										inc(snk,"Down") ;
    								if(keyCode == KeyEvent.VK_UP)
    									    dec(snk,"Up") ;	
    							}
    					}) ;
     
    				Thread t = new Thread(this) ;
    				t.start() ;
    			}
     
    		public void interrupt()
    			{
    				if(i != null)
    						i.stop() ; 
    				if(d != null)
    					    d.stop() ;
    			}
     
    		public void inc(Snake x, String direction)
    			{
    				interrupt() ;
    				i = new Increase(x,direction, width, height) ;
    				i.start() ;
    			}
    		public void dec(Snake x, String direction)
    			{
    				interrupt() ;
    				d = new Decrease(x,direction, width, height) ;
    				d.start() ;
    			}
    		public void run()
    			{
    				int height = this.getSize().height ;
    				inc(snk,"Right");
    				while(true) 
    					{
    						if(foodPresent == false)
    							{
    								int x = rand.nextInt(width) ;
    								int y = rand.nextInt(height) ;
    								food = new Food(x,y) ;
    								food.setSize(25,25) ;
    								this.add(food) ;
    								food.repaint() ;
    								foodPresent = true ;
    							}
    						int gx = snk.x ;
    						int gy = snk.y ;
    						if(gx == food.x && gy == food.y)
    							this.remove(food) ;
    						snk.repaint() ;
    						try
    							{
    								Thread.sleep(5) ;
    							}
    						catch(InterruptedException e)
    							{}
    						validate() ;
    					}
    			}
    		public static void main(String[] args)	
    			{
    				new SnakeFrame().setVisible(true) ;
    			}
    	}

    Snake Program

    import java.awt.*;
    import javax.swing.*;
     
    public class Snake extends JPanel
    	{
    		int x, y ;
    		Snake(int x, int y)
    		 {
    			this.x = x ;
    			this.y = y ;
    		 }
     
    		public void paintComponent(Graphics g)
    			{
    				super.paintComponent(g) ;
    				g.fillRect(x,y,50,50); 
    			}
    	}

    Food Program

    import java.awt.*;
    import javax.swing.*;
     
    public class Food extends JPanel
    	{
    		int x, y ;
    		Food(int x, int y)
    		 {
    			this.x = x ;
    			this.y = y ;
    		 }
     
    		public void paintComponent(Graphics g)
    			{
    				super.paintComponent(g) ;
    				g.fillRect(x,y,25,25); 
    			}
    	}

    Increase Coordinates(Thread)
    import java.awt.*;
    import javax.swing.*;
     
    public class Increase extends Thread
    	{
    		Snake t ;
    		String direction ;
    		int width, height ;
    		Increase(Snake t, String direction, int width, int height)
    			{
    				this.t = t ;
    				this.direction = direction ;
    				this.width = width ;
    				this.height = height ;
    			}
    		public void run()
    			{
    				while(true)
    					{
    						if(direction.equals("Right"))
    							{   
    								if(t.x >=width-50)
    									t.x = -20 ;
    								t.x += 1 ;
    							}
    						else if(direction.equals("Down"))
    							{
    								if(t.y >= height-50)
    									t.y = -20 ;
    								t.y += 1;
    							}
     
    						try
    							{
    								Thread.sleep(5) ;
    							}
    						catch(InterruptedException e)
    							{}
    					}
    			}
    	}

    Decrease Coordinates(Thread)
    public class Decrease extends Thread
    	{
    		Snake t ;
    		String direction ;
    		int width, height ;
    		Decrease(Snake t, String direction, int width, int height)
    			{
    				this.t = t ;
    				this.direction = direction ;
    				this.width = width ;
    				this.height = height ;
    			}
    		public void run()
    			{
    				while(true)
    					{
    						if(direction.equals("Up"))
    							{   
    								if(t.y <= 0)
    									t.y = height ;
    								t.y -= 1 ;
    							}
    						else if(direction.equals("Left"))
    							{
    								if(t.x <= 0)
    									t.x = width;
    								t.x -= 1;
    							}
     
    						try
    							{
    								Thread.sleep(5) ;
    							}
    						catch(InterruptedException e)
    							{}
    					}
    			}
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having two functional "Rectangle" in same JFrame

    Start by doing some debugging. Add some println statements to the code to show you what it is doing.
    For example in the paintComponent() methods print out where the drawing is being done and where the component is located.

    A problem could be the use of components for the shapes. Consider where the layout manager is putting those components.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to put in a "new" image in ImageIcon JFrame ?
    By craigjlner in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 29th, 2013, 03:17 PM
  2. What's the difference between "import.javax.swing*" and JFrame inheritance
    By Johnny Bravo in forum Object Oriented Programming
    Replies: 5
    Last Post: August 14th, 2012, 09:28 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM