简体   繁体   English

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

[英]How do I use mouseListener to add a new Image in Slick2d

I want to make the code so that when I click somewhere in the container, I create a new image with the coordinates of mouseX and Y of where I clicked. 我想编写代码,以便当我在容器中的某个位置单击时,使用单击位置的mouseX和Y坐标创建一个新图像。

I tried to do this, but I'm not sure how I will draw a new image, without the old image getting the same coordinates as the new one, and I'm not sure if I'm even creating different images right now, or if it's just the same image I'm changing the coordinates of whenever I click. 我尝试这样做,但是我不确定如何绘制新图像,而旧图像却没有与新图像相同的坐标,而且我不确定现在是否还要创建不同的图像,或者,如果只是同一张图片,我每次单击都将更改其坐标。

I was thinking about creating an array list of images, and then whenever I click somewhere, it adds a new image to the list, and then the render just keeps rendering the entire list, but here I'm not sure how to tell the render where each image was clicked (the coordinates). 我当时正在考虑创建图像的数组列表,然后每当我单击某个位置时,它都会向列表中添加新图像,然后渲染器将继续渲染整个列表,但是在这里我不确定如何分辨渲染器单击每个图像的位置(坐标)。

Here's what I have so far, I'd really appreciate if someone could help me. 这是到目前为止的内容,如果有人可以帮助我,我将非常感激。 Let me know if there's something you need me to clarify :) 让我知道您是否需要澄清:)

    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);
        }
    }
}

Ok so I did some reading of documents and googling and found a solution :P, although I'm not sure if it's the most optimal way of doing this (would really like some critique!!) 好的,所以我阅读了一些文档并进行了谷歌搜索,找到了一个解决方案:P,尽管我不确定这是否是执行此操作的最佳方法(真的很想批评!)

Here's what I did: 这是我所做的:

The main class: 主班:

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);
        }
    }
}

House class: 房屋类别:

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);
    }
}

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

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