Swing : animation still stutter when I use Toolkit.getDefaultToolkit().sync();

Swing : animation still stutter when I use Toolkit.getDefaultToolkit().sync();



I'm trying to draw Image every 100ms time by using Thread.sleep(). But I heard that when using Thread.sleep() with swing, It moves like stutter. so I add code Toolkit.getDefaultToolkit().sync(), but it still looks like stuttering.



Correctly speaking, it looks like delay like 2~300ms and draw at once with short time.



I mean when I want to move Image, I wish to look like



draw - sleep - draw - sleep - ...



but it looks like



draw - sleep - sleep - sleep - draw - draw - draw - sleep - ...



I'm sorry that poor of my explanation, I wish you can understand this.



And here is my code.


public abstract class EngineViewer implements Runnable

private int _frameInterval = 100;
private CameraInterface _camera;

private Canvas _canvas;

public EngineViewer(int displayWidth, int displayHeight, int resolutionWidth, int resolutionHeight)

Thread thread = new Thread(this);
thread.start();


private void Sleep()

try Thread.sleep(_frameInterval);
catch(Exception e)



@Override
public void run()

while(true)

Clear();

_camera.Draw(_canvas);

Draw();

Sleep();



protected abstract void Draw();



Register camera to EngineViewer, EngineViewer call Camera.Draw() every time.
Camera draw Image to buffer which is get from SwingViewer._buffer.



And draw _buffer to _panel at Draw Method, after that call Toolkit.getDefaultToolkit().sync().


public class SwingViewer extends EngineViewer

private JFrame _frame;

private SWPanel _panel;

private BufferedImage _buffer;
private Graphics2D _graphic;

public SwingViewer(String name, int displayWidth, int displayHeight, int resolutionWidth, int resolutionHeight)

super(displayWidth, displayHeight, resolutionWidth, resolutionHeight);

_frame = new JFrame(name);
_frame.setSize(displayWidth, displayHeight);
_frame.setVisible(true);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

_panel = new SWPanel();
_frame.add(_panel);


@Override
public void Draw()

if(_panel != null)
_panel.Draw();

Toolkit.getDefaultToolkit().sync();


private class SWPanel extends JPanel

private static final long serialVersionUID = 1;

private void Draw()

repaint();


@Override
public void paintComponent(Graphics g)

super.paintComponent(g);

g.drawImage(_buffer, 0, 0, EngineViewer.DisplayWidth(), EngineViewer.DisplayHeight(), null);







There is no need for a while loop and a Thread.sleep(). Instead use a Swing Timer to schedule the animation. Read the section from the Swing tutorial on How to Use Swing Timers for more information. Also, method names should NOT start with an upper case character.
– camickr
Aug 31 at 3:55





😓 This is not how you should be using Canvas or JPanel to perform animation - Swing is double buffered anyway, so your wasting a lot of time for no reasonable results. Either you want to use the Swing (passive) painting system or you want to make use a BufferStrategy and take complete (active) control over the painting system ... don't try and do both
– MadProgrammer
Aug 31 at 4:18


Canvas


JPanel


BufferStrategy





@camickr is there any other way to use Thread.sleep() at all?
– 신승빈
Aug 31 at 4:55






@MadProgrammer I'm sorry that what does it mean. I think you are saying that choose swing paint system or bufferstrategy, but I can't understand what does it mean exactly. Can you explain about it more detail with some example?
– 신승빈
Aug 31 at 5:01





@신승빈 Painting in AWT and Swing; Passive vs Active Rendering; Double Buffering and Page Flipping
– MadProgrammer
Aug 31 at 5:06






Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế