【问题标题】:How do I change the rectangles to an image?如何将矩形更改为图像?
【发布时间】:2021-02-03 17:15:59
【问题描述】:

我刚开始学习 Java (OOP),我正在尝试开发一个简单的游戏(如太空入侵者)。我想将守卫(棕色矩形)替换为图像,以使游戏更美观。

我不知道该怎么做,因为守卫依赖很多东西。我尝试使用 loadImage() 方法和其他方法,但没有成功。

相关代码:

Guard.java

public class Guard {

    private List<Square> squares;

    public Guard(int x, int y) {
        squares=new ArrayList<>();
        for(int i=0; i<3; i++) {
            for (int j = 0; j < 6; j++) {
                squares.add(new Square(x + SQUARE_SIZE * j, y + SQUARE_SIZE * i));
            }
        }
    }

    public void collisionWith(MovingObject obj) {
        for(Square square : squares) {
            if(square.visible && square.intersects(obj.getBoundary())) {
                square.setVisible(false);
                obj.die();
            }
        }
    }

    public void draw(Graphics g) {
        for(Square square : squares) 
        {
            if(square.visible) square.draw(g);
        }
    }

}

Square.java

    class Square extends Rectangle  {

    boolean visible;

    Square(int x, int y) 
    {
        super(x, y, SQUARE_SIZE, SQUARE_SIZE);
        setVisible(true);
    }

    void setVisible(boolean visible) {
        this.visible = visible;
    }

    void draw(Graphics g) {
        g.setColor(new Color(228, 155, 30));
        g.fillRect(x, y, width, height);
    }
}

截图供参考:我想用图像将其更改为其他东西,而不是棕色框

【问题讨论】:

  • 为了在具有 Graphics 类的组件中绘制图像,您应该使用 drawImage() 方法。也许这个问题对你有帮助:stackoverflow.com/questions/9083096/…
  • 1) “我尝试使用 loadImage() 方法和其他方法,但没有成功。” 它是如何失败的?始终复制/粘贴错误和异常输出! 2) “相关代码:” 在你眼中什么是相关的,什么是与获得帮助相关的,是两个不同的东西。 a) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。 b) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。例如。 this answer中的代码..
  • .. 指向嵌入在this question 中的图像的热链接。

标签: java swing oop jframe game-development


【解决方案1】:

下面的 mre 演示了使用图像来表示矩形。请注意,为了更容易实现Gurd 扩展Componenet

import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class SwingMain {

    SwingMain() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(new Guard(0,0));
        frame.pack();
        frame.setVisible(true);
    }

    class Guard extends Component{

        private static final int GAP = 3,  W = 6, H = 3;
        private int squareSize, totalX, totalY;
        private final List<Square> squares;

        public Guard(int x, int y) {
            squares=new ArrayList<>();
            totalY = 0;
            Square square = null;
            for(int i=0; i< H; i++) {
                totalX = 0;
                for (int j = 0; j < W; j++) {
                    square = new Square(x + totalX , y + totalY );
                    squares.add(square);

                    totalX += square.getWidth() +GAP;
                }
                totalY += square.getHeight() +GAP;
            }
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            for(Square square : squares) {
                square.draw(g);
            }
        }

        @Override
        public Dimension getPreferredSize(){
            return new Dimension(totalX, totalY);
        }
    }

    class Square{

        private static final String BOX =
                "https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Box_Orange.png";
        private  Image image = null;

        private final int x,y;

        Square(int x, int y)   {
            this.x=x; this.y = y;
            try {
                image = new ImageIcon(new URL(BOX)).getImage();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        void draw(Graphics g) {
            g.drawImage(image, x,y, null);
        }

        int getWidth(){
            return image.getWidth(null);
        }

        int getHeight(){
            return  image.getHeight(null);
        }
    }

    public static void main(String[] args) {
        new SwingMain();
    }
}

Square 效率不是,因为它会重复读取和构造image
为了提高效率,你可以引入一个静态块来初始化 image 一次:

static class Square{

    private static final String BOX =
            "https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Box_Orange.png";
    private static Image image = null;
    static{

        try {
            image = new ImageIcon(new URL(BOX)).getImage();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private final int x,y;

    Square(int x, int y)   {
        this.x=x; this.y = y;
    }

    void draw(Graphics g) {
        g.drawImage(image, x,y, null);
    }

    int getWidth(){
        return image.getWidth(null);
    }

    int getHeight(){
        return  image.getHeight(null);
    }
}

    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2018-08-14
    • 2017-10-08
    • 1970-01-01
    • 2014-04-09
    • 2014-06-14
    相关资源
    最近更新 更多