Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / JPanel is "Tearing" / Jerky on: 2006-02-13 05:45:21
I used a CardLayout to put two JPanels on top of another for my application.
The first jpanel is of my game, which runs perfectly fine without slowdowns.
On the title screen, however, I am noticing some tearing and I can't for the life of me figure out why.
The code (as far as I can see) is pretty much almost the same as the game's in terms of structure.
The moving "FakeBlocks" tear occasionally and the animation is jerky (unlike in the game -- which works perfectly).

Does it look like I'm missing an override, or as if I'm skipping / calling the wrong thing?
I've been looking at this code for a few hours now.

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  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.*;
import java.io.*;


public class LevelSelect extends JPanel implements MouseMotionListener,MouseListener {
   public static Image bufferImage;
   public Image background = Toolkit.getDefaultToolkit().getImage("images/misc/background1.png");
   public Game GamePanel = new Game();
   public int ctr;
   private ArrayList a = new ArrayList();
   Random rnd = new Random();
   FontMetrics fm;
   Font cookies;
   AnimUpdate update;
   public LevelSelect() {
      setDoubleBuffered(false);
      setIgnoreRepaint(true);
      rnd.setSeed(System.currentTimeMillis());
      setLayout(new GridLayout());
      addMouseListener(this);
      addMouseMotionListener(this);
      add(GamePanel);
      GamePanel.setSize(this.getWidth(),this.getHeight());
      GamePanel.setVisible(false);
      cookies = GameScreens.cookies.deriveFont((float)24);
      fm = getFontMetrics(cookies);
      for (int i=0; i<1; i++) {
         FakeBlock fb = new FakeBlock();
         fb.blockType=rnd.nextInt(8)+1;
         fb.speed=(rnd.nextInt(4)+2)*2;
         fb.xPos=rnd.nextInt(481);
         fb.yPos=15+i*65;//112+i*35;
         fb.setImage();
         a.add(fb);
      }
     
      update = new AnimUpdate();
      update.start();
      setVisible(true);
   }  
   
   public void paintComponent(Graphics 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);
      }
     
      g2 = (Graphics2D)bufferImage.getGraphics();
      g2.setFont(cookies);

      String str2 = "Click to Continue";
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
     
      g2.drawImage(GameScreens.background,0,0,null);
      AlphaComposite ac;
      ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float).4);
      g2.setComposite(ac);
      for (int i=0; i<a.size(); i++) {
         FakeBlock fb = (FakeBlock)a.get(i);
         fb.move();
         g2.drawImage(fb.myPicture,fb.xPos,fb.yPos,null);
      }
      ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)1);
      g2.setComposite(ac);
      g2.drawImage(GameScreens.titleScr,0,0,null);
     
      ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)Math.abs(Math.sin(Math.toRadians(ctr%180))));
      g2.setComposite(ac);
      g2.drawString(str2,(int)((this.getWidth()-fm.stringWidth(str2))/2.0),320);
      g.drawImage(bufferImage,0,0,null);
   }
   
    public void update(Graphics g) {
       paintImmediately(0,0,480,400);
    }
   
   public void paint(Graphics g) {
      paintComponent(g);
   }
      public void mouseDragged(MouseEvent e) {
   } //end method mouseDragged
   
   //for when the mouse is just moving
   public void mouseMoved(MouseEvent e) {
   } //end of method mouseMoved
   
   //for when the mouse button is held down
   public void mousePressed(MouseEvent e) {
   } //end of method mousePressed
   
   //for when the mouse button is released
   public void mouseReleased(MouseEvent e) {
      GameScreens.goGameScreen();
   } //end of method mouseReleased
   
   //some unused mouse methods
   public void mouseClicked(MouseEvent e) {
   }
   public void mouseEntered(MouseEvent e){ }
   public void mouseExited(MouseEvent e) { }
   
   class FakeBlock {
      public int blockType;
      public AlphaComposite alpha;
      public int xPos;
      public int yPos;
      public double speed;
      public double alphaOffset;
      public double alphaSpeed;
      public Image myPicture;
     
      public FakeBlock() {
         
      }
     
      public void setImage() {

         if (blockType == 0) {
            myPicture=GameScreens.one;
         } else if(blockType ==1) {
            myPicture=GameScreens.two;
         } else if (blockType == 2) {
            myPicture=GameScreens.thr;
         } else if (blockType == 3) {
            myPicture=GameScreens.fou;
         } else if(blockType == 4) {
            myPicture=GameScreens.fiv;
         } else if (blockType == 5) {
            myPicture=GameScreens.six;
         } else if (blockType == 6) {
            myPicture=GameScreens.sev;
         } else if (blockType == 7) {
            myPicture=GameScreens.eig;
         } else if (blockType == 8) {
            myPicture=GameScreens.nin;
         }
      }
     
      public void paint(Graphics g) {
         paintComponent(g);
      }
     
      public void move() {
         xPos += (int)speed;
         if (xPos > 481) {
            blockType=rnd.nextInt(8)+1;
            speed=(rnd.nextInt(4)+2)*2;
            xPos=-32-rnd.nextInt(200);

            setImage();
         }
      }
   }
   
   class AnimUpdate extends Thread {
      //the method run when .start() is called
      public void run() {
         while(Thread.currentThread() == update) {
            //paint & update the frame number
            paintImmediately(0,0,480,400);
            ++ctr;
            try { //sleep try-catch
               Thread.sleep(16);  
            } catch(InterruptedException e) {
               System.out.println("InterruptedException @ AnimUpdate");
            } //end of try-catch block (sleep)
           
           
         } //end of while loop
      } //end of run() method
   } //end of class AnimUpdate
}
2  Game Development / Newbie & Debugging Questions / Re: Multiple, overlapping JPanels? on: 2006-02-13 03:03:48
CardLayout has worked great!
Thanks a bunch, guys.  Grin
3  Game Development / Newbie & Debugging Questions / Re: Multiple, overlapping JPanels? on: 2006-02-12 05:06:50
I thought about TabbedPanes but can you hide their buttons?
4  Game Development / Newbie & Debugging Questions / Multiple, overlapping JPanels? on: 2006-02-12 03:04:35
Hey guys, my game is currently set up so that there is a single JFrame. The "Game" class displays levels, etc (extends JPanel).
However, I now need to make a screen for the level select/ menus. Is there a way to make more JPanels and have them overlap each other on the JFrame, and then just pick which one to show (and subsequently hide others), so I don't have to use a GameState variable or something and end up with a 100kb Game.class instead.
Thanks, much appreciated.
5  Game Development / Newbie & Debugging Questions / Re: Incredible game slowdown [Java Application] on: 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.
6  Game Development / Newbie & Debugging Questions / Re: Incredible game slowdown [Java Application] on: 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
7  Game Development / Newbie & Debugging Questions / Re: Incredible game slowdown [Java Application] on: 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

?
8  Game Development / Newbie & Debugging Questions / Incredible game slowdown [Java Application] on: 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!
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

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

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

toopeicgaming1999 (7 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (24 views)
2014-11-25 11:26:43

Gibbo3771 (23 views)
2014-11-24 19:59:16

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

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

digdugdiggy (52 views)
2014-11-12 21:11:50
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!