Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  [Solved]Having problem with drawing  (Read 1310 times)
0 Members and 1 Guest are viewing this topic.
Offline hapsburgabsinth

Senior Newbie





« Posted 2011-11-29 23:41:21 »

So I have made my own game, its a 2d tile game much like pokemon for gameboy.

My problem is that, when I move my player around in my world, the picture is much like jelly or pudding if you will Tongue
It's like the picture isn't updated fast enough, or that the tiles that are drawn after the player (in my loop) isn't updated before the player moves to another tile. this is my code for my drawing board:

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  
package zompocalypseEngine;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

import zompocalypse.Player;



public class GamePanel extends JPanel{

   private BufferedImage backBuffer;
   private int windowHeight;
   private int windowWidth;
   private ZompocalypseGame game;

   public GamePanel(int windowWidth, int windowHeight, ZompocalypseGame sGame){
      setIgnoreRepaint(true);
      this.windowWidth = windowWidth;
      this.windowHeight = windowHeight;
      setVisible(true);
      this.game = sGame;
     
      this.backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
     
      this.backBuffer = new BufferedImage(this.windowWidth, this.windowHeight, BufferedImage.TYPE_INT_RGB);
   }

   public void draw(){
      Graphics2D g = (Graphics2D) getGraphics();
     
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, windowWidth, windowHeight);
     
      g.drawImage(this.backBuffer, 0, 0, this);
      g.dispose();
   }

   public void render() {
      Graphics bbg = this.backBuffer.getGraphics();
      BufferedImage image = null;
     
      Player player = this.game.getTileMap().getPlayer();

      for(int y = 0; y < 24; y++){
         for(int x = 0; x < 42; x++){
            Position position = new Position(x + player.getPosition().getX()-21,y + player.getPosition().getY()-12);
            int yOffset = 0;
            //Draw tiles
           Tile tile = this.game.getTileMap().getTileAt(position);
            if(tile != null){
               image = tile.getImage();
            }else{
               tile = new Tile(position, "WATER");
               image = tile.getImage();
            }

            //Draw items
           Item item = this.game.getTileMap().getItemAt(position);
            if(item != null){
               image = item.getImage();
            }

            //Draw buildings
           Building building = this.game.getTileMap().getBuildingAt(position);
            if(building != null){
               image = building.getImage();
            }

            //Draw units
           Unit unit = this.game.getTileMap().getUnitAt(position);
            if(unit != null){
               image = unit.getImage();
               yOffset = -40; //Unit height
           }
            bbg.drawImage(image, x*20, y*20+yOffset, this);
         }
      }
   }
}
Offline hapsburgabsinth

Senior Newbie





« Reply #1 - Posted 2011-11-30 00:04:01 »

Just tested my game again, and it is every tile, where the player isn't standing, that are jelly-ish. :S

Here is a screenshot of my problem:



The world is a square as for now :S
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-11-30 03:57:37 »

Where do you draw the images after you get them from Tile, Item, or Building?

Also why are you setting backBuffer twice?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline hapsburgabsinth

Senior Newbie





« Reply #3 - Posted 2011-11-30 07:50:45 »

The backbuffer thing is a mistake after I made a minor edit in my code Tongue

I draw the images onto a buffered image, and then onto the JPanel, that what you mean?
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-11-30 08:13:03 »

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  
   public void render() {
      Graphics bbg = this.backBuffer.getGraphics();
      BufferedImage image = null;
     
      Player player = this.game.getTileMap().getPlayer();

      for(int y = 0; y < 24; y++){
         for(int x = 0; x < 42; x++){
            Position position = new Position(x + player.getPosition().getX()-21,y + player.getPosition().getY()-12);
            int yOffset = 0;
            //Draw tiles
           Tile tile = this.game.getTileMap().getTileAt(position);
            if(tile != null){
               image = tile.getImage();
            }else{
               tile = new Tile(position, "WATER");
               image = tile.getImage();
            }

            //You should be drawing the Tile's image here

            //Draw items
           Item item = this.game.getTileMap().getItemAt(position);
            if(item != null){
               image = item.getImage();     // <-- The Tile's image is lost after this assignment
           }

            //You should be drawing the Item's image here

            //Draw buildings
           Building building = this.game.getTileMap().getBuildingAt(position);
            if(building != null){
               image = building.getImage();     // <-- The Item's image is lost after this assigmnent
           }

            //You should be drawing the Building's image here

            //Draw units
           Unit unit = this.game.getTileMap().getUnitAt(position);
            if(unit != null){
               image = unit.getImage();       // <-- The Building's image is lost after this assignment
              yOffset = -40; //Unit height
           }
            bbg.drawImage(image, x*20, y*20+yOffset, this);
         }
      }
   }

Offline hapsburgabsinth

Senior Newbie





« Reply #5 - Posted 2011-11-30 08:16:52 »

Yes I know the tiles images is lost, that's the point, don't wanna draw a tile if it ain't visible Smiley
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-11-30 08:19:52 »

If so, then the code you are showing us is insufficient to answer your question Wink

Offline hapsburgabsinth

Senior Newbie





« Reply #7 - Posted 2011-11-30 08:22:06 »

Hmm... maybe this will help then, but it alot of code Tongue

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  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
public class GUI extends JFrame implements ZompocalypseGUI{

   private GamePanel gamePanel;
   private boolean running;
   private int fps;
   private int windowWidth;
   private int windowHeight;
   private Insets insets;
   private Game game;
   private String gameMode;
   private KeyController keyController;
   private MouseController mouseController;
   private JButton singlePlayerBtn;
   private JFrame titleMenu;
   private int windowLocationX;
   private int windowLocationY;

   public GUI(int windowWidth, int windowHeight, int fps, Game game) {
      this.gameMode = "waiting";
     
      this.running = true;
      this.fps = fps;
      this.windowWidth = windowWidth;
      this.windowHeight = windowHeight;
      this.game = game;

      this.keyController = new KeyController();
      this.addKeyListener(keyController);

      this.mouseController = new MouseController();
      this.addMouseListener(mouseController);

      Toolkit toolkit = Toolkit.getDefaultToolkit();
      Dimension screenSize = toolkit.getScreenSize();

      this.windowLocationX = (int) screenSize.getWidth();
      this.windowLocationY = (int) screenSize.getHeight();

      this.setTitle("Zompocalypse");
      this.setSize(this.windowWidth, this.windowHeight);
      this.setResizable(false);
      this.setLocation((windowLocationX - windowWidth) / 2,( windowLocationY - windowHeight) / 2);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);

      this.setVisible(true);
      this.setIgnoreRepaint(true);

      this.insets = this.getInsets();
      setSize(insets.left + this.windowWidth + insets.right, insets.top + this.windowHeight + insets.bottom);

     

      this.gamePanel = new GamePanel(windowWidth, windowHeight, this.game);
      this.add(this.gamePanel);

      openTitleMenu();
   }

   private void openTitleMenu() {
      this.titleMenu = new JFrame("Zompocalypse - StartMenu!");
      titleMenu.setSize(300, 400);
      titleMenu.setResizable(false);
      titleMenu.setLocation((windowLocationX - 300) / 2, (windowLocationY - 400) / 2);
      titleMenu.setDefaultCloseOperation(EXIT_ON_CLOSE);
      titleMenu.setLayout(null);

      this.singlePlayerBtn = new JButton("SinglePlayer");
      singlePlayerBtn.setSize(250, 30);
      singlePlayerBtn.setLocation(25, 25);
      singlePlayerBtn.addActionListener(this);
      titleMenu.add(singlePlayerBtn);

      titleMenu.setVisible(true);
   }

   @Override
   public void run() {
      while (running) {
         long time = System.currentTimeMillis();
         update();
         render();
         draw();

         time = (1000 / fps) - (System.currentTimeMillis() - time);
         if (time > 0) {
            try {
               Thread.sleep(time);
            } catch (Exception e) {
               System.out.println(e);
            }
         }
      }
      setVisible(false);
   }

   public void update(){
      game.updatePlayer();
      game.updateMobs();
   }
   
   public void render(){
      gamePanel.render();
   }
   
   public void draw() {
      gamePanel.draw();
   }

   public String getGameMode() {
      return gameMode;
   }

   public void setGameMode(String gameMode){
      this.gameMode = gameMode;
   }

   public void actionPerformed(ActionEvent e) {
      if(e.getSource().equals(this.singlePlayerBtn)){
         setGameMode("newGame");
         this.titleMenu.setVisible(false);
      }
   }

   private class MouseController implements MouseListener{

      @Override
      public void mouseClicked(MouseEvent e) {
         Player player = game.getTileMap().getPlayer();
         int xOffset = 19;
         int yOffset = 12;
         if(e.getButton() == MouseEvent.BUTTON3){
            Point clickedPoint = e.getPoint();
            int x = (int) (clickedPoint.getX() / 20) - xOffset;
            int y = (int) (clickedPoint.getY() / 20) - yOffset;
            Position targetPosition = new Position(x, y);
            game.attack(player, targetPosition);
            System.out.println("Attacking targetPosition: " + targetPosition.toString());
         }
      }

      @Override
      public void mouseEntered(MouseEvent arg0) {
         // TODO Auto-generated method stub

      }

      @Override
      public void mouseExited(MouseEvent arg0) {
         // TODO Auto-generated method stub

      }

      @Override
      public void mousePressed(MouseEvent e) {

      }

      @Override
      public void mouseReleased(MouseEvent arg0) {
         // TODO Auto-generated method stub

      }
   }

   private class KeyController implements KeyListener{

      @Override
      public void keyPressed(KeyEvent e) {
         if(gameMode != "waiting"){
            int key = e.getKeyCode();
            Player player = game.getTileMap().getPlayer();
            Position playerPosition = player.getPosition();
            Position targetPosition;
            int playerX = player.getPosition().getX();
            int playerY = player.getPosition().getY();
            Vector direction = player.getDirection();
           
            if (key == KeyEvent.VK_A) {
               targetPosition = new Position(playerX - 1, playerY);
               game.setDirectionOnUnit(playerPosition, targetPosition);
               game.moveUnitTo(playerPosition, new Position(playerX - 1, playerY));
            }else if (key == KeyEvent.VK_D) {
               targetPosition = new Position(playerX + 1, playerY);
               game.setDirectionOnUnit(playerPosition, targetPosition);
               game.moveUnitTo(playerPosition, new Position(playerX + 1, playerY));
            }else if (key == KeyEvent.VK_W) {
               targetPosition = new Position(playerX, playerY - 1);
               game.setDirectionOnUnit(playerPosition, targetPosition);
               game.moveUnitTo(playerPosition, new Position(playerX, playerY - 1));
            }else if (key == KeyEvent.VK_S) {
               targetPosition = new Position(playerX, playerY + 1);
               game.setDirectionOnUnit(playerPosition, targetPosition);
               game.moveUnitTo(playerPosition, new Position(playerX, playerY + 1));
            }else if (key == KeyEvent.VK_E) {
               // Action on item
              Item i = game.getTileMap().getItemAt(playerPosition);
               if (i != null) {
                  game.action(i);
               }

               // Action on building
              Building b = game.getTileMap().getBuildingAt(playerPosition);
               if (b != null) {
                  game.action(b);
               }
            }else if (key == KeyEvent.VK_R) {
               // Action on Zombie
              Position zombiePosition = new Position(playerX + direction.getX(),
                     playerY + direction.getY());
               Zombie z = (Zombie) game.getTileMap().getUnitAt(zombiePosition);
               if (z != null) {
                  if (game.isUnitWithinAttackRange(player, z)) {
                     System.out.println("Attacking: " + z.toString());
                     game.attack(player, z);
                  }
               }
            }else if(key == KeyEvent.VK_I){
               System.out.println(player.getInventory().toString());
            }
         }
      }

      @Override
      public void keyReleased(KeyEvent arg0) {
         // TODO Auto-generated method stub

      }

      @Override
      public void keyTyped(KeyEvent e) {

      }
   }
}
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-11-30 08:23:29 »

