简体   繁体   English

如何将矩形更改为图像?

[英]How do I change the rectangles to an image?

I just started learning Java (OOP) and I am trying to develop a simple game (like space invaders).我刚开始学习 Java (OOP),我正在尝试开发一个简单的游戏(比如太空入侵者)。 I want to replace the guards (brown rectangles) to an image to make the game more aesthetically pleasing.我想将守卫(棕色矩形)替换为图像,以使游戏更美观。

I am not sure how to do this as the guards are dependent on a lot of things.我不确定如何做到这一点,因为守卫依赖于很多事情。 I tried using the loadImage() method and others but it did not work.我尝试使用 loadImage() 方法和其他方法,但它不起作用。

Relevant codes:相关代码:

Guard.java 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方形.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);
    }
}

Screenshot for reference: Instead of brown boxes, I want to change it to other things using images截图供参考:我想用图像将其更改为其他东西,而不是棕色框在此处输入图像描述

The following mre demonstrates using an image to represent a rectangle.下面的 mre 演示了使用图像来表示一个矩形。 Note that for easier implementation Gurd extends Componenet :请注意,为了更容易实现, 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 is not efficient because it reads and constructs image over again. Square效率高,因为它会重新读取和构建image
To make it more efficient you can introduce a static block to initialize image only once:为了提高效率,你可以引入一个 static 块来初始化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);
    }
}

    

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

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