Beginner JPanel Question - Snake Game :P

Hi Huys,
Im new in this Forum. I use it a lot to print stuff so i can study, But now, i have some question that im being unable to figure it out by myself and im going nuts. Im studying how to make games, i have read a dozen of tutorials already and im studying the “killer game java programming” Im trying to do my own simple game: its a snake that moves on the screen when you press the arrows. Thats all. Not even a game actually. Anyway, The thing is, when i move the snake and draw it again on a IMAGE object, and draw in the jPanel, it getts bugged… Somehow it doesnt erase the old one.
Im not using the game loop yet, because im trying to learn how to paint properly.
Im having a lot of difficulty… I have read a lot of tutorials, studied them with friends but still… its being a “prick” to me, this java2D.
All i want to do is to be able to paint my snake in screen and move it without bugs!!!

Anyone have a good tutorial to give me or some advice? Im already full of tutorials here that i keep reading and reading, but still…It didnt work.
Some Guy in stackoverflow forum told me to use BufferedImage in the jPanel, but it doesnt accept it… Only in jFrame, which i did already before. But now im trying to follow the book’s adivice.

Please guys, help me.Im running out of ideas.
I just want to learn the right thing!

package Controllers;  
  
import Objetos.Snake;  
import View.MainView;  
import java.awt.Graphics;  
import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.image.BufferedImage;  
  
/** 
* 
* @author André 
*/  
public class GameUpdate {  
  
    private MainView mainView;  
    private Snake snake;  
    private int gamePanelWidth,gamePanelHeight;  
      
    public GameUpdate(MainView mv)  
    {  
        mainView = mv;  
          
          
        snake = new Snake(10,10);  
          
        gamePanelWidth = mainView.jPanel_GamePanel.getWidth();  
        gamePanelHeight = mainView.jPanel_GamePanel.getHeight();  
          
         
    }  
      
      
    public void Running()  
    {  
        //Pegar o Graphics do JPAnel GAME :  
        Graphics2D jPanelGraphics = (Graphics2D) mainView.jPanel_GamePanel.getGraphics();  
          
        // Criar um Auxliar desse Graphic  
        Graphics jPanelAuxGraphic = jPanelGraphics.create();  
          
        //Carrega a imagem da Snake  
        BufferedImage snakeImage = snake.getSnakeImage();  
          
        //Cria imagem Auxiliar  
        Image drawingImage =  mainView.jPanel_GamePanel.createImage(gamePanelWidth, gamePanelHeight);  
          
         
        //Pega o Graphics da Imagem auxiliar para colocar no jPanel  
        Graphics2D graphicsImage = (Graphics2D) drawingImage.getGraphics();  
          
          
        //Desenha a snake na imagem Auxiliar  
        graphicsImage.drawImage(snakeImage,0,0,null);  
          
          
        //Desenha a Imagem Auxiliar no jPanel_GamePanel  
        jPanelAuxGraphic.drawImage(drawingImage,snake.getPositionX(),snake.getPositionY(),null);  
  
        //jPanelAuxGraphic.dispose();  
        //graphicsImage.dispose();  
          
          
    }  
      
    public void moveSnakeLeft()  
    {  
        snake.setPositionX(snake.getPositionX() - 10);  
          
    }  
      
    public void moveSnakeRight()  
    {  
        snake.setPositionX(snake.getPositionX() + 10);  
    }  
  
    public void moveSnakeUp()  
    {  
        snake.setPositionY(snake.getPositionY()+ 10);  
    }  
      
    public void moveSnakeDown()  
    {  
        snake.setPositionY(snake.getPositionY()- 10);  
    }  
      
}  

In the Running() method, before you draw the snake, try drawing a rectangle the size of the screen in the same color as the background; that should erase the previous snake. As for your problem with the BufferedImage, it should work if you do something like g.drawImage(bufferedImage, x, y, panelToDrawOn); .

Yeah on the brasilian forum i go, a m8 told me to do this :


package Controllers;

import Objetos.Snake;
import View.MainView;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 *
 * @author André
 */
public class GameUpdate {

    private MainView mainView;
    private Snake snake;
    private int gamePanelWidth,gamePanelHeight;
    private Graphics2D graphicGamePanel;  
    
    public GameUpdate(MainView mv)
    {
        mainView = mv;
        snake = new Snake(10,10);
        
        gamePanelWidth = mainView.jPanel_GamePanel.getWidth();
        gamePanelHeight = mainView.jPanel_GamePanel.getHeight();
        
        graphicGamePanel = (Graphics2D) mainView.jPanel_GamePanel.getGraphics();  
       
    }
    
    
    public void Running(){  
  
        graphicGamePanel.drawImage(snake.getSnakeImage(),snake.getPositionX(),snake.getPositionY(),null);  
  
    }  
    
    public void DeleteSnake(){  
          
        graphicGamePanel.clearRect(snake.getPositionX(), snake.getPositionY(), snake.getSnakeImage().getWidth(), snake.getSnakeImage().getHeight());   
        
    }  
    
    
    
