2013-11-01 167 views
2

我想製作一個簡單的java遊戲,其中一個大球吃小球。我做到了。現在我想讓大球在吃小球時「長大」,但我不知道如何調整Java中的圖像大小。提前致謝!Java JPanel調整大小圖像

MovableBall.java:

import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.event.KeyEvent; 

import javax.swing.ImageIcon; 

public class MovableBall { 

    private String craft = "ball.png"; 

    private int dx; 
    private int dy; 
    private int x; 
    private int y; 
    private int width; 
    private int height; 
    private int mx = 200, my = 150; 
    private Image image; 

    public MovableBall() { 
     ImageIcon ii = new ImageIcon(this.getClass().getResource(craft)); 
     image = ii.getImage(); 

     x = 5; 
     y = 5; 
     width = image.getWidth(null); 
     height = image.getHeight(null); 
    } 

    public void move() { 
     x += dx; 

     y += dy; 
    } 

    public int getX() { 
     return x; 
    } 

    public int getY() { 
     return y; 
    } 

    public Image getImage() { 
     return image; 
    } 

    public void keyPressed(KeyEvent e) { 

     int key = e.getKeyCode(); 

     if (key == KeyEvent.VK_LEFT) { 
      dx = -1; 
     } 

     if (key == KeyEvent.VK_RIGHT) { 
      dx = 1; 
     } 

     if (key == KeyEvent.VK_UP) { 
      dy = -1; 
     } 

     if (key == KeyEvent.VK_DOWN) { 
      dy = 1; 
     } 
    } 

    public void keyReleased(KeyEvent e) { 
     int key = e.getKeyCode(); 

     if (key == KeyEvent.VK_LEFT) { 
      dx = 0; 
     } 

     if (key == KeyEvent.VK_RIGHT) { 
      dx = 0; 
     } 

     if (key == KeyEvent.VK_UP) { 
      dy = 0; 
     } 

     if (key == KeyEvent.VK_DOWN) { 
      dy = 0; 
     } 
    } 

    public Rectangle getBounds() { 
     return new Rectangle(x, y, width, height); 
    } 
} 

FoodBoard.java:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 

import javax.swing.JPanel; 
import javax.swing.Timer; 

public class FoodBoard extends JPanel implements ActionListener { 

    private Timer timer; 
    private MovableBall ball; 
    private Food[] foods = { new Food(), new Food(), new Food(), new Food(), 
      new Food(), new Food() }; 
    private int B_WIDTH; 
    private int B_HEIGHT; 

    public FoodBoard() { 
     addKeyListener(new TAdapter()); 
     setBackground(Color.black); 
     setSize(400, 300); 
     setDoubleBuffered(true); 
     ball = new MovableBall(); 

     // WURRY important: 
     setFocusable(true); 
     // every 5 ms the timer will call the actionPerformed() method 
     timer = new Timer(5, this); 
     timer.start(); 
    } 

    public void addNotify() { 
     super.addNotify(); 
     B_WIDTH = getWidth(); 
     B_HEIGHT = getHeight(); 
    } 

    public void checkIfEaten() { 
     Rectangle r3 = ball.getBounds(); 

     for (int j = 0; j < foods.length; j++) { 
      Food a = foods[j]; 
      Rectangle r2 = a.getBounds(); 

      if (r3.intersects(r2)) { 
       a.setVisible(false); 
      } 
     } 
    } 

    public void paint(Graphics g) { 
     super.paint(g); 
     Graphics2D g2d = (Graphics2D) g; 

     for (int i = 0; i < foods.length; i++) { 
      Food f = foods[i]; 
      if (f.isVisible()) { 
       g2d.drawImage(f.getImage(), f.getX(), f.getY(), this); 
      } 
     } 
     g2d.drawImage(ball.getImage(), ball.getX(), ball.getY(), this); 

     Toolkit.getDefaultToolkit().sync(); 
     g.dispose(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     ball.move(); 
     checkIfEaten(); 
     repaint(); 
    } 

    private class TAdapter extends KeyAdapter { 
     public void keyReleased(KeyEvent e) { 
      ball.keyReleased(e); 
     } 

     public void keyPressed(KeyEvent e) { 
      ball.keyPressed(e); 
     } 
    } 
} 
+1

您近了 - 看看@ http://docs.oracle.com/javase/tutorial/2d/images/drawimage.html - 您可以選擇繪製縮放圖像,這可以模擬增長。雖然它可能會得到像素化,所以如果需要,您可能需要加載較大分辨率的圖像。 –

回答

1

您可以調整這樣的圖像,

Image new_image = old_image.getScaledInstance(new_width, new_height, java.awt.Image.SCALE_SMOOTH) ; 

要確保球不超過最大尺寸,

if(prevImage.getWidth(null) >= max_width || prevImage.getHeight(null) >= max_height) 
{ 
    System.out.println("MAX Size reached !"); 
} 
else 
{ 
    // increase the size 
    Image new_image = old_image.getScaledInstance(new_width, new_height, java.awt.Image.SCALE_SMOOTH) ; 
} 
+0

嗨。我寫了'Image prevImage = ball.getImage(); \t \t \t \t ball.setImage(prevImage.getScaledInstance( \t \t \t \t \t \t prevImage.getWidth(空)+ 10, \t \t \t \t \t \t prevImage.getWidth(空)+ 10,Image.SCALE_DEFAULT)) ;在ifIfEaten()內部的' ,並且它導致球永遠變大。任何想法?謝謝! – RominaV

+0

您可以檢查圖像是否已達到最大尺寸,然後停止增加尺寸。看我的編輯。 –

+0

謝謝。你知道爲什麼它不斷執行if中的代碼嗎? – RominaV

1

如果你有一個圖片對象,你真的不需要擔心調整其大小,只需用

yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT); 

你想改變的newWidth/newHeight參數,但除此之外,你應該去好!你可以讀更多here.

希望這有助於!

+0

嗨!我把它放在'checkIfEaten()'中:'Image im = ball.getImage(); \t \t \t \t im.getScaledInstance(im.getWidth(null)+ 5,im.getHeight(null)+ 5,im.SCALE_DEFAULT);' 但它什麼也沒做。我是否把它寫在錯誤的地方?非常感謝! – RominaV

+0

那麼,我不習慣Java的這部分,但我知道,im.SCALE_DEFAULT是不正確的。 SCALE_DEFAULT是Image類中定義的變量,所以當你使用它時,你必須做Image.SCALE_DEFAULT,它應該可以工作。另外,在做getWidth()時,我不認爲你必須放置null,但這不會損害你的遊戲。 – user2277872

+0

問題是,一旦我縮放它,我就沒有將它設置爲球的形象。感謝您的迴應:)我現在正面臨着我在Rustagi的回答中所評論的問題:P – RominaV