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

    Problema prestazioni thread

    Salve raga il mio giochino per la scuola consiste nel cliccare tutti i quadratini che si visualizzano sullo schermo sl che il problema è che pultroppo con i thread mi rallenta xkè dovrei creare tanti thread quanti i quadratini.. potete consigliarmi?...
    Classe che crea 1 bottone e 1 thread
    -------------------
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Quad extends JButton implements Runnable {

    private static Visiva1 vis;
    private static Delay d;
    private boolean clicked=true;

    /**
    * Method Quad
    *
    *
    */
    public Quad(Visiva1 vis) {
    super();

    this.vis=vis;
    this.setEnabled(false);
    this.addActionListener(new GestoreVisiva1(this,this.vis));
    this.setBackground(Color.BLACK);
    }
    /*public void start ()
    {
    t.start();
    }*/

    public void setDelay (long millisec)
    {
    this.d=new Delay(millisec);
    }
    public void run ()
    {
    this.clicked=true;
    this.setEnabled(true);
    this.setBackground(Color.green);
    this.d=new Delay(500);
    d.start();
    while (d.check ());
    if (this.clicked)
    {
    this.setBackground(Color.yellow);
    this.d=new Delay(500);
    d.start();
    while (d.check ());
    if (this.clicked)
    {
    this.setBackground(Color.red);
    this.d=new Delay(500);
    d.start();
    while (d.check ());
    }
    }
    this.setBackground(Color.black);
    if (this.clicked)
    {
    this.vis.addScore(-5);
    this.vis.loadLevel();
    }
    ---------
    gestione dei pulsanti e futuro main

    import javax.swing.*;
    import java.awt.*;

    public class Visiva1 extends Gioco {

    /*
    protected JFrame mainGame = new JFrame();
    protected Rules r = new Rules();
    protected static int score = 0;
    protected Timer60s tempo = new Timer60s();
    protected int lvl;
    */
    private Quad vett[]=new Quad[48];
    private Thread v2[]=new Thread[48];
    private int n=1;

    /**
    * Method Visiva1
    *
    *
    */
    public Visiva1()
    {
    //caricamento


    this.r.setText("regolamenti/visiva1.txt");

    this.mainGame.setDefaultCloseOperation
    (JFrame.DO_NOTHING_ON_CLOSE);

    this.mainGame.setLayout(new GridLayout(6,8));

    this.mainGame.setSize(800,600);

    for (int i=0;i<48;i++)
    {
    vett[i]=new Quad(this);
    this.mainGame.add(vett[i]);
    }
    }

    /**
    * Method play
    *
    *
    */
    public void play() {
    this.r.setVisible(true);
    while(this.r.getLetto());
    this.loadLevel();
    this.mainGame.setVisible(true);
    this.tempo.start();
    while(this.tempo.check());
    this.mainGame.setVisible(false);
    }
    /**
    * Method loadLevel
    *
    *
    */
    public void loadLevel()
    {
    n=(int)(lvl/(10*n))+1;
    for (int i=0;i<48;i++)
    if ((vett[i].getBackground()!=Color.black)&&(n>1))
    n--;
    int pos=(int)(Math.random()*48);
    for(int i=0;i<n;i++)
    {
    do
    pos=(int)(Math.random()*48);
    while (vett[pos].getBackground()!=Color.black);
    this.v2[pos]= new Thread(vett[pos]);
    this.v2[pos].start();
    }
    }



    }
    altri metodi e classi non immesse perchè se no sarebbe un casino
    AlexSkizzo

  2. #2

    aiuto

    Up vi prego sn immezzo alla cacca
    AlexSkizzo

  3. #3
    Non ho ben capito il tuo problema. Comunque io ti consiglierei di dare un occhio al 'Observer Design Pattern', che è molto utilizzato nella programmazione di interfaccie grafiche e a prima vista mi sembra che potrebbe autarti. Per una semplice applicazione che usa il design pattern Observer prova a vedere qui.
    Ciao

  4. #4
    Grazie veramente è molto interessante ma qua si parla di andare a riscrivere tutto il programma... in poche parole stiamo construendo io e il mio gruppo il brain training. Il problema di questo gioco è che nella visulizzazione dei quadratini rallenta xkè eseguito con i thread x far si che cambi colore con lo sleep... Gioco: si accende il quadrato di verde passando 500 millisecondi giallo e dopo rosso e poi scompare tu devi cliccare prima che sparisca naturalmente prima lo schiacci meglio è prendi più punti... vuoi l'intero workspace? mi faresti un piacere infinito... GRAZIE SEMPRE LO STESSO
    AlexSkizzo

  5. #5
    Nel riscrivere il programma puoi usare gran parte del codice che gia hai scritto. Volendo applicare il pattern Observer, nel tuo caso Quad è l'Observable e Visiva l'Observer.

    Codice PHP:

    import java
    .awt.*;
    import java.util.*;
    import java.util.logging.*;
    import javax.swing.*;
    public class 
    Main {
        public static 
    void main(String args[]) {
            
    Visiva visiva = new Visiva();
            
    Quad[] quads = new Quad[48];
            
    //inizializzo i vari quad (Observable)
            
    for(int i 0quads.lengthi++ ){
                
    Quad quad = new Quad(i);
                
    quad.getObservableQuadState().addObserver(visiva);
                
    quads[i] = quad;
            }
            
    //aggiungo i quad al JFrame (Observer)
            
    visiva.addQuads(quads);
            
    //faccio partire i timer per il cambiamento di stato
            
    for(int i 0quads.lengthi++ ){
                
    quads[i].exec();
            }
        }
    }


    class 
    ObservableQuadState extends Observable{
        
    int quadID;
        
    String state;
        public 
    ObservableQuadState(int quadID){
            
    this.quadID quadID;
        }
        public 
    void setState(String state){
            
    this.state state;
            
    this.setChanged();
        }
        @
    Override
        
    public void setChanged(){
            
    super.setChanged();
        }
        @
    Override
        
    public void notifyObservers(){
            
    super.notifyObservers(state);
        }
        public 
    int getQuadID() {
            return 
    quadID;
        }
    }
    class 
    Quad extends JButton{
        private static final 
    long delay 500;

        
    ObservableQuadState observableQuadState;
        public 
    Quad(int id){
            
    observableQuadState = new ObservableQuadState(id);
        }

        public 
    ObservableQuadState getObservableQuadState() {
            return 
    observableQuadState;
        }

        public 
    void exec(){
            try {
                
    Thread.sleep(delay);
                
    observableQuadState.setState("green");
                
    observableQuadState.notifyObservers();
                
    Thread.sleep(delay);
                
    observableQuadState.setState("orange");
                
    observableQuadState.notifyObservers();
                
    Thread.sleep(delay);
                
    observableQuadState.setState("red");
                
    observableQuadState.notifyObservers();
            } catch (
    InterruptedException ex) {
                
    Logger.getLogger(Quad.class.getName()).log(Level.SEVEREnullex);
            }
        }
    }

    class 
    Visiva extends JFrame implements Observer{
        
    Quad[] quads;
        public 
    void update(Observable oObject arg) {
            
    ObservableQuadState observableQuadState = (ObservableQuadState)o;
            
    String color = (String)arg;
            
    System.out.println("Cambiamento di stato: Quad["+observableQuadState.getQuadID()+"], stato = " color);
            
    //modifico il colore del Quand che ha subito il cambiamento
            
    ....
        }

        public 
    void addQuads(Quad[] quads){
            
    this.quads quads;
            
    //aggiungo i quads al JFrame
            
    ....
        }


  6. #6
    Grazie davvero VVoVe: ... Sei stato un Grande
    AlexSkizzo

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 © 2026 vBulletin Solutions, Inc. All rights reserved.