J'ai fait un programme qui doit pouvoir créer, autant qu'on le veut, des JPanel nommer Entity en appuyant sur un bouton(Generate Map et Generate Input), les stocker dans un tableau, et les mettre dans un autre autre JPanel nommer ConstructionPanel.
a chaque création du nouvelle Entity je lui rajoute un MouseListener implementer par le JPanel principal(ConstructionPanel).
Mon probleme est que Les Entity ne sont pas sensible aux evenement de souris.
J'ai essayer de rajouter le MouseListener de differente facon aux Entity (dans leur propre constructeur...) mais rien n'y fait.
Merci
import javax.swing.*;
import java.awt.*;
import java.awt.even
public class ConstructionPanel extends JPanel implements MouseListener {
int counter;
int width, height;
Entity[] entity_list;
public ConstructionPanel(){
Toolkit tk=Toolkit.getDefaultToolkit();
this.setPreferredSize(new Dimension(tk.getScreenSize().width/2,3*tk.getScreenSize().width/4));
width= tk.getScreenSize().width;
height=tk.getScreenSize().height;
this.setBackground(Color.white);
this.setLayout(null);
counter=0;
entity_list=new Entity[10];
add_arrow=false;
connect=false;
first=false;
second=true;
ind_first=-1;
ind_sec=-1;
connexions= new int[10][10];
}
public void add(Map map){
int[] t= {20 ,20};
entity_list[counter]=new Entity(this,map,map.getPosition(),counter,map.getType(), t,width/10,height/5);
map.setPanel_identifier(counter);
entity_list[counter].addMouseListener(this);
this.add(entity_list[counter]);
entity_list[counter].setLocation(20,20);
this.validate();
this.repaint();
counter+=counter;
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("test");
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
class Entity extends JPanel{
ConstructionPanel constr;
Map map;
String title;
int position_as_entity,position_as_map,width,height,wd,type,oldX,oldY ;
int[] coordinates;
Graphics g;
//EntityListener listen;
public Entity(ConstructionPanel constructionpanel,Map map,int position_as_map,int position_as_entity,int type,int[] coordinates,int width, int height){
this.constr=constructionpanel;
this.map=map;
this.position_as_map=position_as_map;
this.position_as_entity=position_as_entity;
this.type=type;
this.coordinates=coordinates;
this.width=width;
this.height=height;
title=map.getTitle();
//this.setPreferredSize(new Dimension(width,height));
//this.addMouseListener(listen = new EntityListener());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(title, 20, 20);
g.drawLine(1, 1,width-2 ,1);
g.drawLine(width-2, 0,width-2 ,height-2);
g.drawLine(width-2, height-2,1 , height-2);
g.drawLine(1, height-2,1 ,1);
}
}