Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Beginner JPanel Question - Snake Game :P  (Read 1669 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-03-05 15:11:49 »

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;  
 
/**
*
* @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);  
    }  
     
}  
Offline tyeeeee1
« Reply #1 - Posted 2013-03-05 15:16:09 »

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); .
Offline Andre Lopes
« Reply #2 - Posted 2013-03-05 15:26:20 »

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

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  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-03-05 15:51:40 »

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.
Offline Andre Lopes
« Reply #4 - Posted 2013-03-05 16:12:47 »

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!!!!

Tongue
Anyway thanks man! Lets keep rocking.

Java Owns
Offline Andre Lopes
« Reply #5 - Posted 2013-03-05 16:21:01 »

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(); ? 

Tongue Sorry to bother u guys so much, but i new at this graphics part.
Offline Andre Lopes
« Reply #6 - Posted 2013-03-05 17:12:19 »

God damit! I think i made it work.

Eat that java!

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  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
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);
    }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!