Add an event production and basic interface of the game - the design of Java-based software Lianliankan game (two)

  Home to the last completed production of the game, and today the completion of a simple game based interface to add production and events. Since the function has not been fully realized, and therefore the game menu list interface simply added a bit, the rest of the menu list and countdown perfect one by one in subsequent productions.

 

  1. First, based on the increased yesterday frame1 method frame2 (), frame2 () to generate the game interface.

    frame2 game interface window, added to the frame2 Panel2, Panel2 set BorderLayout layout, the layout is used for convenience in making the top of the menu interface, a menu which can be fixed NORTH, and the pattern on the body Lianliankan CENTER, easy to both were produced without confusion.

  Add to the central panel2 panel1, panel1 lianliankan to place graphics, since the arrangement pattern is very regular, it is easy to think of a GridLayout layout. Good graphics Lianliankan advance planning several odd row, directly on the layout can be set directly at initialization time. I have here is a simple mode 9 * 14 ..

  

    static the JFrame Frame2 = new new the JFrame ( "lianliankan large work" ); 
    the JPanel Panel1 = new new the JPanel (); // GridLayout 
    the JPanel Panel2 = new new the JPanel (); // BorderLayout 
   
    // layout 
    panel1.setLayout ( new new GridLayout (9,14, 0,0)); // in accordance with 9 * 14 grid disposed 
    panel2.setLayout ( new new the BorderLayout ()); 
    panel2.add (Panel1, BorderLayout.CENTER); 
    frame2.add (Panel2);

  Then is the same as before the settings window icon, in fact, this is optional, but always with a cup of coffee as the Java icon more than there is nothing wrong, so I put the badge inserted directly into it.

        = Tool Toolkit frame1.getToolkit (); 
        Image image1 = tool.getImage ( "E: \\ \\ Sophomore \\ learning software design Picture \\ hit.jpg \\" ); 
        frame2.setIconImage (image1);

  This part no technical content, not do too much explanation.

  2. Production of simple menu bar. As already said, because the feature is not yet fully realized, the menu list simply added a bit, the rest of the menu list and do the countdown perfect one by one in subsequent productions.

  I'm just here to add background color of the menu bar and two buttons. The background color of choice here is necessary to explain, we all know that if a color class RED, BLUE, etc. set the background color direct color shades of light and dark can not choose, can cause the whole picture looks great contrast. The pixel is set by the need to clearly know a few words of value associated with that color, and it is very convenient. Here there is a very convenient way to use decode function color by developer mode browser can easily find a favorite color coding. There are many tutorials on the web, where I will not do too much to explain, in short, you can easily adjust the color by color code. I had put a tutorial made reference to: https: //blog.csdn.net/ArthurCaoMH/article/details/85923949

        // Add button and set the menu background (not completed) 
        JPanel PANEL = new new JPanel (); 
        JButton Button1 = new new JButton ( "Exit" ); 
        JButton Button2 = new new JButton ( "restart" ); 
        panel_button.add (Button2); 
        panel_button.add (Button1); 
        panel_button.setOpaque ( to true ); 
        panel_button.setBackground (Color.decode ( "# dbe4f5" )); 
        panel2.add (PANEL, BorderLayout.NORTH);

  3. Add the main Lianliankan - graphics. Here I use the method is relatively low, and figure out which grid should be graphics, which should be the background grid, and then set up one by one.

  Add graphics JButton I use directly on the JButton panel1 grid. Initially misuse became JLabel, JLabel but later found not convenient for event handling, so they changed to JButton. The use of surrounding background JLabel, JLabel directly set the background color to their favorite color, and can be added to the grid.

  This part of my code is relatively complex, because my method to add components used in a grid basis of comparison, easy to write but to think too much trouble.

  Also add a picture of the use of a random number, in fact, a simple random possibilities there will be no solution, I leave this issue to the back of the handle, where no solution is not considered a direct plug random violence.

  There are other issues involved in the following code I've added a comment, I changed this part for a long time, because the interface has been playing less than the effect I want. To not do so ugly, in many parts of the production process to be considered.

          // add graphics (not completely random) and game background 
          String [] logo = { "E : \\ \\\\ Sophomore \\ learning software design \\ picture \\ c.jpg", "E : \\ learning \ \ Sophomore \\ software design \\ picture \\ c ++ jpg ", " E:. \\ \\ Sophomore \\ learning software design \\ picture \\ go.jpg "," E : \\ \\ Sophomore \\ learning software design \\ picture \\ eclipse.jpg "," E : \\ \\ Sophomore \\ learning software design \\ picture \\ codeblocks.jpg "," E : \\ learning \\ Sophomore \\ \\ software design picture \\ java.jpg "," E: \\ \\ Sophomore \\ learning software design \\ picture \\ js.jpg "," E : \\ \\ Sophomore \\ learning software design \\ picture \\ liteide .jpg "," E: \\ \\ Sophomore \\ learning software design \\ picture \\ PHP.jpg "," E : \\ \\ Sophomore \\ learning software design \\ picture \\ python.jpg ", "E: \\ \\ Sophomore \\ learning software design \\ picture \\ sql.jpg", "E : \\ \\ Sophomore \\ learning software design Picture \\ vs.jpg \\" }; 
          the JButton [] [] imgbutton = new new the JButton [. 7] [. 11];// use array stores Button 
          the ImageIcon [] [] = IMG new newThe ImageIcon [. 7] [. 11]; // use arrays to facilitate later event is determined 
        
            for ( int I =. 1; I <14; I ++) { // first row are blank, for later use to connect 
            the JLabel WhiteLabel = new new the JLabel ( ); 
            whitelabel.setOpaque ( to true ); 
            whitelabel.setBackground (by Color.decode ( "# e1edef" )); 
            panel1.add (WhiteLabel); 
        } 
        
            for ( int I = 0; I <. 7; I ++) { // 2- 8 lines 
            the JLabel WhiteLabel = new new the JLabel (); 
            whitelabel.setOpaque ( to true ); 
            whitelabel.setBackground (by Color.decode ("# e1edef" )); 
            panel1.add (WhiteLabel); // first column of each row of blanks 
            for ( int J = 0; J <. 11; J ++) { // 2-12 column 
                the Random = R & lt new new the Random ( );
                 int RAND = r.nextInt (12 is ); 
                IMG [I] [J] = new new the ImageIcon (logo [RAND]); 
                IMG [I] [J] .setImage (IMG [I] [J] .getImage () .getScaledInstance ( 62 is, 58 , Image.SCALE_DEFAULT)); 
                imgbutton [I] [J] = new new the JButton (IMG [I] [J]); // insert picture Button 
                imgbutton [I] [J] .setOpaque ( to true );
                imgbutton [i] [J] .setBackground (Color.decode ( "# e1edef" )); // sets the background color of each button is consistent with the surrounding, otherwise the picture will be around for the gray 
                panel1.add (imgbutton [i] [ j]); // i-th row j-th column pattern 
            } 
            the JLabel whitelabel2 = new new the JLabel (); 
            whitelabel2.setOpaque ( to true ); 
            whitelabel2.setBackground (by Color.decode ( "# e1edef" )); 
            panel1.add ( whitelabel2); // for each row last column is blank 
        } 

          for ( int I = 0; I <13 is; I ++) { // 9, line 
            the JLabel WhiteLabel = new new the JLabel (); 
            whitelabel.setOpaque ( to true);
            whitelabel.setBackground(Color.decode("#e1edef"));
            panel1.add(whitelabel);
        }          

  4. The following is a registered event listeners. I just wrote this part of the framework, the specific code is not yet complete. Function is another category where I want to start writing, and only to hold the head so. Framework put first event listener. Here used anonymous functions, I have not noticed before final variable in the anonymous function can only be used outside, so look for a long time to find a bug. This part of the follow-up and may be changed, cautious reference!

        // Add Event Listeners 
        the try {
             for ( int I = 0; I <. 7; I ++ ) {
               for ( int J = 0; J <. 11; J ++ ) {
                 Final  int m = I;
                 Final  int n-= J; 
                imgbutton [ I] [J] .addActionListener ( new new the ActionListener () {
                     // If the stack is empty at this time, the stack; if the stack is not empty, then the flag is determined 
                    public  void the actionPerformed (the ActionEvent Event) {
                         IF (Function.stack.empty ()) { // If the stack is empty at this time, click on the button stack 
                            int[] = ARR {m, n-}; // the button is clicked as an array ij stack 
                            Function.stack.push (ARR); // array stack 
                        } the else { 
                            Function.flag (Function.stack, m, n- ); // execute In Flag () function 
                        }    
                    } 
                }); 
            } 
        } 
        } the catch (Exception E) { 
            System.err.print (E); 
        } 
        frame2.setSize ( 1000,650 ); 
        frame2.setVisible ( to true );
 
    

  At present the finished renderings put the first, relatively low levels of demand light spray, and welcome to share!

(Current renderings, icons cater to the majority of programmers like ha ha ha)

 

 

Finally, put a few pictures of the production process for your reference about the process, but I wish you all detours ~

 

(这里是因为只添加了图片,没有设置背景颜色导致里面都是灰色)

 

 

 (隐隐约约不对劲了没有?这是还没有意识到应该用button,我用的JLabel就是这种效果)

 

 (这里也还没有意识到,只是完善了菜单栏的颜色)

 

 (这里终于意识到了,但是一开始把所有的网格都设置成了JButton,奇丑无比不说,周围居然可以点击在功能上很不对了)

 

 这是目前的界面~期待后续优化~

Guess you like

Origin www.cnblogs.com/xioayantongxue/p/12064695.html