2013年12月4日星期三

Window screen flicker problem

Write a snow program , but the interface is blinking , who can help me correct it, very grateful !
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;
class Table extends Frame implements ActionListener
{
Button start=new Button("开始");
int[] xx = new int [100];
    int[] yy = new int [100];
    int[]fs1 = new int [100];  
    Font fs = null;
public Table()
{
super("雪花飞");
setSize(1200,800); 
setBackground(Color.black); 
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new FlowLayout());
add(start);
start.addActionListener(this);
validate();
addWindowListener(new WindowAdapter()
{
 public void windowClosing (WindowEvent e){System.exit(0);}
});
}

 public void actionPerformed(ActionEvent e)
  {
if(e.getSource()==start)begin();
  }
  public void begin()

 for(int i=0;i<xx.length;i++)
{  
  xx[i]=(int)(1200*Math.random());
  yy[i]=(int)(800*Math.random());
  fs1[i]=(int)(20*Math.random()+12);  
   }   
new Thread(){
 public void run()
{
 while(true)
 {
 for(int i=0;i<xx.length;i++)
{     
 if((yy[i]>800)||(yy[i]<0))yy[i] =0;
 yy[i]++; 
 }
 repaint();
try{ Thread.sleep(10);} catch(InterruptedException e)
{System.err.println("Thread interrupted");}
}
 }
}.start();
}
public void paint(Graphics g)
{
  super.paint(g);
  Image img=new ImageIcon("11.jpg" ).getImage();
  g.drawImage(img,0,0,1200,800,null);
      for(int i=0;i<xx.length;i++)
{  
Font fs =new Font("宋体",Font.BOLD,fs1[i]);     
g.setColor(Color.white);
g.setFont(fs);
g.drawString("*",xx[i],yy[i]);//画雪花
}
}
public static void main(String args[])
{
new Table();
}
}

------ Solution ------------------------------------- -------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Table extends Frame implements ActionListener {
Button start = new Button("开始");
int[] xx = new int[100];
int[] yy = new int[100];
int[] fs1 = new int[100];
Font fs = null;
Image offScreen = null;

public Table() {
super("雪花飞");
setSize(1200, 800);
//setBackground(Color.black);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new FlowLayout());
add(start);
start.addActionListener(this);
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == start)
begin();
}

public void begin() {
for (int i = 0; i < xx.length; i++) {
xx[i] = (int) (1200 * Math.random());
yy[i] = (int) (800 * Math.random());
fs1[i] = (int) (20 * Math.random() + 12);
}
new Thread() {
public void run() {
while (true) {

repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.err.println("Thread interrupted");
}
}
}
}.start();
}

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 1200, 800);
//Image img = new ImageIcon("11.jpg").getImage();
//g.drawImage(img, 0, 0, 1200, 800, null);
for (int i = 0; i < xx.length; i++) {
Font fs = new Font("宋体", Font.BOLD, fs1[i]);
g.setColor(Color.white);
g.setFont(fs);
g.drawString("*", xx[i], yy[i]);// 画雪花
}

for (int i = 0; i < xx.length; i++) {
if ((yy[i] > 800) || (yy[i] < 0))
yy[i] = 0;
yy[i]++;
}
}

public void update(Graphics g) {
if(offScreen == null) {
offScreen = createImage(1200, 800);
}
Graphics gOffScreen = offScreen.getGraphics();
paint(gOffScreen);
g.drawImage(offScreen, 0, 0, null);
}

public static void main(String args[]) {
new Table();
}
}



I'll give you a picture annotated
------ For reference only --------------------------- ------------
double buffering , rewritten update function
------ For reference only --------------- ------------------------

------ For reference only ---------------------- -----------------
er er, online search, but the method is not overridden , the method gives better downstairs ! Thanks!
------ For reference only -------------------------------------- -
er er, thank you, online search is rewritten, but did not give way to speak with you this fact ! Thank you !
------ For reference only -------------------------------------- -
  said summed up , novice or not ah
------ For reference only ---------------------------------------
        said summed up , novice or not ah     

I am also a novice, if you do not , you can look Refbacks tanks picture version of the video , which will have a specific update function to rewrite , you want to see , then you can download http://pan.baidu.com/ share / link? shareid = 379477633 & uk = 202436276
reasons:
1. refresh frequency too fast redraw , paint method is not yet complete
2. displayed one by one
solution: all the things painted on the virtual image , disposable displayed

------ For reference only ---------------------------------- -----


offScreen has been declared over, first determine whether it is empty , then there is no longer initialized

没有评论:

发表评论