我在一个Panel上画了背景 再加了两个Panel 用不同的线程在子Panel上显示动画,而动画每显示一祯都要擦除上一祯的,有什么办法在擦除的时候不把背景给擦掉了啊?

解决方案 »

  1.   

    背景画到JFrame中,动画在JPanel中,将JPanel设置透明,加入到JFrame里
    也可以使用层,看你喜欢了
      

  2.   

    弱弱的问下  怎样将JPanel 设透明啊 用哪个方法?
      

  3.   

    还有怎么样才能在JFrame中画图呢
       好象只能在JPanel上画吧?
      

  4.   

    给你写个例子:import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;import javax.swing.JFrame;
    import javax.swing.JPanel;public class Test extends JFrame {
    private JPanel framePane = null; // 画背景图片的JPanel private MovePane move = null; // 画动画的类,继承:JPanel,接口:Runnable public Test() {
    super("Test");
    move = new MovePane();
    framePane = new JPanel() {
    public void paintComponent(Graphics g) {
    g.setColor(new Color(100, 155, 100));
    g.fillRect(0, 0, 300, 200);
    g.setColor(new Color(80, 135, 80));
    g.setFont(new Font("黑体", 1, 25));
    g.drawString("画背景图片", 80, 30);
    }
    };
    framePane.setLayout(new BorderLayout()); // 设置为JFrame所用的BorderLayout
    this.setContentPane(framePane); // 用重写了paintComponent(Graphics g)方法的JPanel替换JFrame本身的Container
    framePane.add(move);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.setVisible(true);
    } public static void main(String[] arg) {
    new Test();
    } class MovePane extends JPanel implements Runnable {
    private Thread thread = null;

    private int i = 0; public MovePane() {
    this.setOpaque(false);
    thread = new Thread(this);
    thread.start();
    } public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(new Color(0, 0, 0));
    g2d.drawString("这里用一个线程来画动画", i += 10, 100);

    } public void run() {
    while (true) {
    this.repaint();
    if (i == 150)
    i = 0;
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }}