I am making a JFrame and drawing a rectangle onto it. It doesn't work, sometimes it is completely black and sometimes completely white, here are my methods.
So the render method is called twice because the first time it creates the buffer, also ignore framerate, it is irrelavent right now.
Edit1: I solved one problem: It is drawing a rectangle now, but sometimes it is just displays a white screen. I still need to solve that problem
Edit2: I am not only looking for the solution, I am also looking for the reason my problem happens, so I am not just blindly writing code.
public MyFrame(String title,int width,int height)
{
super(title);
this.setSize(width,height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setUndecorated(true);
this.addKeyListener(new KeyInput());
this.setVisible(true);
}
public void draw(Graphics2D g,int arg)
{
g.setColor(new Color(0,0,0,255));
g.fillRect(0,0,SIZE,SIZE);
g.setColor(new Color(255,255,255));
g.fillRect(0,0,50,50);
}
public void render(int arg)
{
BufferStrategy bs=this.getBufferStrategy();
if(null==bs)
{
this.createBufferStrategy(3);
}
else
{
Graphics2D g=(Graphics2D)bs.getDrawGraphics();
g.setColor(new Color(0,0,0,255));
g.fillRect(0,0,this.getWidth(),this.getHeight());
BufferedImage canvas=new BufferedImage(SIZE,SIZE,2);
int s=Math.min(this.getWidth(),this.getHeight());
g.drawImage(canvas,(this.getWidth()-s)/2,(this.getHeight()-s)/2,s,s,this);
Graphics2D g2=canvas.createGraphics();
this.draw(g2,arg);
bs.show();
g.dispose();
g2.dispose();
}
}
public static void main(String[] args)
{
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
FRAME=new MyFrame("Insert name here!",d.width,d.height,1);
FRAME.render(0);
FRAME.render(0);
}
Edit: this is no longer neessary, I have managed to solve the problem, thank you for your help anyways.