急)帮忙改下代码。(j2se)。谢谢。 在原基上改,让它不闪屏,用双缓冲,
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;public class Repain extends JFrame{
  
 private static final long serialVersionUID = 1L;
 int x = 10,y = 10;
 
 Repain()
 {
  this.getContentPane().setBackground(Color.RED);
  this.setBounds(300, 300, 600, 480);
  this.setResizable(false);
  
   new Thread (new PainThread()).start();  
 }
 public void paint(Graphics g)
 {
  super.paint(g);
 
  
  Color c = g .getColor();
  g .fillOval(x, y, 40, 40); 
  g .setColor(c);
 }
 public class PainThread implements Runnable {
  public void run() {
   while(true) {
    repaint();
    try {
     Thread.sleep(10);
     x = x + 1;
     y += 1;
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 } 
 public static void main(String[] args) {  
  new Repain().setVisible(true);
 }

解决方案 »

  1.   

    /*自已给自已顶下。唉!杯具呀,搞了半天还是要我自已来把它弄出来。
    我给我自已顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶..........
    **/import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.JFrame;
    import javax.swing.JPanel;public class Repain extends JPanel{//最好用JPanel来画图,
       
     private static final long serialVersionUID = 1L;
     int x = 10,y = 10;
     JFrame jf = new JFrame();
      
     public Repain()
     {  
      this.setBackground(Color.RED);
      jf.setLayout(new BorderLayout());
      jf.setBounds(300, 300, 600, 480);
      jf.add(this,BorderLayout.CENTER);
      jf.addWindowListener(new WindowAdapter(){
    @Override
    public void windowClosing(WindowEvent e) {
    super.windowClosing(e);
    System.exit(0);
    }   
      });
      jf.setResizable(false); 
      jf.setVisible(true);
      new Thread (new PainThread()).start();   
     }
     public void paintComponent(Graphics g)
     {
      super.paintComponent(g);//这不可少。
      g .fillOval(x, y, 40, 40);  
     }
     public class PainThread implements Runnable {
      public void run() {
      while(true) {
      repaint();
      try {
      Thread.sleep(10);
      x = x + 1;
      y += 1;
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      }
      }
     }  
     public static void main(String[] args) {   
      new Repain();
     }
    }  
    //最后再顶下。呜呜呜呜呜呜.............
      

  2.   

    要用双缓冲用awt里的组件,要带透明效果的话用swing里的
      

  3.   


    知道,兄弟,这个可根据实际应用改变run()方法中的while( )中的循环条件来控制吧,不过呢,也谢谢哥哥指出问题。
      

  4.   

    你说的是要这样吗?
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;public class DoubleBuffer extends Frame{

    private static final long serialVersionUID = 1L;
    private int x = 0,y = 0;
    Image offScreenImage = null;
    boolean isRun = true;

    public DoubleBuffer()
    {
    this.setBounds(200, 200, 600, 400);
    this.setResizable(false);
    this.setBackground(Color.RED);
    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e) {
    super.windowClosing(e);
    System.exit(0);
    }
    });
    this.setVisible(true);
    new Thread(new PainThread()).start();
    }

    public void paint(Graphics g)
    {
    g.fillOval(x, y, 40, 40);
    }
    public void update(Graphics g) {

    if(offScreenImage == null) {
    offScreenImage = this.createImage(600, 400);
    }
    Graphics gOffScreen = offScreenImage.getGraphics();
    Color c = gOffScreen.getColor();
     gOffScreen.setColor(Color.RED);
    gOffScreen.fillRect(0, 0, 600, 400);
     gOffScreen.setColor(c);
     paint(gOffScreen);
     g.drawImage(offScreenImage, x, y, null);
    }

    public static void main(String[] args) {
    new DoubleBuffer();
    } public class PainThread implements Runnable {
    public void run() {
    while (isRun) {
    repaint();
    try {
    Thread.sleep(10);
    x = x + 1;
    y += 1;
    if(x > 600 || y > 400)
    isRun = false;
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
    但是我只会会Frame进行,能不能给我说下,在JFrame中如何实现,如上那样实现。谢谢。