Package GUI

Source Code of GUI.ConteneurNeurone

package GUI;

import java.awt.Color;
import java.awt.Cursor;
//import java.awt.Font;
import java.awt.Graphics;
//import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import nets.Neurone;

import fonctions.*;


public class ConteneurNeurone extends JPanel implements MouseListener {
 

  private static final long serialVersionUID = 1L;
  private GestionReseau gr;
  private JLabel legende;
  public JButton loupe;
  public boolean etat_loupe;
 
  public ConteneurNeurone(GestionReseau greseau){
      this.gr = greseau;
      this.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 0, Color.BLACK));
     
      legende = new JLabel("<html><strong><u>L�gende :</u></strong><br /><ul>" +
        "<li color=\"black\">0.123 <span color=\"black\">Poids</span></li>" +
        "<li color=\"red\">0.123 <span color=\"black\">Erreur</span></li>" +
        "<li color=\"green\">0.123 <span color=\"black\">Correction</span></li>" +
        "<li color=\"#ED7F10\"> <span color=\"black\">Symbole � apprendre</span></li>" +
        "</ul></html>");
      this.add(legende);
      loupe = null;
  }

  public void set() {
    if(loupe != null)
      this.remove(loupe);

    loupe = new JButton();
    loupe.setCursor(new Cursor(Cursor.HAND_CURSOR));
    loupe.setToolTipText("Frizer la visualisation d'un neurone");
    loupe.addMouseListener(this);
    this.add(loupe);
   
      etat_loupe = true;
      invLoupe();
  }
 
  public void invLoupe() {
    if(etat_loupe) {
      ImageIcon i = new ImageIcon("src/images/loupemieux.jpg");   
        Image img = i.getImage().getScaledInstance((int)(getWidth()*0.2), (int) (5 + getHeight()*0.2) , Image.SCALE_AREA_AVERAGING);
        loupe.setIcon(new ImageIcon(img));
        etat_loupe = false;
        gr.setVisu(null);
        paintComponent(this.getGraphics());

    } else {
      ImageIcon i = new ImageIcon("src/images/loupestop.jpg");   
        Image img = i.getImage().getScaledInstance((int)(getWidth()*0.2), (int) (5 + getHeight()*0.2) , Image.SCALE_AREA_AVERAGING);
        loupe.setIcon(new ImageIcon(img));
        etat_loupe = true;
        gr.setVisu(gr.getCourant());
        paintComponent(this.getGraphics());

    }
  }
 
    public void paintComponent(Graphics g) {
      super.paintComponent(g);     

      Neurone neu;
      if(gr.getVisu() == null)
        neu = gr.getCourant();
      else
        neu = gr.getVisu();
     
      g.setColor(Color.WHITE);
      g.fillRect(0, 0, getWidth(), getHeight());
     
      if(neu != null) {
        int size = neu.getDendrites().size();
        int diametre_cercle = (int) (this.getHeight()/(1.1*size+0.1));
        int espace = (int) (0.1*diametre_cercle);
       
        int border = 5;
        int fWidth = this.getWidth();
        int fHeight = this.getHeight() - border;
       
 
        for(int i=0; i<size; i++) {
          int x = (int) (fWidth*0.1);
          int y = border + espace + i*(espace+diametre_cercle);
         
          if(gr.getPartie() == 0)
            g.setColor(new Color(Couleur.degradeB((neu.getDendrites().get(i).getYi()+1)/2)));
          else if(gr.getPartie() == 1)
            g.setColor(new Color(Couleur.degradeR((neu.getDendrites().get(i).getYi()+1)/2)));
          else
            g.setColor(new Color(Couleur.degradeV((neu.getDendrites().get(i).getYi()+1)/2)));
 
          g.fillOval(x, y, diametre_cercle, diametre_cercle);
            g.setColor(Color.BLACK);
            g.drawOval(x, y, diametre_cercle, diametre_cercle);
           
            g.setColor(Color.WHITE);
            g.fillRect(x+diametre_cercle+15, y+diametre_cercle/2-7, 43, 15);
           
            g.setColor(Color.BLACK);
            g.drawRect(x+diametre_cercle+15, y+diametre_cercle/2-7, 43, 15);
            g.drawLine(x+diametre_cercle, y+diametre_cercle/2, x+diametre_cercle+15, y+diametre_cercle/2);
           
           
            if(gr.getPartie() < 2)
              g.drawString((Math.round(neu.getDendrites().get(i).getPoids()*1000)/1000. + ""), x+diametre_cercle+20, y+diametre_cercle/2+6);
            else
              g.drawString((Math.round(neu.getDendrites().get(i).getAncienPoids()*1000)/1000. + ""), x+diametre_cercle+20, y+diametre_cercle/2+6);

            if(gr.getPartie() > 0) {
              g.setColor(Color.RED);
              g.drawString((Math.round(neu.getDendrites().get(i).getPoids()*neu.getErreur()*1000)/1000. + ""), x-50, y+diametre_cercle/2+5);
             
              if(gr.getPartie() == 2) {
                g.setColor(Color.BLACK);
                g.drawLine(x+diametre_cercle, y+diametre_cercle/2, x+diametre_cercle+15, y+diametre_cercle/2);
                g.drawLine(x+diametre_cercle+15+43, y+diametre_cercle/2-7, x+diametre_cercle+15, y+diametre_cercle/2-7+15);

                g.setColor(Color.GREEN);
                g.drawString((Math.round(neu.getDendrites().get(i).getPoids()*1000)/1000. + ""), x+diametre_cercle+20, y+diametre_cercle/2+20);
              }
              g.setColor(Color.BLACK);

            }

            g.drawLine(x+diametre_cercle+58, y+diametre_cercle/2, (int) (fWidth/2.5), border + fHeight/2);
 
        }
       
        /*g.setColor(Color.RED);
        g.fillOval((int) (this.getWidth()/2.5), this.getHeight()/2-50, 100, 100);
        g.setColor(Color.BLACK);
        g.drawOval((int) (this.getWidth()/2.5), this.getHeight()/2-50, 100, 100);
          */
        try {
          Image img;
            if(gr.getPartie() == 1)
              img = ImageIO.read(new File("src/images/retour.png"));
            else
              img = ImageIO.read(new File("src/images/sigma.png"));
           
            img = img.getScaledInstance((int)(border + fHeight*0.25), (int) (border + fHeight*0.25) , Image.SCALE_AREA_AVERAGING);
        g.drawImage(img, (int) (fWidth/2.5), (int) (border+fHeight/2-(border + fHeight*0.25)/2), this);
 
      } catch (IOException e) {
        e.printStackTrace();
      }
    
       
        g.drawLine((int) ((int) (fWidth/2.5)+(border + fHeight*0.25)), border+fHeight/2, (int) (fWidth/1.5), border + fHeight/2);
        int centre = (int) (((int) (fWidth/2.5)+(border + fHeight*0.25) + (int) (fWidth/1.5))/2);
       
        g.drawLine(centre, border + fHeight/2, centre, border+fHeight/2+20);
        g.drawRect(centre-25, border + fHeight/2+25 , 50, 20);
        //g.setFont(new Font(g.getFont().getName()),g.getFont().getStyle(),16);
        g.drawString((Math.round((neu.getVal())*1000)/1000. + ""),centre-17, border + fHeight/2+40);
       
          if(gr.getPartie() == 1) {
            g.setColor(Color.RED);
          g.drawString((Math.round(neu.getErreur()*1000)/1000. + ""),centre-17, border + fHeight/2+40+20);
            g.setColor(Color.BLACK);
          }
       
       
          try {
            String fonction;
            if(neu.getFun() instanceof Identite)
              fonction = "identite.png";
            else if(neu.getFun() instanceof Sigmoide)
              fonction = "sigmoide.png";
            else
              fonction = "heaviside.png";
           
           
        Image img = ImageIO.read(new File("src/images/"+ fonction));
            img = img.getScaledInstance((int)(border + fHeight*0.25), (int) (border + fHeight*0.25) , Image.SCALE_AREA_AVERAGING);
        g.drawImage(img, (int) (fWidth/1.5), (int) (border + fHeight/2-(border + fHeight*0.25)/2), this);
        g.drawRect((int) (fWidth/1.5), (int) (border + fHeight/2-(border + fHeight*0.25)/2), (int) (border + fHeight*0.25), (int)(border + fHeight*0.25));
 
      } catch (IOException e) {
        e.printStackTrace();
      }
     
 
        g.drawLine((int) ((int) (fWidth/1.5)+(border + fHeight*0.25)), border + fHeight/2, (int) (fWidth/1.05), border + fHeight/2);
        centre = (int) (((int) (fWidth/1.5)+(border + fHeight*0.25) + (int) (fWidth/1.05))/2);
 
        g.drawLine(centre, border + fHeight/2, centre, border + fHeight/2+20);
        g.drawRect(centre-25, border + fHeight/2+25 , 50, 20);
        g.drawString((Math.round(neu.getAxone()*1000)/1000. + ""),centre-17, border + fHeight/2+40);
       
      loupe.setLocation(fWidth-(int)(fWidth*0.2)-30, 10);
 
      legende.setLocation(fWidth-200, border + fHeight-125);
      }
    }

  @Override
  public void mouseClicked(MouseEvent arg0) {
    if(arg0.getSource() == loupe) {
      this.invLoupe();
    }
   
  }

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

}
TOP

Related Classes of GUI.ConteneurNeurone

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.