Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  JPanel is "Tearing" / Jerky  (Read 1026 times)
0 Members and 1 Guest are viewing this topic.
Offline pragun

Senior Newbie





« Posted 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
}
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2006-02-13 17:31:51 »

1. I believe paintImmediately() skips the double buffering and paints directly to the screen. (Can someone confirm this?) You should use "repaint()" instead.

2. You'll get much better results if you use BufferStrategy instead of calling for constant repaints. Repaint() used to be the only solution for active rendering, but it was always inefficient. The BufferStrategy APIs, OTOH, give you direct access to the underlying DirectDraw/OpenGL pipelines for h9igh-performance  rendering.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #2 - Posted 2006-02-14 18:49:55 »

Yes, jbanes is most likely right about paintImmediately.

You are modifying the perspective or positions of entities from one thread while another thread is painting. There are two options AFAI can see:

1. do everything from the event dispatch thread.
2. do nothing from the event dispatch thread (you'll have to queue e.g. keyboard or mouse input which is otherwise managed from the event dispatch thread so you can handle it elsewhere).
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 (30 views)
2014-12-15 09:26:44

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

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

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

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

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

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

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

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

toopeicgaming1999 (30 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!