我正在使用各种精灵表加载到宇宙飞船中。Graphics.drawImage() 的文档说明参数是
boolean Graphics.drawImage(Image img,
int dstx1, int dsty1, int dstx2, int dsty2,
int srcx1, int srcy1, int srcx2, int srcy2,
ImageObserver observer);
Run Code Online (Sandbox Code Playgroud)
但是,文档说dstx1和dsty2是左上角的坐标,当你使用dstx2和dsty2指定绘制的区域时,尺寸是(dstx2-dstx1)和(dsty2-dsty1)。除非我误解了该功能的工作原理,否则这似乎只会从角落加载图像的一部分。如何绘制未连接到左角的图像的一部分,以绘制精灵表的不同部分?
我已经按照无数其他教程的建议来改变JFrame的背景颜色,但我没有运气.这是我的初始化和创建框架的代码
package frameTests;
import javax.swing.*;
import java.awt.*;
@SuppressWarnings("serial")
public class Foo extends JPanel {
public static void frameInit(Foo foobar){
// window initialization
JFrame frame = new JFrame("Spaceship Defenders!");
frame.add(foobar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.black);
frame.setSize(500,500);
frame.setVisible(true);
frame.setLocationRelativeTo(null);// center frame in screen.
}
public static void main(String[] args) throws InterruptedException{
Foo bar = new Foo();
frameInit(bar);
}
}
Run Code Online (Sandbox Code Playgroud)
