如何使用mouseListener在Slick2d中添加新图像

时间:2015-10-15 16:12:47

标签: java image slick2d

我想制作代码,这样当我点击容器中的某个地方时,我会创建一个新的图像,其坐标为mouseX和Y点击的位置。

我尝试这样做,但我不确定如何绘制新图像,如果旧图像的坐标与新图像的坐标不同,我不确定我是否&#39 ;甚至可以立即创建不同的图像,或者如果它只是相同的图像我会改变每次点击时的坐标。

我正在考虑创建一个图像数组列表,然后每当我点击某个地方时,它会向列表中添加一个新图像,然后渲染就会一直渲染整个列表,但是我不确定如何告诉渲染每个图像被点击的位置(坐标)。

在这里,我到目前为止,如果有人能帮助我,我真的很感激。如果您需要我澄清,请告诉我们。)

    package example;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class SimpleSlickGame extends BasicGame
{

    public String mouseCoords;
    public String testClick ="Nothing clicked";
    public int mouseX;
    public int mouseY;

    public SimpleSlickGame(String gamename)
    {
        super(gamename);
    }

    @Override
    public void init(GameContainer gc) throws SlickException {
        mouseCoords = "";
    }

    @Override
    public void update(GameContainer gc, int i) throws SlickException {
        int mouseX = Mouse.getX();
        int mouseY = Mouse.getY();

        mouseCoords = "Mouse X: "+ mouseX + " Mouse Y: "+ mouseY;               

    }

    @Override
    public void render(GameContainer gc, Graphics g) throws SlickException
    {
        g.drawString(mouseCoords, 250, 200);
        g.drawRect(100, 100, 100, 100);
        g.drawString(testClick, 200, 400);

        g.drawImage(new Image("images/house.png"), mouseX, mouseY);

    }

    public void mousePressed(int button, int x, int y){
        mouseX = x;
        mouseY = y;

        if (button == 0){
            if((x> 100 && x<200) && (y > 100 && y < 200)){
                testClick = "inside box";
            }
            else{
                testClick = "outside box";

            }
        }
    }


    public static void main(String[] args)
    {
        try
        {
            AppGameContainer appgc;
            appgc = new AppGameContainer(new SimpleSlickGame("Simple Slick Game"));
            appgc.setDisplayMode(600, 600, false);
            appgc.start();
        }
        catch (SlickException ex)
        {
            Logger.getLogger(SimpleSlickGame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

好的,所以我做了一些阅读文档和谷歌搜索并找到了解决方案:P,虽然我不确定它是否是最佳的做法(真的很喜欢一些批评!!)

这就是我的所作所为:

主要班级:

package example;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class SimpleSlickGame extends BasicGame
{

    public String mouseCoords;
    public String testClick ="Nothing clicked";
    public int mouseX;
    public int mouseY;
    private House[] house = new House[10];
    public List<House> houses = new ArrayList<House>();
    public House houseTest, houseTest2, houseTest3;

    public SimpleSlickGame(String gamename)
    {
        super(gamename);
    }

    @Override
    public void init(GameContainer gc) throws SlickException {
        mouseCoords = "";

    }

    @Override
    public void update(GameContainer gc, int i) throws SlickException {
        int mouseX = Mouse.getX();
        int mouseY = Mouse.getY();

        mouseCoords = "Mouse X: "+ mouseX + " Mouse Y: "+ mouseY;               

    }

    @Override
    public void render(GameContainer gc, Graphics g) throws SlickException
    {
        g.drawString(mouseCoords, 250, 200);
        g.drawRect(100, 100, 100, 100);
        g.drawString(testClick, 200, 400);


        for(int i = 0; i<houses.size();i++){
            if(houses.get(i) != null){
            houses.get(i).render(gc, g);
            }
        }


    }

    public void mousePressed(int button, int x, int y){
        mouseX = x;
        mouseY = y;

        if (button == 0){
            if((x> 100 && x<200) && (y > 100 && y < 200)){
                testClick = "inside box";
                houses.add(new House(mouseX,mouseY));

            }
            else{
                testClick = "outside box";

            }
        }
    }


    public static void main(String[] args)
    {
        try
        {
            AppGameContainer appgc;
            appgc = new AppGameContainer(new SimpleSlickGame("Simple Slick Game"));
            appgc.setDisplayMode(600, 600, false);
            appgc.start();
        }
        catch (SlickException ex)
        {
            Logger.getLogger(SimpleSlickGame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

众议院课程:

package example;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class House {

    int x;
    int y;
    int width = 20;
    int height = 20;


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

    public void render(GameContainer gc, Graphics g) throws SlickException
    {
        g.setColor(Color.white);
        g.drawRect(x, y, width, height);
    }
}