    public void moveSnakeLeft()
    {
        DeleteSnake(); 
        snake.setPositionX(snake.getPositionX() - 10);
        
    }
    
    public void moveSnakeRight()
    {
        DeleteSnake(); 
        snake.setPositionX(snake.getPositionX() + 10);
    }
 
    public void moveSnakeUp()
    {
        DeleteSnake(); 
        snake.setPositionY(snake.getPositionY() - 10);
    }
    
    public void moveSnakeDown()
    {
        DeleteSnake(); 
        snake.setPositionY(snake.getPositionY() + 10);
    }
    
}


Which works perfectly… But in the book, i didnt saw that.
It seems that the guy in the book does something like this :

public void paintThisArt(Graphics g)
{
Graphics2D gp = (Graphics2D) g.create();
Image img = createImage(theWidth,theHeight); // of the jpanel

Graphics g = img.getGraphics();
g.drawImage(snakeImage,0,0);

gp.draw(img,0,0);

}

He does something like that it seems. And it makes sense because since the img is created everytime the method is called, it should erase the jPanel, shouldnt it ?
Idk what was wrong…
Do you guys make games like that ?

I want be pro like you guys.

The example you showed from the book seems to be what I was saying to do but I’m not entirely sure, I’m still mostly a noob when it comes to this stuff as well. As far as I’ve seen/heard there is no one way to program everything so one person might do something one way and another person might do it a different way; Just do whatever you think is right and works at the moment and then as you learn just go back and fix it up with your new knowledge.

Oh Ok!
But i really wanted to know why wasnt working… I might print the code and take to some of my teachers.
But i doubt they will have time to help me…

Well,I will keep programming like this then, but im freaking about this.
I want to know what im doing wrong!!!

:stuck_out_tongue:
Anyway thanks man! Lets keep rocking.

Java Owns

OK, i just read somewhere that i must use paintComponent method for the jPanel,instead of doing what i am.
Also, whats the use of setIgnoreRepaint(); ?

:stuck_out_tongue: Sorry to bother u guys so much, but i new at this graphics part.

God damit! I think i made it work.

Eat that java!

package Controllers;

import Objetos.Snake;
import View.MainView;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.ImageCapabilities;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import sun.awt.image.OffScreenImage;

/**
 *
 * @author André
 */
public class GameUpdate {

    private MainView mainView;
    private Snake snake;
    private int gamePanelWidth, gamePanelHeight;
    Graphics2D g;

    public GameUpdate(MainView mv) {
        mainView = mv;


        snake = new Snake(10, 10);

        gamePanelWidth = mainView.jPanel_GamePanel.getWidth();
        gamePanelHeight = mainView.jPanel_GamePanel.getHeight();
        
        g = (Graphics2D) mainView.jPanel_GamePanel.getGraphics();

    }
    
    
    public void Running()
    {
        Graphics2D graphic = (Graphics2D) mainView.jPanel_GamePanel.getGraphics();
        
        
        BufferedImage bi = snake.getSnakeImage();
        
        BufferedImage auxImage = new BufferedImage(gamePanelWidth, gamePanelHeight,BufferedImage.SCALE_DEFAULT);
        Graphics auxGraphics = auxImage.getGraphics();
        auxGraphics.drawImage(bi,snake.getPositionX(),snake.getPositionY(),null);
        
        
        graphic.drawImage(auxImage,0,0,null);
        
        
    
        
    }
    
    
    
/*
    public void Running() {
        //Pegar o Graphics do JPnel GAME :  
        Graphics2D jPanelGraphics = (Graphics2D) mainView.jPanel_GamePanel.getGraphics().create();

        // Criar um Auxliar desse Graphic  
        Graphics jPanelAuxGraphic = jPanelGraphics.create();
        
        //Cria imagem Auxiliar  
        Image drawingImage = new  BufferedImage(gamePanelWidth, gamePanelHeight,BufferedImage.OPAQUE);
        
        //Carrega a imagem da Snake  
        BufferedImage snakeImage = snake.getSnakeImage();



        //Pega o Graphics da Imagem auxiliar para colocar no jPanel  
        Graphics2D graphicsImage = (Graphics2D) drawingImage.getGraphics();


        //Desenha a snake na imagem Auxiliar  
        graphicsImage.drawImage(snakeImage, 0, 0, null);


        //Desenha a Imagem Auxiliar no jPanel_GamePanel  
        jPanelAuxGraphic.drawImage(drawingImage, snake.getPositionX(), snake.getPositionY(), null);

        //jPanelAuxGraphic.dispose();  
        //graphicsImage.dispose();  
        
        mainView.jPanel_GamePanel.getGraphics().dispose();
        jPanelAuxGraphic.dispose();
    }
    */ 

    public void moveSnakeLeft() {
        snake.setPositionX(snake.getPositionX() - 10);

    }

    public void moveSnakeRight() {
        snake.setPositionX(snake.getPositionX() + 10);
    }

    public void moveSnakeUp() {
        snake.setPositionY(snake.getPositionY() - 10);
    }

    public void moveSnakeDown() {
        snake.setPositionY(snake.getPositionY() + 10);
    }
}