To be honest, I don't even know what you mean when you say the tiles are jelly-ish, care to show us a video/give us a test applet?

Offline hapsburgabsinth

Senior Newbie





« Reply #9 - Posted 2011-11-30 08:31:10 »

Made an executable .jar file with the "game". Smiley Hope that it works Smiley

http://sudopwn.net/Files/Zompocalypse.zip

ohh, and you move with "wasd", Tongue
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-11-30 08:38:49 »

Oh! That's because the KeyListener is running on the EDT (Event Dispatching Thread) while your game loop is in a different thread. Your options are to use boolean flags in the listener (like true for down and false up) and move the key handling to the update method, or put a big synchronized block inside in the KeyListener methods:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void run() {
    while(running) {
        synchronized(this) {
            update();
            render();
            draw();
        }
       
        ...
    }
}

....

class KeyController implements KeyListener {
    public void keyPressed(KeyEvent key) {
        synchronized(GUI.this) {
            .....
        }
    }
}


EDIT: Update code

Offline hapsburgabsinth

Senior Newbie





« Reply #11 - Posted 2011-11-30 08:41:42 »

I will try that, thanks a lot!
Offline hapsburgabsinth

Senior Newbie





« Reply #12 - Posted 2011-11-30 08:52:35 »

Well it worked, but now the player just keep going in one direction when you push a button Tongue and its pretty laggy... I'll try go with the flaggin', and then come back and let you know if that is better Smiley

Thanks again, I've appreciated your response Smiley
Offline hapsburgabsinth

Senior Newbie





« Reply #13 - Posted 2011-11-30 09:15:40 »

Well now it works, but its VERY laggy, when I press a key it takes for ever to move the player :S

updated the .jar file

http://sudopwn.net/Files/Zompocalypse.zip
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2011-11-30 09:21:00 »

Try making the booleans volatile:
1  
2  
private volatile boolean blah = false;
...


Also, what is the FPS that you set?

Offline hapsburgabsinth

Senior Newbie





« Reply #15 - Posted 2011-11-30 09:23:11 »

It's set to 60

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
while (running) {
         long time = System.currentTimeMillis();

         update();
         render();
         draw();

         time = (1000 / fps) - (System.currentTimeMillis() - time);
         if (time > 0) {
            try {
               Thread.sleep(time);
            } catch (Exception e) {
               System.out.println(e);
            }
         }
      }


where fps is 60



Hmm volatile didn't do the trick Sad
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.

Pippogeek (36 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (35 views)
2014-09-21 00:26:15

UprightPath (48 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!