這是此程序的代碼。如果我加載圖像公共無效paintComponent(Graphics g){}方法的圖像加載,但是如果我加載他們從另一個類他們不。圖像未加載/更新
主類:
public class main {
static GUI GUI = new GUI();
static render render = new render();
static loader loader = new loader();
public static void main(String [] args) {
frame.start();
loader.start();
}
}
幀類:
public class GUI implements Runnable {
public void start() {
new Thread(this).start();
}
public void run(){
JFrame frame = new JFrame();
System.out.println("frame starting");
frame.setSize(700,600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setTitle("Project ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.addMouseListener(render);
frame.add(render);
frame.setVisible(true);
}
}
呈現類
public class render extends JPanel implements ActionListener {
Timer tm = new Timer(7, this);
loader loader = new loader();
public render() {
tm.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(loader.Getbackground(), -100,-400,null);
public void actionPerformed(ActionEvent e) {
repaint();
}
}
loader類
public class loader implements Runnable {
Image background;
public void start() {
new Thread(this).start();
}
public void run() {
ImageIcon backgroundhold = new ImageIcon(render.class.getResource("resources/Background.png"));
background = backgroundhold.getImage();
setbackground(background);
}
public void setbackground(Image background) {
this.background = background;
}
public Image Getbackground() {
return background;
}
public void setbackground(Image background){
this.background = background;
}
}
當程序啓動時,它會打開一個沒有圖像的空幀。我究竟做錯了什麼?
阿倫
你的代碼必須被檢查,你甚至不會在類Render中打開'{'',''frame''沒有聲明... – MCHAppy
對不起,代碼是手動添加的,我可能有忘記添加它的一部分。 – arunptl100
我已經改正了 – arunptl100