Hello everyone, I made a game (not finished yet), and I want to implement the double Buffering to solve the tearing/flashing effect.
Problem, all the tutorials I tried don’t work because I don’t use a common way to paint on the screen (I think :’( )
What can I do (simpliest way) to implement Double Buffering ?
Sorry for my bad English, I’m French. Thanks
Here’s my code (simplified) :
My Main :
public static JFrame frame = new JFrame("The wake");
frame.add(new Box());
frame.setSize(LargeurFenetre+5,HauteurFenetre+27); // les bords de la fenetre & l'en tete de la fenetre
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
frame.createBufferStrategy(2);
Class Box :
package hugo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Box extends JPanel implements ActionListener,MouseListener,MouseMotionListener{
static Image Hero= new ImageIcon(Main.InstallPath+"hero/herocrouch.png").getImage();
... loading few images.
float alphacomposite = 0;
Timer time;
Font font = new Font("Arial", Font.PLAIN, 20);
static boolean handCursor = false;
static boolean beating = false;
static int crouchdecal = 0;
static inputBoy Input = new inputBoy();
Objects[] visible;
human[] humans;
static Particle[] particles;
static Particle[] Bloodparticles;
static Particle[] RainParticles;
private double rainDarkness = 0.60f;
private Color SkyColor = new Color(129, 107, 112);
private Color flashColor = new Color(253,239,74);
public Box(){
addKeyListener(new AL());
addMouseListener(this);
addMouseMotionListener(this);
setFocusable(true);
setBackground(Color.BLACK);
time = new Timer (Main.FPStiming, this);
time.start();
Main.maps.setCurrentMap(Main.maps.getCurrentMap().getID());
particles = new Particle[20];
Bloodparticles = new Particle[20];
RainParticles = new Particle[600];
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0, 1200, 500); //On supprime le fond
g2d.setColor(SkyColor);
g2d.fillRect(0, 0, 1200, 200); //On dessine le fond
for (int i=0;i<250;i++){
g2d.setColor(new Color(SkyColor.getRed()+i/2,SkyColor.getGreen()+i/2,SkyColor.getBlue()+i/2));
g2d.fillRect(0, 200+i, 1200, 2);
i++;
}
g2d.setColor(Color.BLACK);
ThunderEffect(g2d);
}
private void ThunderEffect(Graphics2D g2d){
if (Nature.Thunder!=null && Nature.Thunder.alive){
Nature.Thunder.ttl--;
Stroke s = g2d.getStroke();
for (int i = 0; i<9;i++){
//System.out.println("point en:"+Nature.Thunder.getTree()[i+1][0]+" , "+Nature.Thunder.getTree()[i+1][1]+ "case :"+i);
g2d.setStroke(new BasicStroke(5));
if (!(Nature.Thunder.getTree()[i+1][0]==0)){
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1f));
g2d.setColor(Color.WHITE);
g2d.drawLine(Nature.Thunder.getTree()[i][0], Nature.Thunder.getTree()[i][1], Nature.Thunder.getTree()[i+1][0], Nature.Thunder.getTree()[i+1][1]);
//lumièe autour du l'éclair
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.6f));
g2d.setStroke(new BasicStroke(10));
g2d.drawLine(Nature.Thunder.getTree()[i][0], Nature.Thunder.getTree()[i][1], Nature.Thunder.getTree()[i+1][0], Nature.Thunder.getTree()[i+1][1]);
//lumière lointaine
}
if (Nature.Thunder.getTree()[i+1][1]>300){ // quand l'eclair touche le sol
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.09f));
g2d.fillRect(0, 0,1200, 505);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1f));
}
}
g2d.setStroke(s);
g2d.setColor(Color.BLACK);
if (Nature.Thunder.ttl<1){
Nature.Thunder.alive=false;
}
}
}
public void FadeOut(Graphics2D g2d){
}
public void paintMenu(Graphics2D g2d){
}
public void paintDeath(Graphics2D g2d){
}
public void paintSettings(Graphics2D g2d){
}
public void paintInventory(Graphics2D g2d){
}