Results 1 to 6 of 6
  1. #1
    CoderMan is offline Member
    Join Date
    Feb 2010
    Location
    Ross Lunar Capsule 2571
    Posts
    15
    Rep Power
    0

    Unhappy First Java game: Why is screen painting so slow?

    Hi, I'm fairly new to Java... hope this is the right sub-forum. I'm trying to write my first Java game (not counting an applet I wrote a few months ago...) I wrote a little test code for a class, but I am confused why painting to the screen is so slow. Here is the test class I started with:

    Java Code:
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferStrategy;
    import java.io.File;
    import java.io.IOException;
    import java.lang.InterruptedException;
    import javax.imageio.ImageIO;
    
    public class TestRotation {
        public static void main(String [] args) {
    	try {
    	    Frame frame = new Frame("RotationTest");
    	    frame.setSize(800, 600);
    	    frame.setVisible(true);
    	    frame.createBufferStrategy(2);
    	    BufferStrategy strategy = frame.getBufferStrategy();
    	    Perimeter perimeter = new Perimeter();
    	    perimeter.setBackdrop("nightsky1600x600");
    	    int x_start = 0;
    	    while(true) {
    		Graphics graphicsTarget = strategy.getDrawGraphics();
    		int x_end = x_start + 800;
    		if(x_end > 1599) { x_end -= 1600; }
    		Image image = perimeter.getImage(x_start, x_end);
    		graphicsTarget.drawImage(image, 0, 0, null);
    		x_start += 20;
    		if(x_start > 1599) { x_start = 0; }
    		strategy.show();
    		Thread.sleep(50);
    	    }
    	} catch(Throwable e) {
    	    e.printStackTrace();
    	    if(e.getMessage() != null) { System.err.println(e.getMessage()); }
    	    if(e.getCause() != null) {
    		Throwable cause = e.getCause();
    		cause.printStackTrace();
    		if(cause.getMessage() != null) { System.err.println(e.getMessage()); }
    	    }
    	    System.exit(1);
    	}
        }
    }
    As you can see, I initially put a sleep statement in there. I come from a C++ background, so I thought without the sleep statement the frame rate would be crazy fast, but it was actually really bad. So I kept lowering the sleep value until I got rid of it altogether, at which point the frame rate (estimated visually) was just barely high enough to get by with.

    The image construction operations taking place inside the Perimeter object are very simple and I shouldn't think they would be using very much time at all, although I don't know exact time values yet.

    It seems like this should be a lot faster because I'm testing it on a 64 bit Linux system with a real nice Nvidia card with the Nvidia drivers properly installed (runs all my 3D games just fine with good frame rates).

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What is this Perimeter class? You seem to be reading in an image with each iteration of the loop. Are you really doing this, and if so do you really want to do this?

    Other than that, I can't add much more since I'm not up on AWT graphics and am better at Swing graphics.

  3. #3
    CoderMan is offline Member
    Join Date
    Feb 2010
    Location
    Ross Lunar Capsule 2571
    Posts
    15
    Rep Power
    0

    Default

    The first call to the perimeter class, setBackdrop(), reads a PNG file and puts the image data into a BufferedImage. All the subsequent calls, getImage(), just returns a subset of that BufferedImage data. (With some splitting and stitching processing if necessary.) I don't know of a more efficient way to do that than I am now.

    You mentioned Swing... will using Swing instead of awt speed this up? I haven't messed around with swing yet because it seems like it does more than I need it to do... but I could if it would speed the graphics up somehow.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CoderMan View Post
    The first call to the perimeter class, setBackdrop(), reads a PNG file and puts the image data into a BufferedImage. All the subsequent calls, getImage(), just returns a subset of that BufferedImage data. (With some splitting and stitching processing if necessary.) I don't know of a more efficient way to do that than I am now.
    Perhaps split your image from the get-go and have the images available in memory (if not too big).

    You mentioned Swing... will using Swing instead of awt speed this up? I haven't messed around with swing yet because it seems like it does more than I need it to do... but I could if it would speed the graphics up somehow.
    Swing is more robust than AWT, more flexible and more powerful. I can't say that it's any faster, but it may not be the graphics library that's slowing your down but rather your handling of images (I'm guessing here).

  5. #5
    bleah's Avatar
    bleah is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

  6. #6
    CoderMan is offline Member
    Join Date
    Feb 2010
    Location
    Ross Lunar Capsule 2571
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by bleah View Post
    Thanks, but I think you're about 3 months too late... Ended up giving up on the game for mostly unrelated reasons. (Now I'm working on a Ruby game with light-weight graphics.) But I'll keep lwjgl in mind for my next project.

Similar Threads

  1. Why my java lan game programm slow!
    By arnelpogs in forum Networking
    Replies: 2
    Last Post: 12-07-2009, 05:00 PM
  2. Why JAVA is so slow?
    By aRTx in forum New To Java
    Replies: 2
    Last Post: 05-11-2009, 11:19 AM
  3. Replies: 2
    Last Post: 04-03-2009, 06:04 AM
  4. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 06:03 PM
  5. Java applet strange painting behaviour
    By sirdori in forum New To Java
    Replies: 1
    Last Post: 01-07-2009, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •