当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Get click Target on JPanel Netbeans

问题描述:

I'm new in Java programming, and >I'm trying to make a 2D game.

I search on the website an answer to my question but I didn't find it so I hope that I'm doind the right thing.

So, I got a class named Board which extends JPanel and implements ActionListener.

In this Board I draw some "Zone" which is a class with an associate image.

My problem is, when I click on the board, I want to get the "Zone" which I clicked but I actually get the Board with the event mouseClicked.

I hope I'm understandable, here's the Board class :

public class Board extends JPanel implements ActionListener {

private List<Zone> zones = new ArrayList<Zone>();

public Board() {

addMouseListener(new TAdapter());

setFocusable(true);

setBackground(Color.BLACK);

setDoubleBuffered(true);

Dalle[] dalle1C = new Dalle[]{new Dalle()};

zones.add(new Zone(false, false, dalle1C, null, "zone1D1C.jpg", 0, 0));

zones.add(new Zone(false, false, dalle1C, null, "zone2D1C.jpg", 150, 0));

timer = new Timer(5, this);

timer.start();

}

public void paint(Graphics g) {

super.paint(g);

Graphics2D g2d = (Graphics2D) g;

for (Zone zone : zones) {

g2d.drawImage(zone.getImage(), zone.getX(), zone.getY(), this);

}

Toolkit.getDefaultToolkit().sync();

g.dispose();

}

public void actionPerformed(ActionEvent e) {

repaint();

}

private class TAdapter extends MouseAdapter {

public void mouseClicked(MouseEvent e) {

//Here, instead of using x,y positions I want to get something like "e.getClickedObject()"

Integer x = e.getX();

Integer y = e.getY();

Zone zone_selected = null;

for (Zone zone : zones) {

if (x > zone.getX() && x < zone.getX_end() && y < zone.getY_end() && y > zone.getY(){

zone_selected = zone;

}

}

zones.remove(zone_selected);

// And Here I want to use repaint method but this is not possible if you have a solution...

}

}

}

And the zone class :

public class Zone {

private String name;

private boolean piece;

private boolean egout;

private List<Dalle> dalles = new ArrayList<Dalle>();

private List<Connexion> connexions = new ArrayList<Connexion>();

private List<Personnage> personnages = new ArrayList<Personnage>();

private Image image;

private Integer x;

private Integer y;

public Integer x_end;

public Integer y_end;

public Zone(boolean piece, boolean egout, Dalle[] dalles, List<Connexion> connexions, String image_name, Integer x, Integer y) {

this.piece = piece;

this.egout = egout;

this.dalles.addAll(Arrays.asList(dalles));

for(Dalle dalle : dalles) {

dalle.addZone(this);

}

this.name = image_name;

this.connexions = connexions;

ImageIcon ii = new ImageIcon(this.getClass().getResource(image_name));

image = ii.getImage();

this.x = x;

this.y = y;

this.x_end = x + image.getWidth(null);

this.y_end = y + image.getHeight(null);

}

网友答案:

A JComponent like Board sees mouse events relative to it's top-left corner, the point (0, 0) by default. To get internal coordinates, you can

  • Use a grid of components, as shown here.

  • Interpolate points by linear scaling of the coordinates, as shown here.

分享给朋友:
您可能感兴趣的文章:
随机阅读: