如何解决Java窗口中添加背景图后,再添加图片无法显示?

Pool.java
package com_Tentact_FishGame; //捕鱼达人游戏

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Pool extends JPanel{
Pool pool;
Fish fish;
Net net;
BufferedImage poolImage;
public Pool() throws IOException {
poolImage=ImageIO.read(new File("src/image/bg.jpg")); //读取图片
}
@Override //读取图片后画图片
public void paint(Graphics g) {
g.drawImage(poolImage,0,0,null);
//g.drawImage(fish.fishImage,fish.point.x,fish.point.y,null);
** //就是上面这行代码,添加进去后就无法正常显示图片了。**
** //求大佬解决呀**
//g.drawImage(net.netImage,net.point.x,net.point.y,null);
}
}

Fish.java
package com_Tentact_FishGame;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class Fish {
Fish fish;
Point point;//用点来代替鱼的位置
BufferedImage fishImage;
Random random=new Random();//随机生成数
public Fish() throws IOException {
fishImage=ImageIO.read(new File("src/image/fish01_01.png"));
int x=random.nextInt(80);//生成x坐标
int y=random.nextInt(50);//生成y坐标
point=new Point(x,y);//生成鱼的位置
}
}

Windows.java
package com_Tentact_FishGame;

import java.io.IOException;

import javax.swing.JFrame;

public class Window extends JFrame{
public Window() throws IOException {
this.setSize(800,500);
this.setTitle("捕鱼达人");
this.setLocationRelativeTo(null);//居中
this.setResizable(false);//不可调节
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Pool pool=new Pool();
this.add(pool);
this.setVisible(true);
}
}

Test.java
package com_Tentact_FishGame;

import java.io.IOException;

public class Test {
public static void main(String[] args) throws IOException {
Window window= new Window();
}
}

1个回答

g.drawImage(poolImage,0,0,null); 这里已经绘制过一次图片了,这个图片已经显示了。
去掉这个,放开另一个绘图方法呢?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问