Arrays An array is a type of variable that is able to hold more than one piece of information under a single variable name. Basically you are sub-dividing a memory box into many numbered slots that can be accessed and changes independently from one another. Example. Tic-Tac-Toe Without arrays the board needs to have 9 separate memory boxes to represent the game board. Int s1=0,s2=0,s3=0,s4=0 s9=0; With and array we can define a single variable and give it the number of slots that we require (in this case 9) Int [ ] board; board=new int [9]; Placing values into the slots I could do this board[0]=0; board[1]=0; board[8]=0; for comparison and much easier for (int x=0;x<9;x++) board[x]=0; *** The second example uses a counted loop to move through the slots and place a zero in each slot
Intro to Graphical User Interfaces GUIs allow our programs to be a lot more attractive and current looking. Ultimately it will allow us to combine text and graphics features to give our programs greater appeal and functionality. To implement a GUI Step 1: import javax.swing.*; import java.awt.*; import java.awt.event.*; These are outside libraries of code that allow us to build and use our GUI. Step 2: In the class creation line add the following extends JFrame Step 3: Panels are used to group objects or items used to build the GUI with (like labels and textfields) To create a panel JPanel panel1=new JPanel(); Step 4: Basic objects can now be created and added to the JPanel JLabel label1=new JLabel( Hello World ); JTextField text1=new JTextField ( Type Here,20);
Labels are used to display information TextFields are used to allow a person to type information into a program Step 5: Add the newly created objects onto the JPanel panel1.add(label1); panel1.add(text1); Step 6; Add the JPanel onto the JFrame this.add(panel1); Step 7; Set the size of the JFrame this.setsize(300,300); Step 8; Make the JFrame visible this.setvisible(true);
Setting Layouts in a JPanel or JFrame By default, the Layout used by Java is called FlowLayout. FL places objects on the JFrame or JPanel left to right (as much as it can) and then top to bottom (starting an new line as there is no more space on the previous line). This all depends on the size and/or shape of the JFrame or JPanel. A very common Layout used also is GridLayout GL as you might expect will allow you to divide a panel or frame into a set number of rows/columns and it will place objects into the cells created in the grid. How do you do this Panelname.setLayout(new GridLayout(4,2)); *** where Panelname is the name of your panel. If you want a GL on the Jframe itself use the word this
Creating a Button in Java Step 1: Add the words implements ActionListener at the end of the class creation line (after extends JFrame) Step 2: Add the actionperformed method to the bottom of your program. Again keep it inside the last curly bracket that finishes the Class. public void actionperformed(actionevent e) if (e.getsource ()==b1) *** this is where the codes for the button called b1 goes Note that b1 is the name of a button object. Your actual button may be named something else. Step 3: Create a button variable JButton b1,b2,b3; This line needs to be located inside the start of the class but before the main subroutine. Step 4: Inside the GUI creation subroutine you need to assign a JButton to your button variable declared in Step 3. b1=new JButton ( OK ); Step 5: Add the ActionListener to the button b1.addactionlistener(this);
Step 6: Add the button to a panel and then add to the frame (Just like with a label) panel1.add(b1); this.add(panel1);
Changing the Text on a JButton or JLabel or JTextField objectname.settext( text you want to display ); everything you display on screen needs to be String based (nonnumeric) so if it is numeric it needs to be converted to a String first. Getting information FROM an object in a GUI Information coming into a program should really only come from a JTextField. variablename=textfieldname.gettext(); ex. String MyName; MyName=txtName.getText(); Changing the content of a GUI object Objectname.setText( Hello World ); Ex. Label1.setText( Hello World ); Text1.setText( Please enter your name ); Label2.setText(name); *** where name is a memory box (variable)
Converting Strings (or text) into Values In order to convert text info into a value or number you need the following built-in functions double value = Double.parseDouble(string); int value = Integer.parseInt(string); where value is the name of the variable you are assigning the converted string to AND String is the name of a string variable containing a number OR a gettext statement. example int v1 = Integer.parseInt(text1.getText()); this example will take text info from a text object called text1 and store it in an integer variable called v1
GUIs Continued Changing Color and Fonts in GUI Objects Step 1 Define Font(s) and Color(s) to be used just inside the CLASS title line where we are currently defining our other variable names example Font f = new Font("Courier", Font.BOLD, 30); Color c1=new Color(100,50,30); These 2 lines define a Font called f and a Color called c1 just like variables they could really be called anything. Step 2 When it comes to actually applying a font or color to an object we do the following Identify the object we want to alter put a dot identify what you want to change (Foreground or Background) b1.setforeground(c1); b1.setbackground(color.green); or set the font b1.setfont(f); In order to change the background color of a JLabel you need to use the following command as well for each label affected. label1.setopaque(true); *** by default labels are transparent and in order to see the colour they need to be made opaque (or solid).
Creating Arrays of Objects An object array in Java allows you to create MANY occurrences of the same kind of object (ex. A JLabel), have all of the occurrences of the JLabel share the SAME name, but still differentiate those individual labels using and index value. Example Label[0]= A ; Label[1]= B ; Label[25]= Z ; Steps to creating an object array Step 1: Indicate that you intend to create an array of objects when you initially declare the variable name for those objects by using a set of square brackets JLabel [ ] letters; Steps 2: When you know how many objects you need in the array declare the size of the object array in the main class letters=new JLabel[15]; *** the 15 could be any value OR it could be a value generated by another function Step 3: Each individual object then needs to be created, formatted, added to a panel (possible attached to an ActionListener) in order to be functional. This usually involves a counted loop For (int a=0;a<15;a++)
letters[a]=new JLabel ( $ ); panel1.add(letters[a]); *** where dollar sign ($) could be anything that you wanted to appear on the object. If they were Jbuttons then adding the ActionListener would take place inside the loop Step 4 (for any object with an ActionListener) In step 3 when using a JButton (instead of JLabel) we also need to add each button in the array to the ActionListener Likewise, in doing this we need to determine which specific button in the "array of buttons" is pressed when the program jumps to the ActionPerformed subroutine. To do this we need a loop in the ActionPerformed sub for(int d=0;d<9;d++) if (e.getsource==buttonset1[d]) if (player==1) buttonset1[d].settext("x"); else buttonset1[d].settext("o");
JButton [ ] buttonset1; buttonset1=new JButton[9]; For (int a=0;a<9;a++) buttonset1[a]=new JButton ( TTT ); panel1.add(buttonset1[a]); buttonset1[a].addactionlistener(this);
Object Arrays An object array is an object (JButton, JLabel, JTextField, etc...) that is created under a singular name, but is duplicated by creating a numbered set of that object. All of the objects within the set of numbered objects operates independently of one another and can be given different functionality. The advantage of an object array (and even a data array) is that we can mass process/create array objects/data slots using a counted loop to assist with the process. To create an object array... Step 1 In the initial declaration indicate if the object being created is going to be an array of objects (ie more than one). JButton [ ] ButtonSet; Step 2 Indicate how many of the object will be part of the array ButtonSet=new JButton[100]; Steps 1 and 2 can be done together at the top of the program as follows... JButton [ ] ButtonSet=new JButton[100]; Step 3 Create each individual object in the array of objects.
for (int u=0;u<100;u++) ButtonSet[u]=new JButton( +(u+100)); ButtonSet[u].setBackground(new Color(0+(u*2),50+u,150-u)); ButtonSet[u].addActionListener(this); Step 4 From this point forward you will likely want to process the objects as part of a counted loop for (int u=0;u<100;u++) If (e.getsource()=buttonset[u]) ButtonSet[u].setBackground(Color.BLACK); u=100;