When moving a JFrame, I sometimes see that part of it, that is the part where the frame and its old position overlap, will not refresh.
Here's an Horrible Paintjob Of An Explanation.
My theory that I hold dear is that by default Java will not update the contents of a JFrame that are hidden behind another window, and that it sometimes fails to understand that this other window is in fact the JFrame itself, but before the move you made it do (and where did that other dog come from...).
Would there be a way to force the JFrame to either entirely repaint itself regardless of its environment or at least make it re-check said environment? And where/how to get the window moving event's interpretation method at the end of which I'd place it?
Code example:
MyFrame:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class MyFrame extends JFrame{
private static final long serialVersionUID = 1L;
public MyFrame(){
this.setTitle("2D view");
this.setSize(640,480);
MyPanel mp=new MyPanel();
this.setContentPane(mp);
Timer timer=new Timer();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
timer.cancel();
MyFrame.this.dispose();
}
public void windowOpened(WindowEvent e){
if(MyFrame.this.isFocused()){
mp.requestFocus();
}
}
});
timer.scheduleAtFixedRate(new TimerTask(){
public void run() {
mp.repaint();
}
},0,1000/60);
}
}
MyPanel:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MyPanel extends JPanel{
private static final long serialVersionUID = 1L;
private static final int tsx=15;
private static final int tsy=15;
private int vscis;
private int hscis;
public MyPanel(){
this.vscis=this.hscis=-1;
}
@Override
public void setBounds(int x,int y,int width,int height){
super.setBounds(x, y, width, height);
this.vscis=-1;
this.hscis=-1;
}
public void paintComponent(Graphics g){
int w=this.getWidth(),h=this.getHeight();
if(this.hscis==-1){
this.hscis=(((w-(1+MyPanel.tsx))/2)/MyPanel.tsx)+1;
}
if(this.vscis==-1){
this.vscis=(((h-(1+MyPanel.tsy))/2)/MyPanel.tsy)+1;
}
int i,j,posY=(h-((vscis*2+1)*tsy))/2,bposX=(w-((hscis*2+1)*tsx))/2,posX;
Color clearC=new Color(11,66,66);
Color clearW=new Color(0,0,0);
for(i=0;i<=vscis*2;++i){
posX=bposX;
for(j=0;j<=hscis*2;++j){
g.setColor(clearC);
g.fillRect(posX,posY,tsx,tsy);
g.setColor(clearW);
g.drawLine(posX, posY, posX+tsx, posY);
g.setColor(clearW);
g.drawLine(posX, posY, posX, posY+tsy);
posX+=tsx;
}
posY+=tsy;
}
}
}
Instantiate a MyFrame, and move it a few times on your screen. I use Win7 and I have the windows' contents not show as I move them (unchecked an option somewhere). This glitch is not reproductible by nature, but I have provided [a screenshot][5] on another website where it is apparent.
[5] user.oc-static (dot) com/upload/2017/03/10/148918317799_Sans%20titrescreencap.png
Can't post more than two links, figures.