如何重置此缓冲图像的坐标或将该缓冲图像放置在矩形中?

时间:2019-01-20 20:03:17

标签: java

我将此“巨魔面孔”作为我的敌人导入了我的代码,从屏幕顶部到底部下雨。但是在这样做之前,我创建了粉红色的矩形,以便可以将巨魔面部图像覆盖在矩形的顶部。但是,当我将图像的坐标设置为等于矩形时,可以看到它没有居中。然后,我将一个巨魔脸部图像的坐标设置为等于(0,0),您可以看到它显然远离原点。

我尝试导入其他PNG图像,但没有成功,并且无效。

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
public class magazinerain extends GameDriverV4{
    LinkedList<rainingenemies> r = new LinkedList<rainingenemies>();
    rainingenemies variable;
    BufferedImage trollface;

    public magazinerain() {
        trollface = this.addImage("minion.png");
    }
    public void faller(Graphics2D win) {
        for (int i = 0; i<r.size(); i++) {
            variable=r.get(i);
            variable.fall(win);
            if (variable.getY()>=800) {
                delete(variable);
            }

        }
    }

    public void draw(Graphics2D win) {
        int count = 0;
        win.drawImage(trollface, 0, 0,null);
        for (int i = 0; i < r.size(); i++) {
            variable = r.get(i);
            variable.fall(win);
            //win.drawImage(trollface, (int) variable.getX(), (int) variable.getY(),null);
            //the images are not correctly centered on the screen
            //win.drawImage(trollface, 400, 400,null);
        }
    }
    public void memorymanagement() {
        for (int i = 0; i < r.size(); i++) {
            variable = r.get(i);
            //System.out.println(variable.getY());
            if (variable.getY()>=800) {
                r.remove(i);
                //System.out.println("Removed");
            }

        }
    }

    public void checkintersects(int a, int b) {
        int buffer = 10;
        int buffery = 15;
        for (int i = 0; i< r.size(); i++) {
            variable  = r.get(i);
            //if (a>(variable.getX()-buffer) && (a<variable.getX()+buffer)) {
            if ((variable.getX()>a-buffer-10)&& (variable.getX()<a+buffer+30)) {
                //if ((b>variable.getY()-buffery) && (b<variable.getX()+buffery)) {
                if ((variable.getY()>b-buffery) && (variable.getY()<b+buffery)){
                    r.remove(i);
                    System.out.println("spacex="+a+"getx="+variable.getX());
                    System.out.println("spacey="+b+"gety="+variable.getY());

                }
            }

        }
    }

    public void delete(rainingenemies a) {
        r.remove(a);
    }
    public void insert(rainingenemies a) {
        r.add(a);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

特别是在这段代码中,我期望巨魔的脸位于(0,0),但它不存在,如屏幕截图所示

enter image description here

0 个答案:

没有答案