Results 1 to 5 of 5
  1. #1
    Alexander_003 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default problem mastermind

    Hello guys ,
    I have to make a mastermind game for school but I've some problem doing that .
    The problem is that if I click on the buttons that I want to use for making the guess nothing happens . Below I will post the classes that are used tot do the view .

    anyone have a suggestion ?

    Greetings

    PHP Code:
    package View;
    
    import Mastermind.Colour;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
    
    /**
     * author: Alexander Coopmans
     * version: 1.0 4/03/11 12:35
     */
    
    public class Panels extends JFrame{
    
    
        private final Color[] colors = {
                Color.RED, Color.GREEN, Color.BLUE,  Color.YELLOW, Color.orange,Color.pink,
                Color.BLACK,new Color(163,73,164),new Color(185,122,87),Color.WHITE,getBackground()};
    
        private JMenuBar mbar;
        private JButton check;
        private Colour [] feedb;
        private Colour [] keuze;
        private JMenuItem mnew;
        private JMenuItem mninst;
        private JMenuItem mninfo;
        private JMenuItem mnhigh;
        private JMenuItem mnload;
        private JMenuItem mnexit;
        private JMenu mngame;
        private JMenu mnhelp;
    
    
    
        public Panels() throws HeadlessException {
            initialiseerComponenten();
            menu();
            maakEventHandlers();
            toonFrame();
    
    
        }
    
        private void initialiseerComponenten() {
    
            keuze = new Colour[4];
            feedb=new Colour[4];
            check=new JButton("Check");
            mbar=new JMenuBar();
            mngame=new JMenu("Game ");
            mnhelp=new JMenu("Help ");
            mninst=new JMenuItem("Read Instructions ");
            mninfo=new JMenuItem("Info ");
            mnew=new JMenuItem("New game");
            mnhigh=new JMenuItem(" View Highscore");
            mnload=new JMenuItem("Load Game");
            mnexit=new JMenuItem("Exit");
    
    
        }
    
    
    
        private void layoutComponenten() {
    
            JPanel mainframe=new JPanel(new GridLayout(1,2));
            JPanel kleuren = new JPanel(new GridLayout(10,4));
            JPanel allekleuren = new JPanel(new GridLayout(2,9));
            JPanel feedback = new JPanel(new GridLayout(10,4));
    
           for(int j=0;j<10;j++){
                for(int i=0;i< 4 ;i++){
    
                    kleuren.add(this.keuze[i] = new Colour(getBackground(), 0, 0, 25));
                    feedback.add(this.feedb[i]=new Colour(getBackground(),0,0,12)).setEnabled(false);
    
                }
          }
    
    
    
            for (int i=0;i<colors.length;i++) {
    
                TransferHandler transfer = new TransferHandler("text");
                keuze[0].setTransferHandler(transfer);
    
                allekleuren.add(new Colour(colors[i], 0, 0, 25)).addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent me) {
    
                }
            });
    
    
    
            }
            allekleuren.add(check,BorderLayout.SOUTH);
            kleuren.setSize(500, 400);
            add(allekleuren, BorderLayout.SOUTH);
            mainframe.add(kleuren);
            mainframe.add(feedback);
    
            add(mainframe);
        }
        private void menu() {
            mbar.add( mngame);
            mngame.add(mnew);
            mngame.addSeparator();
            mngame.add(mnhigh);
            mngame.add(mnload);
            mngame.add(mnexit);
            mbar.add(mnhelp);
            mnhelp.add(mninst);
            mnhelp.add(mninfo);
            setJMenuBar(mbar);
    
        }
    
        private void maakEventHandlers() {
    
            mnew.addActionListener( new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
    
    
                    new PopupScreen();
    
                    layoutComponenten();
                    toonFrame();
                }
            });
    
            mnhigh.addActionListener( new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
    
                    // show score 
                }
            });
            mnload.addActionListener( new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    // load game 
    
                }
            });
    
            mnexit.addActionListener( new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
    
            mninst.addActionListener( new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
    
                    try {
                        Scanner scanner = new Scanner(new FileReader("instructions.txt"));
                        while (scanner.hasNext()){
                            String string = scanner.nextLine();
                            JOptionPane.showMessageDialog(null, string + "\n", "Instructies: ", JOptionPane.INFORMATION_MESSAGE);
                        }
                        scanner.close();
                    }
                    catch (FileNotFoundException ea) {
                        System.out.println(" bestand instructions.txt  niet gevonden!");
    
                    }
                }
            });
    
            mninfo.addActionListener( new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
    
                    new SplashScreen();
    
    
                }
            });
            check.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // hier kijken de code die ingegeven werd controleren
    
                }
            });
    
        }
    
    
        private void toonFrame() {
            setTitle("** Mastermind **");
            setPreferredSize(new Dimension(439, 498));
            setMinimumSize(new Dimension(439,389));
            //  setResizable(false);
    
            //setExtendedState(JFrame.MAXIMIZED_BOTH);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setVisible(true);
    
        }
    
    }
    
    
    
    
    package Mastermind;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     * Created by IntelliJ IDEA.
     * User: Nick Weymiens en Alexander Coopmans
     * Date: 18/02/11
     * Time: 11:50
     * To change this template use File | Settings | File Templates.
     */
    public class Colour extends JButton
    {
        private Color color;
    
        public enum Colours {RED,GREEN,YELLOW,BLUE,PURPLE,ORANGE,BROWN,PINK,BLANK,WHITE, BLACK}    // used for model side 
        private int x,y,size;
    
    
        public Colour(Color color, int x, int y, int size) {
            this.color = color;
            this.x = x;
            this.y = y;
            this.size = size;
    
            super.setBorder(null);
            super.setBackground(Color.WHITE);
            super.setOpaque(true);
            final Color kleur=color;
            this.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   System.out.println("geklikt op" +kleur);
                   setColor(kleur);
    
                }
           });
        }
    
    
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(color);
            g.fillOval(x, y, size, size);
        }
    
        public void setColor(Color color) {
            this.color = color;
    
        }
    
        public Color getColor() {
            return color;
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    That's way too much code for the question- instead, try narrowing your problem down to an SSCCE.

    But here's a question- when are you adding your ActionListener to the button? What does that ActionListener do?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    This code may be causing problems:
    Java Code:
    allekleuren.add(new Colour(colors[i], 0, 0, 25)).addMouseListener(new MouseAdapter() { 
        @Override 
        public void mousePressed(MouseEvent me) { 
    
        } 
    });
    I think it overrides the action listener you assign in the Colour constructor.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    Quote Originally Posted by berkeleybross View Post
    This code may be causing problems:
    Java Code:
    allekleuren.add(new Colour(colors[i], 0, 0, 25)).addMouseListener(new MouseAdapter() { 
        @Override 
        public void mousePressed(MouseEvent me) { 
    
        } 
    });
    I think it overrides the action listener you assign in the Colour constructor.
    Why would a MouseListener interfere with an ActionListener? Did you try this out before posting your advice?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Alexander_003 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    The idea is that by clicking a one of the buttons of the pannel allekleuren the first button of the array keuze should take the color that is pressed and so on .

    But I don'y know how to do it

Similar Threads

  1. MasterMind Assignement , Use of String Class
    By Ebayer in forum New To Java
    Replies: 2
    Last Post: 03-14-2011, 07:23 PM
  2. Mastermind!
    By MishkaRX in forum New To Java
    Replies: 11
    Last Post: 12-14-2010, 02:39 PM
  3. MasterMind Problem!!
    By papinhio in forum New To Java
    Replies: 4
    Last Post: 07-09-2010, 04:51 PM
  4. how to make mastermind game
    By javabeginer in forum New To Java
    Replies: 10
    Last Post: 04-14-2009, 03:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •