There is this JFrame
holding JComponent
that being drawn 30
times a second. The problem appears to be with flashing or blinking effect when drawing the BufferedImage
like it gets drawn on each other un-accordingly..
Also, There is a second problem wheres the Window constructor is executing really late, sometimes when the program actually need to use this object if EventQueue.invokeLater
is applied, is it critical to have it there or leave it the way it is?
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class Window{
private JFrame window;
private CanvasComponent component;
public Window(Input input){
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int hostMonitorWidth = gd.getDisplayMode().getWidth();
int hostMonitorHeight = gd.getDisplayMode().getHeight();
window = new JFrame();
component = new CanvasComponent(hostMonitorWidth, hostMonitorHeight);
window.setUndecorated(true);
window.setBackground(new Color(0,0,0,200));
window.setLayout(new GridLayout());
window.setSize(hostMonitorWidth, hostMonitorHeight);
window.getContentPane().add(component);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.requestFocus();
window.setFocusableWindowState(true);
window.addKeyListener(input);
}
public void draw(){
component.draw();
}
private class CanvasComponent extends JComponent{
public CanvasComponent(int hostMonitorWidth, int hostMonitorHeight){
super.setSize(hostMonitorWidth, hostMonitorHeight);
super.setBackground(new Color(0,0,0,0));
super.setDoubleBuffered(true);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
if(GraphicsBatch == null) return;
BufferedImage image = GraphicsBatch.getImage();
g.drawImage(image,0,0,null);
}
public void draw(){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
repaint();
}});
}
}
}
Window.draw()
is being called 30 times a sec.