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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| 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; 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() { Graphics2D jPanelGraphics = (Graphics2D) mainView.jPanel_GamePanel.getGraphics(); Graphics jPanelAuxGraphic = jPanelGraphics.create(); BufferedImage snakeImage = snake.getSnakeImage(); Image drawingImage = mainView.jPanel_GamePanel.createImage(gamePanelWidth, gamePanelHeight); Graphics2D graphicsImage = (Graphics2D) drawingImage.getGraphics(); graphicsImage.drawImage(snakeImage,0,0,null); jPanelAuxGraphic.drawImage(drawingImage,snake.getPositionX(),snake.getPositionY(),null); } 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); } } |