Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Incredible game slowdown [Java Application]  (Read 1914 times)
0 Members and 1 Guest are viewing this topic.
Offline pragun

Senior Newbie





« Posted 2006-02-07 23:24:17 »

Hey guys, I'm new here but I'm not totally new to java (albeit I am new to java games).
I've got a P4 3.4Ghz / 1GB RAM computer which I have been using to develop a project for school. The only problem is, the school computers are much slower (450Mhz & 320MB RAM worst case scenario). My game paints very, very slowly on the school computers but at the desired 60FPS at home. It used to work well in school too but then I added a bunch of stuff and I find it hard to target what exactly is causing the delay. If anyone could help me with this issue, i'd appreciate it much.

Here I have excerpts from Game.java, Game extends JPanel and implements both MouseListener and MouseMotionListener
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  
   /* Paint Method */
      public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2;
     
      // Create bufferImage & set vars if it/they don't exist
      if (bufferImage == null) {
         bufferImage = createImage(this.getWidth(),this.getHeight());        
         g2 = (Graphics2D)bufferImage.getGraphics();
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
         setCursor(cMouse);
      }
     
      // paint on top of the image
      g2 = (Graphics2D)bufferImage.getGraphics();
      g2.setFont(cookies);
      g2.setColor(Color.white);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2.drawImage(background,0,0,null);
     
      AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float).2);
      g2.setComposite(ac);
      g2.setColor(Color.WHITE);
      g2.fillRect(boardOffsetX+2,boardOffsetY+2,316,252);
      ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)1);
      g2.setComposite(ac);
     
      // draw each block on the board
      for (int y=0; y<8; y++) {
                    for (int x=0; x<10; x++) {
             Block temp = blocksMap[y][x];
             temp.drawBlock();
                    }
                }
     
      // draw the cursor  
      if (somethingSelected==1 && selBlock != null && selBlock.blockType != -1 && selBlock.alpha>0) {
         g2.drawImage(bCursor[(int)(counter)%12],boardOffsetX+selBlock.gridX*32+selBlock.offsetX,boardOffsetY+selBlock.gridY*32+selBlock.offsetY,null);
      } else if((somethingSelected==0 || mouseDown==0) && blockInFocus!=null) {
         g2.drawImage(bCursor[(int)(counter)%12],boardOffsetX+blockInFocus.gridX*32+blockInFocus.offsetX,boardOffsetY+blockInFocus.gridY*32+blockInFocus.offsetY,null);
      }

      // draw to the screen
      g2.setColor(Color.black);
      ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float).5);
                g2.setComposite(ac);
                g2.fillRoundRect(5,256+84-2,this.getWidth()-10, 58, 15,15);
                g2.fillRoundRect(5, 5, boardOffsetX-11, 256+84-2-11,15,15);
                g2.fillRoundRect(boardOffsetX, 5,320,boardOffsetY-11,15,15);
                ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)1);
                g2.setComposite(ac);
      if (mouseHoverButton == 0) {
                    g2.drawImage(navImage[6],this.getWidth()-64-48*3,256+84+3,null);
                } else {
                    g2.drawImage(navImage[0],this.getWidth()-64-48*3,256+84+3,null);
                }
      if (mouseHoverButton == 1) {
                    g2.drawImage(navImage[7],this.getWidth()-64-48*2,256+84+3,null);
      } else {
                    g2.drawImage(navImage[1],this.getWidth()-64-48*2, 256+84+3,null);
      }
      if (mouseHoverButton == 2) {
                    g2.drawImage(navImage[8],this.getWidth()-48-64, 256+84+3,null);
                } else {
                    g2.drawImage(navImage[2],this.getWidth()-48-64, 256+84+3,null);
                }
      if (mouseHoverButton == 3) {
                    g2.drawImage(navImage[9],this.getWidth()-64,256+84+3,null);
                } else {
                    g2.drawImage(navImage[3],this.getWidth()-64,256+84+3,null);
                }
      if (mouseHoverButton == 5) {
                    g2.drawImage(navImage[11],16, 256+84+3,null);
                }  else {
                    g2.drawImage(navImage[5],16,256+84+3,null);
                }
                if (mouseHoverButton == 4) {
                    g2.drawImage(navImage[10],16+48, 256+84+1,null);
                }  else {
                    g2.drawImage(navImage[4],16+48,256+84+1,null);
                }
                g2.setColor(Color.WHITE);
                g2.drawString("Level: " + currentLevel+"",(int)((boardOffsetX-fm.stringWidth("Level: " + currentLevel))/2.0),37);
                g2.drawString("Moves: " + moves+"",(int)((boardOffsetX-fm.stringWidth("Moves: " + moves))/2.0),64);
                g2.drawString("\"Some Kind of String Goes Here\"", (320-fm.stringWidth("\"Some Kind of String Goes Here\""))/2+boardOffsetX,44);
      g.drawImage(bufferImage,0,0,this);
               
    } // end of paintComponent (paint method)


