以下代码定义了我的位图:
Resources res = context.getResources();
mBackground = BitmapFactory.decodeResource(res,
R.drawable.bg2);
//scale bitmap
int h = 800; // height in pixels
int w = 480; // width in pixels
Bitmap scaled = Bitmap.createScaledBitmap(mBackground, w, h, true); // Make sure w and h are in the correct order
Run Code Online (Sandbox Code Playgroud)
...以下代码用于执行/绘制它(未缩放的位图):
c.drawBitmap(mBackground, 0, 0, null);
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何设置它来绘制以"Bitmap scaled"形式返回的缩放位图,而不是原始?
我一直在关注Java Game Programming for Beginners教程系列,并希望通过应用背景图像进行实验.不幸的是,当我通过这个paintComponent方法渲染它时,它会随着我的精灵移动(虽然连续一个单位而不是五个); 当我通过paint方法渲染它时,我得到一个奇怪的,闪烁的盒子,它与该setBackground (color)属性中指定的颜色相匹配,JFrame并且它与精灵一起移动与前一个实例(内部paintComponent)相同.
我如何编码图像以保持静态,因为背景应该是?
码:
public class JavaGame extends JFrame{
int x, y;
private Image dbImage;
private Graphics dbg;
Image ghost;
Image bg;
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT){
if(x <= 8)
x = 8;
else
x += -5;
}
if(keyCode == e.VK_RIGHT){
if(x >= 235)
x = 235;
else
x += +5;
}
if(keyCode == …Run Code Online (Sandbox Code Playgroud)