Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    mouseMovent() non viene visto

    Ragazzi ho un problema, ho un JPanel chiamato MiddlePanel che implementa il MouseListener con dentro un'altro JPanel chiamato JGatePanel, il problema è questo nel momento in cui passo il mouse sopra il JGatePanel nel MiddlePanel non lo vede più il mouse, l'ho verificato facendogli stampare le coordinate del mouse...

  2. #2
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    755
    Il MouseListener chi ce l'ha?Il focus chi ce l'ha?
    La risposta al tuo problema sta in queste due domande..


  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123
    Posta il codice e spiegati meglio...

    Comunque mouseMovent() non esiste. lol

  4. #4
    Il middlePanel....

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    755
    Prova a postare il codice , detta così non mi è facile aiutarti

  6. #6
    Questo è il midllePanel

    codice:
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.*;
    import java.util.ArrayList;
    
    public class MiddlePanel extends JPanel implements MouseListener,MouseMotionListener{
        
        int x0,y0,startx0,starty0,x,y,
                preferedWidth = getWidth(),
                preferedHeight = getHeight();
        boolean out,     //Indica se il cursore è fuori dalla finestra
                drag,    //Indica se il cursore è trascinato
                butgateact, //Indica se è stato attivato un pulsante porta della toolbar 
                butlineact, //Indica se è stato attivato il pulsante linea nella toolbar
                ok = false; //booleano ausiliare per verificare se sono stati fatti due click per diseganre una LineShape 
        
        JGatePanel ctmp;
        Image imggatetmp;
        MyShape shapetmp;
        InputShape inputtmp;
        private Point corner1 = new Point(0,0);
        private Point corner2 = new Point(0,0);
        
        ArrayList<JGatePanel> gate = new ArrayList(); 
        ArrayList<MyShape> shapes = new ArrayList();
        ArrayList<InputShape> inputshape = new ArrayList();
        ArrayList<OutputShape> outputshape = new ArrayList();
        
        public MiddlePanel(){
            LayoutManager overlay = new OverlayLayout(this) { 
                @Override
                public void layoutContainer(Container target) {  
                    int nChildren = target.getComponentCount();
                    
                    for (int i = 0; i < nChildren; i++){  
                        Component c = target.getComponent(i);  
                        c.setBounds(c.getLocation().x, c.getLocation().y,   
                                c.getPreferredSize().width, c.getPreferredSize().height);  
                    }  
                }
            };
            
            setMaximumSize(new Dimension(800,600));
            
            setLayout(overlay);
            addMouseListener(this);
            addMouseMotionListener(this);
            setBackground(Color.white);
    //        setAutoscrolls(true); //Studiare questo metodo per fare l'autoscroll
       } 
       
       //Un metodo che restituisce se non c'è il rischio di sovrappore i componenti figlio
       @Override
       public boolean isOptimizedDrawingEnabled() {
            return false;
       }
       
       //Metodo che viene chiamato con il repaint(); 
       @Override
       public void paintComponent(Graphics g){
           super.paintComponent(g);
           g.setColor(Color.LIGHT_GRAY);
           paintGateTmp(g);
           paintGrid(g); 
           refreshShapes((Graphics2D)g);
           paintLineTmp(g);
           //paintSelection(g);
       }
       
       //Serve per disegnare una Shape qualsiasi sul pannello
       public void paintShape(Graphics g,MyShape s){
           Graphics2D g2 = (Graphics2D)g;
           g2.setColor(Color.black);
           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
           g2.draw(s.getShape());
       }
       
       //Serve per creare un rettangolo per selezionare i componenti
       public void paintSelection(Graphics g){
           int x = Math.min(corner1.x,corner2.x);
           int y = Math.min(corner1.y,corner2.y);
           int width  = Math.abs(corner1.x - corner2.x);
           int height = Math.abs(corner1.y - corner2.y);
           
           g.setColor(Color.CYAN);
           g.drawRect(x, y, width, height);
           g.setColor(Color.BLUE);
           g.fillRect(x+1, y+1, width-1, height-1);
       }
       
       
       public void paintLineTmp(Graphics g){
           if(inputtmp!=null && butlineact){
               g.setColor(Color.black);
               g.drawLine(inputtmp.end.x,inputtmp.end.y,x0,y0);
           }
       }
       
       //Disegna la porta
       public void paintGateTmp(Graphics g){
           if(butgateact){
               g.drawImage(imggatetmp,x0-24,y0-24,this);
               g.setColor(Color.DARK_GRAY);  //Cambia il colore della griglia momentaneamente
           }
       }
       
       //Disegna la griglia
       public void paintGrid(Graphics g){
           for(int i=0; i<getWidth() || i<getHeight(); i+=20) {
              g.drawLine(i,0,i,getHeight());
              g.drawLine(0,i,getWidth(),i);
           }
       }
       
       //Crea una porta passando l'immagine temporanea utilizzata, la posizione del mouse, è una costante passata dal core (Da sistemare)
       public JGatePanel createGate(MouseEvent e){
           JGatePanel Jglbl = new JGatePanel(imggatetmp,e,0);
           refresh(Jglbl,e);
           add(Jglbl);
           return Jglbl;
       }
       
       //Refrescia quando l'oggetto è una JGateLabel (o un LineShape)
       public void refresh(JGatePanel j,MouseEvent e){
           refreshGrid(j);
           moveScroolBar(e.getPoint(),j);
           refreshShapes((Graphics2D)getGraphics());
           revalidate();
           repaint();
       }
       
       //Refreshia tutto il middlepanel chiamandosi in cascata tutti i metodi che permettono il refresh totale del panel 
       public void refresh(){
         refreshShapes((Graphics2D)getGraphics());
         revalidate();
         repaint();  
       }
       
       //Si visita tutta lista contenente tutti le Shape
       public void refreshShapes(Graphics2D g2){
           for(int i=0;i<shapes.size();i++){
                g2.setColor(Color.black);
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g2.draw(shapes.get(i).getShape());
           }
           for(int i=0;i<inputshape.size();i++){
                g2.setColor(Color.black);
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g2.fill(inputshape.get(i).getShape());
           }
       }
       
       //Muove la scroll bar solo se il cursore è fuori dal Frame
       public void moveScroolBar(Point p,JGatePanel c){
           if(!LCE01.Grafica.Split.onScreen(c) && LCE01.Grafica.Split.MP.out){
               LCE01.Grafica.Split.mySetJViewport(this,MouseInfo.getPointerInfo().getLocation(),c);
           }
       }
       
       //Aggiorna la griglia, estendola (farla ridurre se necessario da sistemare) 
       public void refreshGrid(JGatePanel j){
            if((j.getX()+j.getWidth())>preferedWidth) preferedWidth = j.getX()+j.getWidth();
            if((j.getY()+j.getHeight()>preferedHeight)) preferedHeight = j.getY()+j.getHeight();
            setPreferredSize(new Dimension(preferedWidth,preferedHeight));
       }
       
       //Si potrebbe creare una lista di coordianate x e y di ogni label in ordine decrescente così da evitare di fare una ricerca su tutti i componenti
       public Dimension searchSizeGrid(){
           int x = getWidth();
           int y = getHeight();
           for(int i=0;i<gate.size();i++){
               if(x<gate.get(i).getX())
                   x = gate.get(i).getX();
               if(y<gate.get(i).getY())
                   y = gate.get(i).getY();
           }
           
           return new Dimension(x,y);
       }
       
       //Vede se nel punto cliccato c'è un input e nel caso memorizza il reference
       public InputShape searchInput(Point p){
           for(int i=0;i<inputshape.size();i++)
               if(inputshape.get(i).getShape().contains(p)){
                   return inputshape.get(i);
               }
           return null;
       }
       
       public void colleague(Point p){
           shapes.add(new LineShape(inputtmp.end,p));
           shapetmp = shapes.get(shapes.size()-1);
           inputtmp = null;
           butlineact = false;
           ctmp = null;
       } 
       
       @Override
       public void mouseClicked(MouseEvent e) {
           
           if(butlineact)
                inputtmp = searchInput(e.getPoint());
           
           if(butgateact){
                gate.add(createGate(e));
           }
           
           drag = false;
           startx0 = 0;
           starty0 = 0;
           butgateact = false;
           imggatetmp = null;
           setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
           repaint();
       }
    
       @Override
       public void mousePressed(MouseEvent e) {
           corner1 = e.getPoint();
           corner2 = corner1;
           repaint();
       }
    
       @Override
       public void mouseReleased(MouseEvent e) {
           corner1 = e.getPoint();
           corner2 = corner1;
           repaint();
       }
       
       @Override
       public void mouseEntered(MouseEvent e) {       
           out = false;
           repaint();
       }
       
       @Override
       public void mouseExited(MouseEvent e) {
           out = true;
           repaint();
       }
    
       @Override
       public void mouseDragged(MouseEvent e) {
           corner2 = e.getPoint();
           drag = true;
           repaint();
       }
       
       @Override
       public void mouseMoved(MouseEvent e) {
           x0 = e.getX();
           y0 = e.getY();
           drag = false;
            System.out.println(hasFocus());
           repaint();
       }
       
       public void setImageGateTmp(Image i){
           setCursor(new Cursor(Cursor.MOVE_CURSOR));
           imggatetmp = i;
           butgateact = true;
       } 
       
       //Setta il numero di input, il metodo viene chiamato dal ChangeListener degli JSpinner del SouthPanel
       public void setInputShape(int n){
           if(n>=inputshape.size())
               for(int i=inputshape.size();i<n;i++)
                    inputshape.add(new InputShape(i*40+30));
           else
               for(int i=inputshape.size()-1;i>=n;i--)
                    inputshape.remove(i);       
           refresh();
       }
       
       //Sistemare
       public void setOutputShape(int n){
           
       }
    }

  7. #7
    Questo è il componente che gli inserisco che poi è un'altro JPanel

    codice:
    import java.awt.*;
    import java.awt.Component;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import javax.swing.OverlayLayout;
    
    public class JGatePanel extends JLinkablePanel {
    
        //Indica se il mouse è fuori dalla label
        private int id;
        private Image img;
        
        public JGatePanel(Image _img,MouseEvent e,int _id) {
            img = _img;
            id = _id;
            //Il valore per fare una prova
            id = 0;
            setLocation(e.getX()-24,e.getY()-24);
            setPreferredSize(new Dimension(img.getWidth(this),img.getHeight(this)));
            setOpaque(false);
            
            //Crea un layout per la sovrapposizione dei Componenti
            LayoutManager overlay = new OverlayLayout(this) {
    
                @Override
                public void layoutContainer(Container target) {
                    int nChildren = target.getComponentCount();
    
                    for (int i = 0; i < nChildren; i++) {
                        Component c = target.getComponent(i);
                        c.setBounds(c.getLocation().x, c.getLocation().y,
                                c.getPreferredSize().width, c.getPreferredSize().height);
                    }
                }
            };
            setLayout(overlay);
            
            //Creo un ascoltatore per trasportare gli elementi
            addMouseMotionListener(new MouseMotionAdapter() {
    
                //Serve per trascinare la JLabel dove vuoi
                @Override
                public void mouseDragged(MouseEvent e) {
                    Component c = e.getComponent();
                    if (!LCE01.Grafica.Split.onScreen((JGatePanel) c)) {
                        c.setLocation(c.getX() + e.getX() / 50, c.getY() + e.getY() / 50);
                    } else {
                        c.setLocation(c.getX() + e.getX(), c.getY() + e.getY());
                    }
                    LCE01.Grafica.Split.MP.refresh((JGatePanel)e.getSource(), e);
                }
            });
            
            
            addMouseListener(new MouseAdapter() {
    
                @Override
                public void mouseClicked(MouseEvent e){
                    if(LCE01.Grafica.Split.MP.inputtmp!=null){
                        inputshape.add((LineShape)LCE01.Grafica.Split.MP.shapetmp);
                        LCE01.Grafica.Split.MP.colleague(getEffetiveLink(e.getPoint()));
                    }
                    else{
                        LCE01.Grafica.Split.MP.inputtmp = null;
                    }
                }
            });
            
            refresh();
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img,0,0,null);
        }
    
        //Un metodo che restituisce se non c'è il rischio di sovrappore i componenti figlio
        @Override
        public boolean isOptimizedDrawingEnabled() {
            return false;
        }
    
        @Override
        protected void setInputOutput(){
            int input = 0;
            int output = 0;
            switch(id){
                case 0:{
                    input = 2;
                    output = 1;
                    positioninput.add(new Point(0,25));
                    positioninput.add(new Point(0,46));
                    positionoutput.add(new Point(69,35));
                }
            }
            for(int i=0;i<input;i++)
                inputshape.add(null);
            for(int i=0;i<output;i++)
                outputshape.add(null);
        }
        
        //Setta il punto dove fare il collegamento con coordiante assolute, facendo il confronto con quelle relative alla porta 
        @Override
        protected Point getEffetiveLink(Point p){
            if(p.x>=0 && p.x<36){
                if(p.y>=0 && p.y<36)
                    return new Point(positioninput.get(0).x+getX(),positioninput.get(0).y+getY());
                else
                    return new Point(positioninput.get(1).x+getX(),positioninput.get(1).y+getY());
            }        
            else
                return new Point(positionoutput.get(0).x+getX(),positionoutput.get(0).y+getY());
        }
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.