简体   繁体   English

使用Graphics2D缩放图像

[英]Scale an image with Graphics2D

In a game that I am making I want the power ups to be airdrops. 在我正在制作的游戏中,我希望功率提升是空投。 I have a image of a crate that I want to be draw bigger than I have drawn it. 我有一个板条箱的图像,我想画的比画的还要大。 Then I want the image to become progressively smaller with times so that it looks like it's dropped from the air. 然后,我希望图像随着时间逐渐变小,以使其看起来像是从空中掉下来的。 I have crafted this code which I think should work but for some reason it doesn't. 我编写了此代码,我认为它应该可以工作,但是由于某种原因,它不起作用。

if(power.getPowerUp()){
   double crateX = (int) power.getX();
   double crateY = (int) power.getY();
   Image crate = power.getCrate();
   int crateW = crate.getWidth(null) + 100;
   int crateH = crate.getHeight(null) + 100;
   if(crateW > 64){
      g2d.drawImage(
         power.getCrate(), (int) crateX, (int) crateY, crateW, crateH, this );
      crateX += .5;
      crateX += .5;
      crateW -= 1;
      crateH -= 1;
   }
   else {
      g2d.drawImage(
         power.getCrate(), (int) crateX, (int) crateY, crateW, crateH, this );
   }
}

I have this method inside paintComponent(Graphics g) and I use a Thread to repaint the graphics. 我在paintComponent(Graphics g)内部有此方法,并且使用Thread重绘了图形。 Why doesn't it work? 为什么不起作用? Does it have to do with something that I use values from another class? 它是否与我使用另一个类的值有关? How would I make it so that the progressively smaller? 我如何使它逐渐变小?

You are recreating the crateW and crateH variables everytime the method is called. 每次调用该方法时,都将重新创建crateWcrateH变量。 Then the method goes into either the if or else block, it never goes into the else (what you want) after having gone into the if block. 然后该方法进入if或else块,进入if块后再也不会进入else(您想要的)。 In other words, the width and heights are the same next time the method is called. 换句话说,下次调用该方法时,宽度和高度相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM