5 Replies - 1342 Views - Last Post: 09 July 2012 - 12:33 PM Rate Topic: -----

#1 marcinkk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-May 12

java swing double buffering

Posted 09 July 2012 - 06:19 AM

I have a problem with the implementation of double buffering, can someone check my code? flashing square
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingkwadrat;

import java.util.TimerTask;
/**
 *
 * @author marcin
 */
public class KlasaZadanie extends TimerTask{
    
    int pozX = 50;
    int pozY = 50;
    int kierunek = 1;
    int licznik = 1;

    @Override
    public void run() {
        switch(kierunek){
            case 1:
                licznik++;
                pozX++;
                if(licznik == 600){
                    licznik = 0;
                    kierunek = 2;
                }
                break;
            case 2:
                licznik++;
                pozY++;
                if(licznik == 400){
                    licznik = 0;
                    kierunek = 3;
                }
                break;
            case 3:
                licznik++;
                pozX--;
                if(licznik == 600){
                    licznik = 0;
                    kierunek = 4;
                }
                break;
            case 4:
                licznik++;
                pozY--;
                if(licznik == 400){
                    licznik = 0;
                    kierunek = 1;
                }
                break;
        }
    }
    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingkwadrat;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author marcin
 */
public class KlasaKwadrat extends JFrame{
    
    private int width = 800;
    private int height = 600;
    private Dimension dimension = new Dimension(width, height);
    JPanel jPanel = new JPanel();
    KlasaZadanie klasaZadanie = new KlasaZadanie();
    Timer timer;
    Image bufor;
    Graphics bg;
    private boolean podwojneBuforowanie = jPanel.isDoubleBuffered();

    public KlasaKwadrat() {
        setTitle("ruchomy kwadrat !!!");
        setSize(dimension);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jPanel);
        jPanel.setDoubleBuffered(podwojneBuforowanie = true);
        setVisible(true);
        
        bufor = createImage(width, height);
        bg = bufor.getGraphics();
        timer = new Timer(10, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                klasaZadanie.run();
                repaint();
            }
        });
        timer.start();
    }
    
    @Override
    public void update(Graphics graphics){
        bg.clearRect(0, 0, width, height);
        paint(bg);
        graphics.drawImage(bufor, 0, 0, this);
    }
    
    @Override
    public void paint(Graphics graphics){
        super.paint(graphics);
        graphics.setColor(Color.red);
        graphics.fillRect(klasaZadanie.pozX, klasaZadanie.pozY, 100, 100);
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingkwadrat;

import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingKwadrat {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                KlasaKwadrat klasaKwadrat = new KlasaKwadrat();
            }
        });
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: java swing double buffering

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: java swing double buffering

Posted 09 July 2012 - 06:59 AM

Please describe the problem. Include any error messages, if applicable.
Was This Post Helpful? 0
  • +
  • -

#3 marcinkk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-May 12

Re: java swing double buffering

Posted 09 July 2012 - 07:01 AM

there is a flickering effect and the problem with the liquidity
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: java swing double buffering

Posted 09 July 2012 - 07:08 AM

Your telling it to paint too many times. You don't need to call super.paint(graphics); in your paint method. In fact you don't even need to override the update method, just the paint method. This should make it run smoother:

@Override
    public void update(Graphics graphics){
    }
    
    @Override
    public void paint(Graphics graphics){
		graphics.setColor(Color.red);
        graphics.clearRect(0, 0, width, height);
        graphics.fillRect(klasaZadanie.pozX, klasaZadanie.pozY, 100, 100);
    }


When the panel is invalidated, it will call the paint() on its own, so by having a pain in the update(), it will be called twice as often which is too often for the repaint to finish.
Was This Post Helpful? 0
  • +
  • -

#5 marcinkk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-May 12

Re: java swing double buffering

Posted 09 July 2012 - 09:29 AM

how force as in this example active rendering??
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: java swing double buffering

Posted 09 July 2012 - 12:33 PM

View PostSwiftStriker00, on 09 July 2012 - 10:08 AM, said:

Your telling it to paint too many times. You don't need to call super.paint(graphics); in your paint method.

Yes you do.
It will paint the background and the borders

To OP: The hardware of a CRT refreshs it every 16.666 millisecond (at least in North America)
Having a Timer that calls repaint() move than every 17-20 milli second is completly useless
you are grabbing resources for nothing and drawing in memory for nothing

Swing JPanel are double buffered by default, no need to implement it by hand. If you want to do so, for whichever reason, turn off first doublle buffering on your JPanel at construction time
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1