我在画了图的JFrame上再加个JPanel画图,就会出现图片闪屏闪得很严重,因为是在做小游戏想背景和人物分开来画如何解决这个问题
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Baground extends JFrame{
Image baground;
Random random;
int bgNum;
People p=new People();
public Baground(){
JFrame frame=new JFrame();
this.setSize(1200,336*2-100);
this.setVisible(true);
this.setResizable(false);
random=new Random();
random.setSeed(49849);
bgNum=(int)(random.nextDouble()*10);
System.out.print(bgNum);
baground=Toolkit.getDefaultToolkit().getImage("素材/背景图片/"+bgNum+".gif");
this.getContentPane().add(p);
}
public void paint(Graphics g){
p.paint(g);
int w=baground.getWidth(this);
int h=baground.getHeight(this);
g.drawImage(baground,1,0,w+400,h*2-100,this);
}
public static void main(String[] args){
new Baground();
}
}
class People extends JPanel{
Image peopleImage;
public People(){
peopleImage=Toolkit.getDefaultToolkit().getImage("素材/people1/1.gif");
}
public void paint(Graphics g){
super.paint(g);
int a=peopleImage.getWidth(this);
int b=peopleImage.getHeight(this);
g.drawImage(peopleImage,150,350,this);
}
}