2

大きなボールが小さなボールを食べるシンプルな Java ゲームを作ろうとしています。私はそれをしました。大きなボールが小さなボールを食べたときに「成長」したいのですが、Javaで画像のサイズを変更する方法がわかりません。前もって感謝します!

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);
        }
    }
}
4

2 に答える 2

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 ) ;
}
于 2013-11-01T12:46:37.430 に答える
1

Image オブジェクトがある場合は、サイズ変更について心配する必要はありません。

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

newWidth/newHeight パラメーターを変更したい場合もありますが、それ以外は問題ありません。ここでさらに多くのことを読むことができます。

お役に立てれば!

于 2013-11-01T12:47:04.120 に答える