And then I also have two nested threads, one repaints, one does calculations...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   //AnimUpdate repaints the screen at a set interval (~60 FPS)
   class AnimUpdate extends Thread {
     
      //the method run when .start() is called
      public void run() {
         while(Thread.currentThread() == update) {
            //paint & update the frame number
            //repaint();
            System.out.println("from" + System.)
            paintImmediately(0,0,480,400);
            ++counter;
            try { //sleep try-catch
               Thread.sleep((int)((1.0/60.0)*1000));  
            } catch(InterruptedException e) {
               break;
            } //end of try-catch block (sleep)
         } //end of while loop
      } //end of run() method
   } //end of class AnimUpdate


and

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   class PlayGame extends Thread {
      public void run() {
         while(Thread.currentThread() == pg) {
           
            if (isRunning==0) {
            isRunning=1;
             //calculations code...very very long....goes here.
            }
            isRunning=0;
           
           
            try {
               Thread.sleep((int)((1.0/60.0)*1000));  
            } catch(InterruptedException e) {
               break;
            }
         }
      }


I also can't think of a way to sync the two threads, if that is even necessary. I'm almost positive there's a big flaw somewhere that I don't see (or in the program's flow/design or something).
Once again, thanks!
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2006-02-08 01:34:32 »

I find it hard to target what exactly is causing the delay.

Thats because you aren't using the right tools.

If you arent using Netbeans yet, download it,.  Then download and install the Netbeans Profiler.  Finally, profile your app.

Thats how professionals find out why their code is slow.  You might as well learn profiling skills now as they will be some of the most valuable you will have.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #2 - Posted 2006-02-08 01:49:42 »

1  
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);


I'm sure you tested this candidate?

Hmm, alpha composite stuff is also quite demanding. I'd guess one of those two is responsible. It seems most of your rendering is done by means of images. In this case you should consider the INTERPOLATION_BILINEAR renderinghint instead of ANTIALIASING. But try without any of them first.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pragun

Senior Newbie





« Reply #3 - Posted 2006-02-08 02:19:47 »

thanks guys, I have a copy of netbeans but I never tried it's profiler. I had tried the antialiasing but it didnt seem to kill anything horribly.
I'll keep you updated; appreciate it.

[edit] I installed the profiler but when I use it, it gives me tons of nullpointerexceptions =(
Is there any flaw in having it set up so that:

Game.java (JPanel) has mouseListeners and mousemotionlisteners
 "" "" loads and starts PlayGame thread
game.java loads and starts AnimUpdate thread
AnimUpdate calls paintImmediately() and has a Thread.sleep(16) in a try catch,
PlayGame does calculations and has a Thread.sleep(16) in a try catch

?
Offline Mr_Light

Senior Devvie


Medals: 1


shiny.


« Reply #4 - Posted 2006-02-08 11:04:25 »

don't you get a conflict between swing painting, you painting, swings dubbelbuffer, your own dubbelbuffer etc.

try seperating the stuff.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline pragun

Senior Newbie





« Reply #5 - Posted 2006-02-08 21:49:32 »

don't you get a conflict between swing painting, you painting, swings dubbelbuffer, your own dubbelbuffer etc.

try seperating the stuff.

Coincidentally, I was playing with that today, wondering about it.
I removed super.paintComponent(g) from the draw method, and when I load the panel, i turn the JPanel's native doublebuffering setting off.
Slight improvements but there's still something really killing it, and I haven't been able to find it out yet.

& on a side note, the profiler didnt work that well....I must have done something wrong, it kept giving me NullPointerExceptions
Offline Jeff

JGO Coder




Got any cats?


« Reply #6 - Posted 2006-02-09 09:45:04 »

Yup sounds liek you dont have it installed right.

If youare doing swing then you want to do all your paiting ONLY on the AWT paint thread.  Dont knwo if thats relevent or not, just commenting based on the other comment.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Mr_Light

Senior Devvie


Medals: 1


shiny.


« Reply #7 - Posted 2006-02-09 16:25:16 »

search for "swing" + "thread safety" for the details though I doubt its what causing the slowdown.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline pragun

Senior Newbie





« Reply #8 - Posted 2006-02-10 00:26:59 »

what "programming-template" do you guys tend to use for games?
Like a while loop for the entire game that calls methods w/a wait at the end, or something multithreaded, etc...
what seems to work best?

I might just try redesigning the format.
Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2006-02-10 01:05:11 »

Action games are all "near real-time" programs which means they look like this:

1  
2  
3  
4  
5  
WHILE PLAYING {
   Read Input
   Calculate new state
   RenderFrame
}



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
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.

rwatson462 (38 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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