public class PanelModel { private List points; //Constructeurs et accesseurs public void addPoint(PointColore point){ points.add(points); } } public class Grille extends JPanel { private PanelModel model = new PanelModel(); public void paintComponents(Graphics g){ for(PointColore p : model.getPoints()){ g.setColor(p.getColor()); g.fillRect(p.getX(), p.getY(), largeur, hauteur); } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionimport java.awt.Color; import java.awt.Graphics; import java.util.List; import java.awt.*; import javax.swing.JPanel; import java.util.ArrayList; public class Panel extends JPanel { static int xp; static int yp; static Color chan =Color.black; /**La liste des coordonnées des carrés à dessiner*/ private List points; /**La liste des couleurs associées aux points*/ private List<Color> couleurs; /** * Constructeur */ public Panel(){ super(); //Initialisation des listes points = new ArrayList (); couleurs = new ArrayList <Color>(); } /** * Méthode qui permet d'ajouter un nouveau point * appelée dans le mouselistener */ public int addPoint(int x, int y, Color couleur){ xp =x; yp=y; chan=couleur; points.add(new Point(x, y)); couleurs.add(couleur); int i; i=1; return i; } /** * Méthode qui décrit la façon de dessiner les carres */ public void paintComponent(Graphics g){ //On dessine le fond normalement super.paintComponent(g); //grille g.setColor(Color.blue);/// couleur grille int uniteX = getWidth()/TWA.xx; int uniteY = getHeight()/TWA.yy; for(int i=0; i<TWA.xx; i++){ g.drawLine(uniteX*i, 0, uniteX*i, getHeight()); g.drawLine(0, uniteY*i, getWidth(), uniteY*i); } //Taille d'un carre (xx*yy) int tailleCarrex = getWidth()/TWA.xx; int tailleCarrey = getHeight()/TWA.yy; for(int i=0; i<points.size(); i++){ //Indique qu'on dessine le carre d'une certaine couleur g.setColor(Color.white);// couleur par default g.setColor(couleurs.get(i)); //dessin du carré g.fillRect( (int)points.get(i).getX()-((int)points.get(i).getX()%tailleCarrex),//x (int)points.get(i).getY()-((int)points.get(i).getY()%tailleCarrey),//y tailleCarrex,//largeur tailleCarrey//hauteur ); int dimx =TWA.xx-(int)points.get(i).getX()/tailleCarrex; int dimy =(int)points.get(i).getY()/tailleCarrey; if (TWA.couleur==Color.orange){ TWA.msf[dimx+1][dimy+1]=1; TWA.msc[dimx-1][dimy+1]=0; TWA.mme[dimx-1][dimy+1]=0; TWA.mdi[dimx-1][dimy+1]=0; } else if (TWA.couleur==Color.RED){ TWA.msc[dimx-1][dimy+1]=1; TWA.msf[dimx-1][dimy+1]=0; TWA.mme[dimx-1][dimy+1]=0; TWA.mdi[dimx-1][dimy+1]=0; //flchhhhhhhhhhhh g.setColor(Color.black);// couleur flèche g.drawLine((int)points.get(i).getX()-((int)points.get(i).getX()%tailleCarrex)+10/2, (int)points.get(i).getY()-((int)points.get(i).getY()%tailleCarrey),(int)points.get(i).getX()-((int)points.get(i).getX()%tailleCarrex)+10/2,(int)points.get(i).getY()-((int)points.get(i).getY()%tailleCarrey)+10); g.drawLine((int)points.get(i).getX()-((int)points.get(i).getX()%tailleCarrex)+10/2,(int)points.get(i).getY()-((int)points.get(i).getY()%tailleCarrey),(int)points.get(i).getX()-((int)points.get(i).getX()%tailleCarrex),(int)points.get(i).getY()-((int)points.get(i).getY()%tailleCarrey)+10/2); g.drawLine((int)points.get(i).getX()-((int)points.get(i).getX()%tailleCarrex)+10/2,(int)points.get(i).getY()-((int)points.get(i).getY()%tailleCarrey), (int)points.get(i).getX()-((int)points.get(i).getX()%tailleCarrex)+10,(int)points.get(i).getY()-((int)points.get(i).getY()%tailleCarrey)+10/2); } else if (TWA.couleur==Color.green){ TWA.mme[dimx-1][dimy+1]=1; TWA.msc[dimx-1][dimy+1]=0; TWA.msf[dimx-1][dimy+1]=0; TWA.mdi[dimx-1][dimy+1]=0; } else if (TWA.couleur==Color.white){ TWA.mdi[dimx-1][dimy+1]=1; TWA.msc[dimx-1][dimy+1]=0; TWA.mme[dimx-1][dimy+1]=0; TWA.msf[dimx-1][dimy+1]=0; } int mx =(int)points.get(i).getX()/tailleCarrex; int my =TWA.yy-(int)points.get(i).getY()/tailleCarrey-1; String n= Integer.toString(mx); Panel1.xeg.setText(n); String n1= Integer.toString(my); Panel1.yeg.setText(n1); int h = (int)points.get(i).getX()-tailleCarrex/2; int h1 = (int)points.get(i).getY()-tailleCarrex/2; System.out.println(h+"_"+h1); System.out.println("["+TWA.mme[dimx-1][dimy+1]+"]"); System.out.println("["+TWA.msf[dimx-1][dimy+1]+"]"); System.out.println("["+TWA.mdi[dimx-1][dimy+1]+"]"); System.out.println("["+TWA.msc[dimx-1][dimy+1]+"]"); } } }