Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  How to draw png images to the screen efficiently?  (Read 3141 times)
0 Members and 1 Guest are viewing this topic.
Offline Quillion

Senior Newbie





« Posted 2010-04-14 13:58:55 »

Hello everyone, sorry if this question sounds too nooby, but how would you draw png images in the game without it lagging. I tried to use drawImage, but it caused HUGE lag when number of pctures increased.

This is my code skeleton here

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  
235  
236  
package attempt1;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main implements Runnable
{
   private int WIDTH = 1000;
   private int HEIGHT = 500;
   private long desiredFPS = 60;
   
   private String title = "Maple Story";
   
   private long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
   private boolean running = true;
   
   private JFrame frame;
   private Canvas canvas;
   private BufferStrategy bufferStrategy;
   
   private GraphicsDevice graphicsDevice;
   private DisplayMode originalDisplayMode;
   
   public Main()
   {
      GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
      graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
      originalDisplayMode = graphicsDevice.getDisplayMode();
     
      // create new Frame
     frame = new JFrame(title);
      frame.setUndecorated(true);

      graphicsDevice.setFullScreenWindow(frame);
      if (graphicsDevice.isDisplayChangeSupported())
      {
         graphicsDevice.setDisplayMode(getBestDisplayMode(graphicsDevice));
      }
     
      Rectangle bounds = frame.getBounds();
      WIDTH = (int)bounds.getWidth();
      HEIGHT = (int)bounds.getHeight();      
     
      // create new JPanel with specified width and height
     JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(null);
      // create a canvas inside the JPanel with specified width and height
     canvas = new Canvas();
      //canvas.setBounds(0, 0, WIDTH, HEIGHT);
     canvas.setBounds(bounds);
      canvas.setIgnoreRepaint(true);
     
      panel.add(canvas);
      // Add Mouse and Key Listeners
     canvas.addMouseListener(new MouseControl());
      canvas.addKeyListener(new KeyInputHandler());
      // initialize the window
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);
     
      setup();
      // set graphics device
     canvas.createBufferStrategy(2);
      bufferStrategy = canvas.getBufferStrategy();
      // focus on the window
     canvas.requestFocus();
   }
   /*
    * Main game loop
    */

   public void run()
   {
      long beginLoopTime;
      long endLoopTime;
      long currentUpdateTime = System.nanoTime();
      long lastUpdateTime;
      long deltaLoop;
     
      while(running)
      {
         beginLoopTime = System.nanoTime();
         
         render();
         
         lastUpdateTime = currentUpdateTime;
         currentUpdateTime = System.nanoTime();
         update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
         
         endLoopTime = System.nanoTime();
         deltaLoop = endLoopTime - beginLoopTime;
           
           if(deltaLoop > desiredDeltaLoop)
           {
               //Do nothing. We are already late.
          }
           else
           {
               try
               {
                   Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
               }
               catch(InterruptedException e)
               {
                   //Do nothing
              }
           }
      }
   }
   /*
    * Method to initialise graphics that are going to be painted to the screen
    */

   private void render()
   {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      render(g);
      g.dispose();
      bufferStrategy.show();
   }
   /*
    * Initialise all of your Objects in here
    */

   public void setup()
   {
     
   }
   /*
    * Method that handles all the movements that are supposed to happen
    * So initialise all the x and y changes here
    * deltaTime tells you how many nanoseconds have passed by since this function
    * was last called
    */

   protected void update(int deltaTime)
   {
     
   }
   /*
    * Method where you paint to the screen
    */

   protected void render(Graphics2D g)
   {
      /*
       * START PAINTING
       * START PAINTING
       * START PAINTING
       * START PAINTING
       * START PAINTING
       * START PAINTING
       */


     
     
      /*
         * STOP PAINTING
         * STOP PAINTING
         * STOP PAINTING
         * STOP PAINTING
         * STOP PAINTING
         * STOP PAINTING
         */

   }
   /*
    * Keyboard Action listeners
    * Key esc closes the program by default
    */

   private class KeyInputHandler extends KeyAdapter
   {
      public void keyPressed(KeyEvent e)
      {
         if(e.getKeyCode()==27)
           {
            graphicsDevice.setDisplayMode(originalDisplayMode);
            graphicsDevice.setFullScreenWindow(null);
              System.exit(0);
           }
      }
     
      public void keyReleased(KeyEvent e)
      {
         
      }
   }
   /*
    * Mouse Action Listeners
    */

   private class MouseControl extends MouseAdapter
   {
     
   }
   /*
    * public static void main
    * I do not even have to explain this
    */

   public static void main(String [] args)
   {
      Main ex = new Main();
      new Thread(ex).start();
   }
   
   private static DisplayMode MODES[] = new DisplayMode[]
   {
      new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 16, 0),
       new DisplayMode(640, 480, 8, 0)
   };

   private static DisplayMode getBestDisplayMode(GraphicsDevice device)
   {
      for (int x = 0, xn = MODES.length; x < xn; x++)
      {
         DisplayMode[] modes = device.getDisplayModes();
         for (int i = 0, in = modes.length; i < in; i++)
         {
            if (modes[i].getWidth() == MODES[x].getWidth() && modes[i].getHeight() == MODES[x].getHeight() && modes[i].getBitDepth() == MODES[x].getBitDepth())
            {
               return MODES[x];
            }
         }
      }
      return null;
   }
}


So what would you suggest I use to draw png images?
I heard that BufferedImage is good, but I have failed miserably at using it, because it does not seem to have same properties as Image, especially when it comes to getWidht and getHeight commands.
Thanks to everyone for help in advance ^^
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2010-04-14 14:11:16 »

Pretty much hardware acceleration needs to be on or drawImage is going to be very slow. If you need really good output, I'd recommend just going ahead and using Slick.

See my work:
OTC Software
Offline Quillion

Senior Newbie





« Reply #2 - Posted 2010-04-14 14:43:44 »

Thank you very much for your help. I looked at slick, and it looks pretty good, but being the noob that I am, I was wondering if there were any tutorials on how to work with it?
Also is slick the only alternative I am faced with?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #3 - Posted 2010-04-14 14:49:44 »

this aughta do the trick,

http://slick.cokeandcode.com/wiki/doku.php
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2010-04-14 17:37:46 »

Please do not double post. Your other post was removed.

Thanks.

See my work:
OTC Software
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.

Nickropheliac (14 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
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!