Visualizzazione dei risultati da 1 a 2 su 2

Discussione: mastermind help!!!!!!

  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2011
    Messaggi
    16

    mastermind help!!!!!!

    Ciao a tutti, ho creato questo metodo per la realizzazione di mastermind. Come vedete a inizia partita mi permette di trascinare col mause i colori. vorrei riuscire a memorizzare in un array di Label i colori inseriiti dall'utente.....
    Vi posto il codice
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;



    public class TestFrame extends JFrame
    {
    private ImageIcon yellow;
    private ImageIcon red;
    private ImageIcon blue;
    private ImageIcon orange;
    private ImageIcon green;
    private ImageIcon grey;
    private ImageIcon purple;
    private JLabel [] option;
    private JLabel [] scelte;
    private JLabel [] segreto;
    final int altezza=700;
    final int larghezza=420;
    final int altezzaTentativi=400;
    final int altezzaTitolo=70;
    final int larghezzaSuggerimenti=100;
    final int spazioLaterale=20;
    final int spazioSotto=100;
    private final JMenuBar BarraMenu;
    private final JMenu File;
    private final JMenuItem Inizia;
    private final JMenuItem Esci;
    private final JMenu About;
    private final JMenuItem ComeGiocare;
    private final JMenuItem jMenuItem1;
    private JPanel pTitolo= new JPanel();
    private JPanel ppresentazione = new JPanel();
    private int NumeroPosti;
    private final JLabel[] color;



    public TestFrame () throws FileNotFoundException, FontFormatException, IOException
    {
    super ("Test Frame");
    getContentPane().setLayout(null);
    try {
    yellow = new ImageIcon ("src/Yellow.png");
    red = new ImageIcon ("src/Red.png");
    blue = new ImageIcon ("src/Blue.png");
    orange = new ImageIcon ("src/Orange.png");
    green = new ImageIcon ("src/Green.png");
    grey = new ImageIcon ("src/Grey.png");
    purple = new ImageIcon ("src/Purple.png");
    } catch (Exception e) {
    System.out.println (e);
    }
    final MouseListener mouseListener = new MouseInputAdapter()
    {
    @Override
    public void mousePressed (MouseEvent me)
    {
    JComponent comp = (JComponent) me.getSource ();
    TransferHandler handler = comp.getTransferHandler ();
    try
    {
    handler.exportAsDrag(comp, me, TransferHandler.COPY);
    }
    catch(Exception e)
    {
    for(int i=0;i<6;i++)
    {
    option[i].setTransferHandler(new TransferHandler ("icon"));
    }

    mousePressed(me);

    }
    for(int i=0;i<6;i++)
    {
    if(!option[i].equals(me.getSource()))
    {
    option[i].setTransferHandler(null);
    }
    }
    }
    @Override
    public void mouseReleased (MouseEvent me)
    {
    for(int i=0;i<6;i++)
    {
    option[i].setTransferHandler(new TransferHandler ("icon"));
    }
    }
    };

    BarraMenu = new javax.swing.JMenuBar();
    File = new javax.swing.JMenu();
    Inizia = new javax.swing.JMenuItem();
    Esci = new javax.swing.JMenuItem();
    About = new javax.swing.JMenu();
    ComeGiocare = new javax.swing.JMenuItem();
    jMenuItem1 = new javax.swing.JMenuItem();
    File.setText("File");
    Inizia.setText("NuovaPartita");
    Inizia.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mousePressed(java.awt.event.MouseEvent evt) {
    for(int i=0;i<6;i++)
    {
    option[i].setTransferHandler(new TransferHandler ("icon"));
    option[i].addMouseListener (mouseListener);
    }

    }
    });
    File.add(Inizia);
    Esci.setText("Esci");
    Esci.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mousePressed(java.awt.event.MouseEvent evt) {
    EsciMousePressed(evt);
    }

    private void EsciMousePressed(java.awt.event.MouseEvent evt) {
    System.exit(0);
    }
    });
    File.add(Esci);
    BarraMenu.add(File);
    About.setText("?");
    ComeGiocare.setText("Come si gioca?");
    ComeGiocare.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mousePressed(java.awt.event.MouseEvent evt) {
    ComeGiocareMousePressed(evt);
    }
    private void ComeGiocareMousePressed(java.awt.event.MouseEvent evt) {
    JOptionPane.showMessageDialog(null, "Verrà generata una stringa di colori segreti che\n"
    +" il giocatore dovrà indovinare in massimo 6 tentativi.\n"
    +"Ad ogni tentativo inserito visualizzerete il numero di\n"
    + " colori gisti al posto giusto e il numero di colori \n"
    + "giusti al posto sbagliato in modo da riuscire a\n "
    + "indovinare la stringa segreta.", "Come si gioca?", JOptionPane.INFORMATION_MESSAGE);
    }
    });
    About.add(ComeGiocare);
    jMenuItem1.setText("About...");
    jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mousePressed(java.awt.event.MouseEvent evt) {
    jMenuItem1MousePressed(evt);
    }
    private void jMenuItem1MousePressed(java.awt.event.MouseEvent evt) {
    JOptionPane.showMessageDialog(null,"Mastermind \n"
    + "VER: 0.0.1.2 alpha\n"
    + "Program Developer: Stella Bici\n"
    + "Last Modify : 28/02/2011", "About", JOptionPane.QUESTION_MESSAGE);
    }
    });
    About.add(jMenuItem1);
    BarraMenu.add(About);
    setJMenuBar(BarraMenu);
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setSize (larghezza, altezza);

    JLabel lTitolo= new JLabel("MASTERMIND");
    lTitolo.setForeground(Color.white);
    pTitolo.setBackground(Color.black);

    File file = new File("src/SuperMarioBros..ttf");
    FileInputStream fis = new FileInputStream(file);
    Font font = Font.createFont(Font.TRUETYPE_FONT, fis);
    font = font.deriveFont(Font.PLAIN, 50);
    lTitolo.setFont(font);
    pTitolo.setSize(larghezza, altezzaTitolo);
    pTitolo.add(lTitolo);
    getContentPane().add(pTitolo);
    JPanel contenitoreRisposte = new JPanel();

    contenitoreRisposte.setLayout(null);
    contenitoreRisposte.setLocation(0, altezzaTitolo);
    contenitoreRisposte.setBackground(Color.black);

    JPanel contenitoreCGPS = new JPanel();
    JPanel contenitoreTentativi = new JPanel();
    JPanel contenitoreCGPG = new JPanel();
    contenitoreRisposte.setSize(larghezza, altezzaTentativi);

    contenitoreCGPS.setSize(larghezzaSuggerimenti, altezzaTentativi);
    contenitoreTentativi.setSize(larghezza-((larghezzaSuggerimenti+spazioLaterale)*2), altezzaTentativi);
    contenitoreCGPG.setSize(larghezzaSuggerimenti, altezzaTentativi);
    contenitoreCGPS.setBackground(Color.black);
    contenitoreTentativi.setBackground(Color.black);
    contenitoreCGPG.setBackground(Color.black);

    contenitoreCGPS.setLocation(spazioLaterale, 0);
    contenitoreTentativi.setLocation(spazioLaterale+la rghezzaSuggerimenti, 0);
    contenitoreCGPG.setLocation(larghezza-larghezzaSuggerimenti-spazioLaterale, 0);

    contenitoreRisposte.add(contenitoreCGPS);
    contenitoreRisposte.add(contenitoreTentativi);
    contenitoreRisposte.add(contenitoreCGPG);

    contenitoreCGPS.setLayout(new GridLayout(10, 4));
    contenitoreTentativi.setLayout(new GridLayout(10, 4));
    contenitoreCGPG.setLayout(new GridLayout(10, 4));

    scelte =new JLabel[40];

    for(int i=0;i<40;i++)
    {
    scelte[i]=new JLabel ("", grey, SwingConstants.CENTER);
    contenitoreTentativi.add(scelte[i]);
    // contenitoreCGPS.add(new JLabel ("", new ImageIcon ("src/WhiteB.png"), SwingConstants.CENTER));
    //contenitoreCGPG.add(new JLabel ("", new ImageIcon ("src/RedB.png"), SwingConstants.CENTER));
    }

    getContentPane().setLayout(null);
    Component add = getContentPane().add(contenitoreRisposte);

    option = new JLabel[6];

    option[0]=new JLabel ("", yellow, SwingConstants.CENTER);
    option[1]=new JLabel ("", red, SwingConstants.CENTER);
    option[2]=new JLabel ("", blue, SwingConstants.CENTER);
    option[3]=new JLabel ("", orange, SwingConstants.CENTER);
    option[4]=new JLabel ("", green, SwingConstants.CENTER);
    option[5]=new JLabel ("", purple, SwingConstants.CENTER);
    JPanel panelOption= new JPanel(new GridLayout(1, 6));


    for(int i=0;i<6;i++)
    {
    //option[i].setTransferHandler(new TransferHandler ("icon"));
    //option[i].addMouseListener (mouseListener);
    panelOption.add (option[i]);
    }
    panelOption.setLocation(spazioSotto,altezzaTentati vi+altezzaTitolo);
    panelOption.setSize(larghezza-(spazioSotto*2), 60);
    panelOption.setBackground(Color.black);
    getContentPane().setBackground(Color.black);
    getContentPane ().add (panelOption);
    segreto =new JLabel[4];
    JButton controlla =new JButton("controlla",new ImageIcon ("src/Check.png"));
    JPanel panelPulsante=new JPanel(new FlowLayout());
    panelPulsante.setSize(larghezza, 45);
    panelPulsante.setLocation(0,altezzaTentativi+altez zaTitolo+60);
    panelPulsante.add(controlla);
    panelPulsante.setBackground(Color.black);
    getContentPane().add(panelPulsante);

    color = new JLabel[6];
    color [0]=new JLabel ("", yellow, SwingConstants.CENTER);
    color [1]=new JLabel ("", red, SwingConstants.CENTER);
    color [2]=new JLabel ("", blue, SwingConstants.CENTER);
    color [3]=new JLabel ("", orange, SwingConstants.CENTER);
    color [4]=new JLabel ("", green, SwingConstants.CENTER);
    color [5]=new JLabel ("", purple, SwingConstants.CENTER);
    int j;
    for(int i=0;i<4;i++){
    j=(int)(Math.random()*5)+1;
    System.out.println(j);
    segreto[i]=color [j];
    panelPulsante.add(segreto[i]);

    }

    for(int i=0;i<4;i++)
    {
    System.out.println(segreto[i].getIcon());
    }


    inizializza(0, 4);

    }

    public static void main (String[] args) throws Exception
    {
    SwingUtilities.invokeLater (new Runnable ()
    {
    public void run ()
    {
    TestFrame f = null;
    try {
    f = new TestFrame();
    } catch (FileNotFoundException ex) {
    Logger.getLogger(TestFrame.class.getName()).log(Le vel.SEVERE, null, ex);
    } catch (FontFormatException ex) {
    Logger.getLogger(TestFrame.class.getName()).log(Le vel.SEVERE, null, ex);
    } catch (IOException ex) {
    Logger.getLogger(TestFrame.class.getName()).log(Le vel.SEVERE, null, ex);
    }
    f.setVisible (true);
    }
    });
    }

    public void inizializza(int tentativo,int difficolta)
    {
    if(tentativo!=0)
    {
    for(int i =((tentativo-1)*(difficolta)); i< tentativo*difficolta;i++)
    {
    scelte[i].setTransferHandler(null);
    }
    }
    if(tentativo!=10)
    for(int i=tentativo*difficolta;i<(tentativo+1)*difficolta; i++)
    {
    scelte[i].setTransferHandler(new TransferHandler ("icon"));
    }

    }

    }

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,480

    Moderazione

    Suggerisco innanzitutto di leggere il Regolamento di quest'area del forum per conoscere tutte le regole da seguire nell'apertura e conduzione delle discussioni.

    In particolare, il codice va formattato usando il tag [CODE] e il titolo dovrebbe essere significativo; suggerisco inoltre di "spezzare" i problemi in parti più piccole in modo da affrontarli singolarmente all'interno di una discussione, riportando solo la parte di codice interessata dall'errore o dal comportamento anomalo del programma.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

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.