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);
}
}