Wednesday, 15 February 2012

java - Get click Target on JPanel Netbeans -



java - Get click Target on JPanel Netbeans -

i'm new in java programming, , >i'm trying create 2d game. search on website reply question didn't find hope i'm doind right thing. so, got class named board extends jpanel , implements actionlistener. in board draw "zone" class associate image. problem is, when click on board, want "zone" clicked board event mouseclicked. hope i'm understandable, here's 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; (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 want "e.getclickedobject()" integer x = e.getx(); integer y = e.gety(); zone zone_selected = null; (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); // , here want utilize repaint method not possible if have solution... } } }

and 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 board sees mouse events relative it's top-left corner, point (0, 0) default. internal coordinates, can

use grid of components, shown here.

interpolate points linear scaling of coordinates, shown here.

java swing netbeans jpanel

No comments:

Post a Comment