微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将矩形更改为图像?

如何解决如何将矩形更改为图像?

我刚刚开始学习 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,width,height);
    }
}

供参考的截图:我想使用图像将其更改为其他内容而不是棕色框

enter image description here

解决方法

以下 mre 演示了使用图像来表示矩形。请注意,为了更容易实现 Gurd extends 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,null);
    }

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

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

    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。