如何编码高效的背景图像 [英] How to code efficient background image
本文介绍了如何编码高效的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请我用 java 编写一个 2D 游戏,但我的背景图像没有加载 JFrame,因为它可以调整大小
please I am coding a 2D game with java,but my background image does not load up with the JFrame it loads up when I drag the JFrame, since it resizeable
import javax.swing.*;
import java.awt.*;
public class imageLearn1 extends JFrame{
private Image im;
private JPanel p1;
public imageLearn1(){
this.setSize(300,400);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1=new paintIt();
this.add(p1);
}
class paintIt extends JPanel{
public paintIt(){
ImageIcon ima=new ImageIcon("ballfall3.jpg");
im=ima.getImage();
im=im.getScaledInstance(200,-1,Image.SCALE_DEFAULT);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(im!=null){
g.drawImage(im,0,0,300,400,this);
}
}
}
public static void main(String[]args){
new imageLearn1();
}
}
推荐答案
您必须先将组件添加到框架中,然后才能使框架可见.代码的顺序应该是:
You must add the components to the frame before making the frame visible. The order of the code should be:
p1=new paintIt();
this.add(p1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,400);
this.setLocationRelativeTo(null);
this.setVisible(true);
此外,类名应该以大写字符开头.你的类名应该是PaintIt".
Also, class names should start with an upper case character. You class name should be "PaintIt".
这篇关于如何编码高效的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文