Results 1 to 3 of 3
  1. #1
    Khaled is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Thumbs up Creating Classes for my code!

    Hi guys,
    I am new to Java and I need your help please. I created the below code and it works fine. It is a deck card game. I just want to divide my code into classes so that it seems more organized.

    This is a snapshot of my code:
    Creating Classes for my code!-one.jpg
    Creating Classes for my code!-two.jpg


    Java Code:
    package getimage;
    
    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    import javax.swing.*;
    
    public class GetImage extends JFrame {
        
        public JPanel northPanelbutton = new JPanel();
        public JPanel southPanelbutton = new JPanel();
        public JPanel westPanelbutton = new JPanel();
        public JPanel eastPanelbutton = new JPanel();
        public JPanel centerPanelbutton = new JPanel();
      //  public JPanel centerPanelLabel = new JPanel();
        public JPanel southPanelButtonBuyer = new JPanel();
                 
        public JPanel northPanel = new JPanel();
        public JPanel southPanel = new JPanel();
        public JPanel eastPanel  = new JPanel();
        public JPanel westPanel = new JPanel();
        public JPanel centerPanel = new JPanel();
        
        JButton[] button = new JButton[32];
       
        JButton buttonBuySun = new JButton("First");
        JButton buttonBuyRuler = new JButton("Second");    
        JButton buttonBuyGiveToFriend = new JButton("Third");
        JButton buttonBuyNo = new JButton("Forth");
        
        // labels in the center
        GridBagConstraints c = new GridBagConstraints();
        public JLabel labelFirstCenter = new JLabel();
        public JLabel labelSecondCenter = new JLabel();
        public JLabel labelThirdCenter = new JLabel();
        public JLabel labelForthCenter = new JLabel();
        
        int[] createRandomCard = new int[32];
        
       // int[] sortArray = new int[32];
        
        public ImageIcon[] showCard = new ImageIcon[32];   
        public ImageIcon[] emptyCard = new ImageIcon[32];   
    // int[] c = new int[32];
                
        public GetImage()
        {
            // create random number 
            ArrayList<Integer> numbers = new ArrayList<Integer>();
            for(int i = 0; i < 32 ; i++)
            {
                numbers.add(i+1);
            }
            Collections.shuffle(numbers);      
            for(int n =0; n < 32 ; n++)
            {
               createRandomCard[n] = numbers.get(n);                 
            }
            
            // create button, full image, and empty image arrays
            for(int i = 0; i<= 31; i++)
            {
                button[i] =  new JButton();         
                showCard[i] = new ImageIcon(getClass().getResource("card" + createRandomCard[i] + ".gif"));
                
                
             //   emptyCard[i] = new ImageIcon(getClass().getResource("b" + createRandomCard[i] + ".gif"));        
            }  
            
            northPanelbutton.setLayout(new GridLayout(1, 5));
            southPanelbutton.setLayout(new GridLayout(1, 6));
            westPanelbutton.setLayout(new GridLayout(5, 1));
            eastPanelbutton.setLayout(new GridLayout(5, 1));
            centerPanelbutton.setLayout(new GridLayout(1, 1));
      //      centerPanelLabel.setLayout(new GridLayout(4,1));
    
            northPanel.setLayout(new BorderLayout());
            southPanel.setLayout(new BorderLayout());
            eastPanel.setLayout(new BorderLayout());
            westPanel.setLayout(new BorderLayout());
            centerPanel.setLayout(new BorderLayout());
    
    
            // create button, but do not assin pics!
            for(int i = 0; i<= 4; i++)
            {       
                  southPanelbutton.add(button[i]);
                  button[i].addActionListener(new calculateSun());
            }
            for(int i = 5; i<= 9; i++)
            {                        
                eastPanelbutton.add(button[i]);
                button[i].addActionListener(new calculateSun());
            }
            for(int i = 10; i<= 14; i++)
            {         
                northPanelbutton.add(button[i]);
                button[i].addActionListener(new calculateSun());
            }
            for(int i = 15; i<= 19; i++)
            {                        
    
                westPanelbutton.add(button[i]);
                button[i].addActionListener(new calculateSun());
            }
            for(int i = 20; i< 21; i++)
            {
                centerPanelbutton.add(button[i]);
                button[i].addActionListener(new calculateSun());
            }
    
            southPanelButtonBuyer.add(buttonBuySun);
            southPanelButtonBuyer.add(buttonBuyRuler);
            southPanelButtonBuyer.add(buttonBuyGiveToFriend);
            southPanelButtonBuyer.add(buttonBuyNo);
    
            southPanelbutton.add(southPanelButtonBuyer);
    
            for(int j =0; j < 5; j++)
            {
                button[j].setIcon(showCard[j]);
            }
            for(int j = 5; j <= 19; j++)
            {
    
                button[j].setIcon(showCard[j]);
            }
    
            for(int j = 20; j <= 20; j++)
            {
               button[j].setIcon(showCard[j]);
            }        
            northPanel.add(northPanelbutton);
            centerPanel.add(centerPanelbutton);
            southPanel.add(southPanelbutton);
            eastPanel.add(eastPanelbutton);
            westPanel.add(westPanelbutton);
    
            add(northPanel, BorderLayout.NORTH);
            add(centerPanel, BorderLayout.CENTER);
            add(southPanel, BorderLayout.SOUTH);
            add(eastPanel, BorderLayout.EAST);
            add(westPanel, BorderLayout.WEST);    
            buttonBuySun.addActionListener(new AddSunListener());
            
            setTitle("Connect 4");
            setSize(1400, 1000);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
        
    public static void main(String[] args) 
    {
        GetImage aa = new GetImage();      
    }    
    class AddSunListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
        {
            addActionAllButtons();
            northPanelbutton.setLayout(new GridLayout(1, 8));
            southPanelbutton.setLayout(new GridLayout(1, 8));
            westPanelbutton.setLayout(new GridLayout(8, 1));
            eastPanelbutton.setLayout(new GridLayout(8, 1));
            centerPanelbutton.setLayout(new GridLayout(4, 4));
            southPanelbutton.remove(southPanelButtonBuyer);
    
            for(int j = 20; j <= 20; j++)
            {
                centerPanelbutton.setVisible(false);
                southPanelButtonBuyer.setVisible(false);
                button[j].setIcon(showCard[j]);              
                southPanelbutton.add(button[j]);
                button[j].addActionListener(new calculateSun());
                southPanelbutton.validate();
                southPanelbutton.repaint();                
            }            
            for(int j = 21; j<= 22; j++)
            {  
                button[j].setIcon(showCard[j]);
                southPanelbutton.add(button[j]);
                button[j].addActionListener(new calculateSun());
                southPanelbutton.validate();
                southPanelbutton.repaint();
            }
            for(int j = 23; j<= 25; j++)
            {             
                button[j].setIcon(showCard[j]);
                eastPanelbutton.add(button[j]);
                button[j].addActionListener(new calculateSun());
                eastPanelbutton.validate();
                eastPanelbutton.repaint();
            }
            for(int j = 26; j<= 28; j++)
            {
                button[j].setIcon(showCard[j]);
                northPanelbutton.add(button[j]);
                button[j].addActionListener(new calculateSun());
                northPanelbutton.validate();
                northPanelbutton.repaint();
            }
            for(int j = 29; j<= 31; j++)
            {
                button[j].setIcon(showCard[j]);
                westPanelbutton.add(button[j]);
                button[j].addActionListener(new calculateSun());
                westPanelbutton.validate();
                westPanelbutton.repaint();
            }
        }
    }
    
    // add action listner for all buttons
    public void addActionAllButtons()
    {
        for(int i = 0; i < 32; i++)
        {
            button[i].addActionListener(new ButtonListener());
        }
    }
    class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {  
            for(int i = 0; i < 32; i++)
            {
                if(button[i] == e.getSource())
                {
                    if(button[i] == button[0] || button[i] == button[1] ||
                       button[i] == button[2] || button[i] == button[3] ||
                       button[i] == button[4] || button[i] == button[20] ||
                       button[i] == button[21] ||  button[i] == button[22]
                      )
                    {
                        southPanelbutton.remove(button[i]);  
                        southPanelbutton.validate();
                        southPanelbutton.repaint();
                                                                
                        addFirstCenterLabel();
                        labelThirdCenter.setIcon(showCard[i]);                    
                    }
                }
                if(button[i] == e.getSource())
                {
                    if(button[i] == button[5] || button[i] == button[6] ||
                    button[i] == button[7] || button[i] == button[8] ||
                    button[i] == button[9] || button[i] == button[23] ||
                    button[i] == button[24] ||  button[i] == button[25]
                    )
                    {
                    eastPanelbutton.remove(button[i]);  
                    eastPanelbutton.validate();
                    eastPanelbutton.repaint();              
                    addSecondCenterLabel();
                    labelForthCenter.setIcon(showCard[i]);                   
                    }
                }
                if(button[i] == e.getSource())
                {
                    if(button[i] == button[10] || button[i] == button[11] ||
                    button[i] == button[12] || button[i] == button[13] ||
                    button[i] == button[14] || button[i] == button[26] ||
                    button[i] == button[27] ||  button[i] == button[28]
                    )                
                    {
                    northPanelbutton.remove(button[i]);  
                    northPanelbutton.validate();
                    northPanelbutton.repaint();
                    
                    addThirdCenterLabel();
                    labelSecondCenter.setIcon(showCard[i]);                 
                    }
                }           
                if(button[i] == e.getSource())
                {
                    if(button[i] == button[15] || button[i] == button[16] ||
                    button[i] == button[17] || button[i] == button[18] ||
                    button[i] == button[19] || button[i] == button[29] ||
                    button[i] == button[30] ||  button[i] == button[31]
                    )
                    {
                    westPanelbutton.remove(button[i]);  
                    westPanelbutton.validate();
                    westPanelbutton.repaint();              
                    addForthCenterLabel();
                    labelFirstCenter.setIcon(showCard[i]);                        
                    }                                
                }
            }
        }
        
    public void addFirstCenterLabel()
    {    
       // centerPanelLabel.add(labelFirstCenter);              
      //  centerPanel.add(centerPanelLabel);
        // down table 
       centerPanel.setLayout(new GridBagLayout());
        c.gridx = 1;
        c.gridy = 3;
        centerPanel.add(labelThirdCenter,c);
        add(centerPanel);
    }
    public void addSecondCenterLabel()
    {       
        //centerPanelLabel.add(labelSecondCenter);      
       // centerPanel.add(centerPanelLabel);
    
        // right table
        c.gridx = 2;
        c.gridy = 1;
        centerPanel.add(labelForthCenter,c);
        add(centerPanel);
    }
    public void addThirdCenterLabel()
    {       
      //  centerPanelLabel.add(labelThirdCenter);              
       // centerPanel.add(centerPanelLabel); 
        // top label
        c.gridx = 1;
        c.gridy = 0;
        centerPanel.add(labelSecondCenter,c);
        add(centerPanel);
    }
    public void addForthCenterLabel()
    {       
        //centerPanelLabel.add(labelForthCenter);              
        //centerPanel.add(centerPanelLabel); 
        
        // left table 
        c.gridx = 0;
        c.gridy = 1;
        centerPanel.add(labelFirstCenter,c);
        add(centerPanel);
    }
    }
    
    class calculateSun implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
        {
    
    
        }
    }
    
    
    
    } // end main class

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Creating Classes for my code!

    Before you try and make your code more compact with classes or whatever you may want to focus on some more basic stuff.

    There are some things I don't quite get. For example, look at your ButtonListener class. You have four if blocks, all checking the same thing. e.getSource() will not change within the for loop.

    And why are you creating a new ButtonListener instance for each button? Since I don't see that you are doing anything different that depends on a certain button, I would think one shared listener would work.

    On the other hand, if it works, then leave it alone. Or make a copy before you start to "improve" it.

    Regards,
    Jim
    Last edited by jim829; 05-17-2014 at 06:05 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    mousumiy is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Creating Classes for my code!

    Can you help understand what your ButtonListener class is doing exactly?
    e.getSource() might not change within the for loop, I guess you need to first optimize your code and then think about making it more readable.

Similar Threads

  1. Creating New Classes
    By Syed Rizvi in forum New To Java
    Replies: 2
    Last Post: 07-31-2013, 05:47 AM
  2. Help with Creating Classes
    By d300539 in forum New To Java
    Replies: 5
    Last Post: 04-08-2013, 12:08 AM
  3. please help.creating a source code for classes
    By adrianking in forum New To Java
    Replies: 5
    Last Post: 03-04-2013, 04:40 PM
  4. Creating inner classes
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:07 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
  •