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

问题描述:

我想制作代码,以便当我点击容器中的某个位置时,我创建一个新的图像,其坐标为mouseX和Y,我点击了它们。如何使用mouseListener在Slick2d中添加一个新的图像

我试图做到这一点,但我不知道如何绘制新图像,没有旧图像获得与新图像相同的坐标,并且我不确定是否创建了不同的坐标图像,或者如果它只是相同的图像,我会改变每次点击时的坐标。

我正在考虑创建一个图像数组列表,然后每当我点击某处时,它会向列表中添加一个新图像,然后渲染只是呈现整个列表,但在这里我不知道如何告诉渲染点击每个图像的位置(坐标)。

这是我到目前为止,我真的很感激,如果有人可以帮助我。让我知道,如果有需要我澄清:)

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

好了,所以我做的文档和谷歌搜索一些阅读,并找到了解决的东西: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); 
    } 
} 
+0

您的解决方案似乎相当不错。只需要注意几件事情:1)在每个循环中创建一个“新图像”的问题,你应该在类中声明你的图像,然后在渲染中绘制它(通常:在渲染或更新中从不使用'new' )。 2)ATM只是绘制白色形状,但是当你绘制图像时也会关心图像的创建,只需声明一次,然后再使用它几次。 – RPresle