Results 1 to 2 of 2
  1. #1
    ll Source ll is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Need help moving a shape

    Ok, new to the forum. This question inspired me to sign up, hi everyone.

    I'm trying to make a snake game. I have 4 classes.
    1.SnakeRunner (main)
    2.Enviroment
    3.SnakeDraw
    4.FoodDraw


    The problem is within the SnakeDraw class. I created a test for loop to increment the x values for the x coordinate of the snake rectangle. and then declared the repaint() method
    inherited from the JPanel class. It doesn't make the shape move though. What am I doing wrong here?

    Thanks in advance for any and all help I get.

    SnakeRunner
    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import javax.swing.JFrame;
    
    
    
    
    class snakeRunner
    {
    	  
    	public static void main(String[] args) 
    	{
    	    //Creates Frame 
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocation(600,100);
    	    int length = 700;
    	    int width  = 700;
    	    frame.setSize(length, width);
    	    frame.setVisible(true);
    	    
    	    //Sets background Color 
    	    Container cp = frame.getContentPane();
    	    cp.setBackground(Color.black);
    	    
    	    //Adds Food and Snake shapes to the frame
    	    Enviroment envi = new Enviroment();
    	    frame.getContentPane().add(envi,java.awt.BorderLayout.CENTER);
    	    
       } 
    }
    Enviroment
    Java Code:
    import java.awt.Graphics;
    import javax.swing.JComponent;
    
    
    
    class Enviroment extends JComponent 
    {
    	 
    	public Enviroment()
    	{
          
    	      
    		   
    	
    	}
    	
    	   public void paintComponent(Graphics g)
    	   {
    	       //Draw Food
    		   FoodDraw food = new FoodDraw();
    		   food.drawFood(g);
    		   
    		   
    		   //Draw Snake
    		   SnakeDraw snake = new SnakeDraw();
    		   snake.drawSnake(g);
    		  
    		
    			    
    		   
    	   }
    
    	
    
        
    }
    SnakeDraw
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    class SnakeDraw extends JPanel
    {
    
      public void drawSnake(Graphics g) 
      {
    		int x_coordinate = 550;
    		int y_coordinate = 295;
    		int length       = 12;
    		int width        = 12;
    	    g.drawRect (x_coordinate, y_coordinate, length, width);  
    	    g.setColor(Color.GREEN);
    	    g.fillRect(x_coordinate, y_coordinate, length, width);
    	    
    	    
    	    for(int x = 0; x < 120; x++)
    	    {
    	    	x_coordinate++;
    	    	this.repaint();
    	    }
        
      }
      public void translate(double dx, double dy)
      {
    	      x += dx;
    	      y += dy;
    	     
    	    
      }
      
      private int x;
      private int y;
     
    }
    FoodDraw
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JComponent;
    
    
    class FoodDraw 
    {
    
      public void drawFood(Graphics g) 
      {
    		int x_coordinate = 150;
    	    int y_coordinate = 295;
    		int length       = 12;
    		int width       = 12;
    		g.drawRect (x_coordinate, y_coordinate, length, width);  
    	    g.setColor(Color.pink);
    	    g.fillRect(x_coordinate, y_coordinate, length, width);
        
      }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help moving a shape

    It doesn't make the shape move though.
    Unless I'm missing something you have a method translate() in the SnakeDraw class, but you don't call it at any point.

    -----

    A deeper problem (I think) is the way you create a SnakeDraw instance each time you paint the environment. The whole point about motion is that identity is preserved while location changes. If it were me I would have a Snake class, and an instance of this class would be part of the environment. The Snake class would have a draw() method (your current drawSnake() renamed). With this setup you would have an instance of Snake on which to call the translate() method.

    The same thing applies to food which another part of the environment.

Similar Threads

  1. Need help moving a shape!
    By chrishans in forum AWT / Swing
    Replies: 3
    Last Post: 10-16-2011, 07:17 AM
  2. Replies: 1
    Last Post: 06-08-2011, 10:22 PM
  3. Make shape rounding (moving in circle)
    By mneskovic in forum New To Java
    Replies: 8
    Last Post: 08-17-2010, 03:05 PM
  4. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  5. rezise shape
    By frankenstein in forum Java 2D
    Replies: 5
    Last Post: 07-30-2009, 12:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •