1

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);
}
pred
  • 185
  • 1
  • 14

1 Answers1

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I got a new problem with your solution, I want a special grid, for example : line 1 : Zone1 Zone2 Zone3 Zone4 Zone4 Zone5 Zone6 Zone6 Zone6 Is it possible to have a gridLayout of this type ? – pred Nov 02 '14 at 14:06
  • I'd use a `Gridlayout(3, 3)`. You may want to post a new question; include a [complete example](http://stackoverflow.com/help/mcve) that illustrates any problem you encounter. – trashgod Nov 02 '14 at 14:09
  • Ok I'll try with a 3x3 thank you very much for your help – pred Nov 02 '14 at 14:18