用ExtendendJFrame显示图像的java
我已经为我的平铺做了一些代码,在这里我画了两个对象,但是我不能在平铺中加载背景,因为它扩展了JFrame。 我做错了什么? 我使用缓冲图像读取图片,使其适合任何屏幕大小,将其设置为JLabel,但仍然无法工作。 Square1和障碍物是导入的矩形图形,一个作为玩家移动,另一个作为障碍物
public class BasicTwoPlayer extends JFrame implements Runnable {
static Square1 p1 = new Square1();
static Square2 p2 = new Square2();
static obstacle o1 = new obstacle();
static Thread p1t;
static Thread p2t;
static Thread o1t;
KeyADAPT a = new KeyADAPT();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final int widthScreen = screenSize.width;
final int heightScreen = screenSize.height;
public BasicTwoPlayer() {
try {
BufferedImage backgroundImage = ImageIO.read(new File("P://My Pictures//background1.jpg"));
JLabel background = new JLabel(new ImageIcon(backgroundImage));
Image scaleBackground = backgroundImage.getScaledInstance(widthScreen, heightScreen, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(scaleBackground);
setContentPane(new JLabel(imageIcon));
addKeyListener(a);
setSize(new Dimension(widthScreen, heightScreen));
setUndecorated(true);
setBackground(Color.WHITE);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p1t = new Thread(p1);
p2t = new Thread(p2);
o1t = new Thread(o1);
p1t.start();
p2t.start();
} catch (IOException ex) {
Logger.getLogger(BasicTwoPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
background.paint(g);
}
public void draw(Graphics g) {
p1.draw(g);
o1.draw(g);
repaint();
}
public static void main(String[] args) {
BasicTwoPlayer sf = new BasicTwoPlayer();
Thread mt = new Thread(sf);
mt.start();
t1.start();
}
# 1 楼答案
你会惊讶于我多么愿意学习如何回答自己的问题;)
你需要了解:
理解这一点也无妨:
“如何添加背景图像”
您将想要了解:
根据你的指示,以及你迄今为止提出的所有其他问题。它们将帮助你避免常见的陷阱
我不怀疑你会遇到一些问题来处理这些概念,我鼓励你玩一玩,如果和什么时候,发布一个问题出来,特别是