Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    724

    repaint() su applet che passa da secondo a primo piano

    ho realizzato un applet che disegna delle figure e l'ho inserito in una pagina html e fuziona correttamente.
    Quando vado a ridurre la dimensione della pagina o altre finestre vengono aperte sopra l'applet i disegni sparisco... Come posso rimediare a questo??? Devo mettere un repaint() da qualche parte, ma dove??

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: repaint() su applet che passa da secondo a primo piano

    Originariamente inviato da perzem
    ho realizzato un applet che disegna delle figure e l'ho inserito in una pagina html e fuziona correttamente.
    Quando vado a ridurre la dimensione della pagina o altre finestre vengono aperte sopra l'applet i disegni sparisco... Come posso rimediare a questo??? Devo mettere un repaint() da qualche parte, ma dove??
    Bisognerebbe vedere dove/come/quando fai i disegni.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    724
    mmmm... allora...

    ho una classe applet che estende runnable, poi ho sovrascritto il metodo paintComponent e i metodi pause start del thread in base alle mie esigenze. ogni volta che metto in pausa il thread per un tempo determinato richiamo la repaint...

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da perzem
    ho una classe applet che estende runnable
    che implementa Runnable. E che cosa faresti nel run()??

    Originariamente inviato da perzem
    poi ho sovrascritto il metodo paintComponent
    paintComponent di quale componente?? Perché non c'è un paintComponent() in Applet/JApplet.

    Originariamente inviato da perzem
    i metodi pause start del thread in base alle mie esigenze. ogni volta che metto in pausa il thread per un tempo determinato richiamo la repaint...
    Qui non è chiaro come "metti in pausa il thread" e cosa fai.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    724
    ti ho allegato le due classi che utilizzo allegerite


    codice:
    class SortAlgorithm {
        /**
         * oggetto sort.
         */
        private SortItem parent;
    
        /**
         * Se vero stop ord.
         */
        protected boolean stopRequested = false;
    
        /**
         * Set padre.
         */
        public void setParent(SortItem p) {
    	parent = p;
        }
    
        /**
         * Pause .
         */
        protected void pause() throws Exception {
    	if (stopRequested) {
    	    throw new Exception("Sort Algorithm");
    	}
    	parent.pause(parent.h1, parent.h2,parent.temp);
        }
    
        /**
         * Pause .
         */
        protected void pause(int H1) throws Exception {
    	if (stopRequested) {
    	    throw new Exception("Sort Algorithm");
    	}
    	parent.pause(H1, parent.h2,parent.temp);
        }
    
        /**
         * Pause e segna  oggetto1 & 2.
         */
        protected void pause(int H1, int H2,int T) throws Exception {
    	if (stopRequested) {
    	    throw new Exception("Sort Algorithm");
    	}
    	parent.pause(H1, H2,T);
        }
    
        /**
         * Stop ord.
         */
        public void stop() {
    	stopRequested = true;
        }
    
        /**
         * inizializza
         */
        public void init() {
    	stopRequested = false;
        }
    
        /**
         * metodo chiamato per ord array
         * 
         */
        void sort(int a[]) throws Exception {
        }
    }



    codice:
    public class SortItem
        extends Applet
        implements Runnable, MouseListener {
    
        /**
         * thread che ordina).
         */
        private Thread kicker;
    
        /**
         *  array che deve be ordinato
         */
        int []arr= new int[13];
        
        
        int mid=0;
        int temp=0;
        int app=0;
    
        /**
         * Limite sup.
         */
        int h1 = -1;
    
        /**
         * Limite inf
         */
        int h2 = -1;
    
        /**
         * nome algo.
         */
        String algName;  
    
        /**
         * algo di ord.
         */
        SortAlgorithm algorithm;
    
        Dimension initialSize = null;  
    
    	private JButton jBStart = null;
    	
    
    	private Image img = null;  
    	
    	private JPanel jPCarte = null;
    
    	private JRadioButton jRBB = null;
    
    	private JRadioButton jRBQ = null;
    
    	private JRadioButton jRBH = null;
    
    	private JRadioButton jRBI = null;
    
    	private JLabel jLabel = null;
    	
    	ButtonGroup bc = new ButtonGroup(  );  
    
    	private JButton jBNuovoOrd = null;
    
    	 String at = "";
    
    	private JSlider jSlider = null;
    
    	private JLabel jLabel1 = null;
    
    	int sleep=0;
    	
    	int paint=0;
    	
    	
    	/**
    	 * Questo metodo genera i numeri da 1 a 13	
    	 */
    	public void GenNum (){
    		Random rgen = new Random();  
    		  
    		for (int i=0; i<arr.length; i++) {
    		    arr[i] = i+1;
    		}
    		for (int i=0; i<arr.length; i++) {
    		    int randomPosition = rgen.nextInt(arr.length);
    		    int temp = arr[i];
    		    arr[i] = arr[randomPosition];
    		    arr[randomPosition] = temp;
    		}
    		}
    	
    	
    
        /**
         * Pause 
         * @see SortAlgorithm
         */
        void pause() {
    	pause(-1, -1,0);
        }
    
        /**
         * Pause 
         * @see SortAlgorithm
         */
        void pause(int H1) {
    	pause(H1, -1,0);
        }
    
        /**
         * Pause 
         * @see SortAlgorithm
         */
        void pause(int H1, int H2,int T) {
    	h1 = H1;
    	h2 = H2;
    	temp=T;
    	if (kicker != null) {
    		//System.out.println("pause");
    		repaint();
    	}
    	//System.out.println("Sleep"+sleep);
    	try {Thread.sleep(sleep);} catch (InterruptedException e){}
        }
    
        /**
         * Initialize the applet.
         */
        public void init() {
        	this.setLayout(null);
        	this.setSize(new Dimension(1006, 501));
    	jLabel1 = new JLabel();
    	jLabel1.setBounds(new Rectangle(445, 111, 206, 24));
    	jLabel1.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
    	jLabel1.setText("Seleziona la velocità di esecuzione");
    	jLabel = new JLabel();
    	jLabel.setBounds(new Rectangle(29, 26, 234, 19));
    	jLabel.setText("Seleziona l'algoritmo");
    	
    
    	this.setName("PSfondo");
    	this.add(getJBStart(), null);
    	this.add(getJPCarte(), null);
    
    	 this.setVisible(true);
    	 this.add(getJRBB(), null);
    	 this.add(getJRBQ(), null);
    	 this.add(getJRBH(), null);
    	 this.add(getJRBI(), null);
    	 this.add(jLabel, null);
    	 this.add(getJBNuovoOrd(), null);
    	//addMouseListener(this);
    	 this.add(getJSlider(), null);
    	 this.add(jLabel1, null);
    	bc.add(jRBB);
    	bc.add(jRBH);
    	bc.add(jRBQ);
    	bc.add(jRBI);
    	jBNuovoOrd.setEnabled(false);
    	
    	this.setVisible(true);
    	 GenNum();
        }
    
        public void start() {
            h1 = h2 = -1; 
            GenNum();  
            
            //System.out.println("start"+"indice:"+h1+" "+ h2);
            repaint();
            showStatus(getParameter("alg"));
        }
    
        /**
         * Deallocate resources of applet.
         */
        public void destroy() {
            removeMouseListener(this);
        }
    
        /**
         * Paint the array of numbers as a list
         * of horizontal lines of varying lengths.
         */
        public void paintComponent(Graphics g) {
        	super.paintComponents(g);
            int pos=50;
            int a[] = arr;
          
            ... ho eliminato tutta la parte dei disegni
            
      }
         
    
        /**
         * Update without erasing the background.
         */
        public void update(Graphics g) {
        //	System.out.println("update");
        	paintComponent(g);
        }
    
        /**
         * Run the sorting algorithm. This method is
         * called by class Thread once the sorting algorithm
         * is started.
         * @see java.lang.Thread#run
         * @see SortItem#mouseUp
         */
        public void run() {
        	sleep=jSlider.getValue()*1000;
    	try {
    	    if (algorithm == null) {
    		algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
    		algorithm.setParent(this);
    	    }
    	    algorithm.init();
    	    algorithm.sort(arr);
    	} catch(Exception e) {
    	}
        }
    
        /**
         * Stop the applet. Kill any sorting algorithm that
         * is still sorting.
         */
        public synchronized void stop() {
    	if (algorithm != null){
                try {
    		algorithm.stop();
                } catch (IllegalThreadStateException e) {
                    // ignore this exception
                }
                kicker = null;
    	}
        }
    
        /**
         * For a Thread to actually do the sorting. This routine makes
         * sure we do not simultaneously start several sorts if the user
         * repeatedly clicks on the sort item.  It needs to be
         * synchronized with the stop() method because they both
         * manipulate the common kicker variable.
         */
        private synchronized void startSort() {
    	if (kicker == null || !kicker.isAlive()) {
    	    kicker = new Thread(this);
    	    kicker.start();
    	}
        }
    
    
        public void mouseClicked(MouseEvent e) {
            showStatus(getParameter("alg"));
        }

  6. #6
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    724
    proprio nessuno sa come risolvere questa cosa??
    please help....

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.