在JPanel Netbeans上单击Target

时间:2014-11-02 12:29:32

标签: java swing netbeans jpanel

我是Java编程的新手,并且>我试图制作2D游戏。 我在网站上搜索了我的问题的答案,但我没有找到它,所以我希望我能做正确的事情。 所以,我得到了一个名为Board的类,它扩展了JPanel并实现了ActionListener。 在这个董事会中,我画了一些" Zone"这是一个具有关联图像的类。 我的问题是,当我点击电路板时,我想得到" Zone"我点击了但实际上我得到了事件mouseClicked的董事会。 我希望我能理解,这是董事会成员:

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...
        }
    }

}

区域类:

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

1 个答案:

答案 0 :(得分:1)

JComponent Board看到相对于它左上角的鼠标事件,默认点为0, 0)。要获取内部坐标,您可以

  • 使用组件网格,如图here所示。

  • 通过坐标线性缩放来插值点,如here所示。