Source of ObjectClasses.java


  2: import java.util.Scanner;
  3: import java.awt.Color;
  4: import javax.swing.JButton;
  5: import javax.swing.JFrame;
  6: import javax.swing.JLabel;
  7: import java.awt.BorderLayout;

  9: /**
 10:  * Just creating some objects of various classes
 11:  *
 12:  * @author Mark Young (A00000000)
 13:  */
 14: public class ObjectClasses {

 16:     public static void main(String[] args) {
 17:         // declare some objects
 18:         String s1, s2;
 19:         Scanner kbd, str;
 20:         Rectangle r1, r2;
 21:         Color c1, c2;
 22:         JButton okButton, cancelButton;
 23:         JFrame win;

 25:         // create some objects
 26:         kbd = new Scanner(System.in);
 27:         str = new Scanner("This is the String this Scanner scans!");
 28:         r1 = new Rectangle(0.36, 2.21);
 29:         r2 = new Rectangle(0.63, 0.71);
 30:         c1 = new Color(255, 127, 0);
 31:         c2 = new Color(17, 79, 251);
 32:         okButton = new JButton("OK");
 33:         cancelButton = new JButton("Cancel");
 34:         s1 = "Strings are special!";
 35:         s2 = new String("But this works, too!");

 37:         // access some object data
 38:         System.out.printf("\"%s\".length() is %d.\n", s1, s1.length());
 39:         System.out.printf("\"%s\".charAt(0) is '%c'.\n", s1, s1.charAt(0));
 40:         System.out.printf("\"%s\".charAt(1) is '%c'.\n", s1, s1.charAt(1));
 41:         System.out.printf("\"%s\".charAt(2) is '%c'.\n", s1, s1.charAt(2));
 42:         System.out.printf("\"%s\".charAt(2) is '%c'.\n", s2, s2.charAt(2));
 43:         System.out.printf("r1 is a %4.2fx%4.2f Rectangle.\n", 
 44:                 r1.getHeight(), r1.getWidth());
 45:         System.out.printf("r2 is a %4.2fx%4.2f Rectangle.\n", 
 46:                 r2.getHeight(), r2.getWidth());
 47:         System.out.printf("r1's area is %4.2f.\n", r1.getArea());
 48:         System.out.printf("r2's area is %4.2f.\n", r2.getArea());


 51:         // NOTE:  You do not need to understand ANY of the following code.
 52:         // It's just here so I can show you the colors and buttons I created.

 54:         if (java.awt.GraphicsEnvironment.isHeadless()) {
 55:             Utilities.printParagraph(""
 56:                     + "I have some really cool window stuff to show you, "
 57:                     + "but it won't run on this computer. "
 58:                     + "You'll have to download the source code "
 59:                     + "to your computer "
 60:                     + "and run it using NetBeans (or something).");
 61:         } else {
 62:             // The Graphics environment is NOT headless,
 63:             // so it DOES support windows.  Cool!

 65:             // So let's create a window to show some of the other objects
 66:             // (Need to give title of window in the (parentheses)
 67:             win = new JFrame("This window has two buttons");
 68:             win.setSize(300, 200);  // make window big enuf to see!

 70:             // Let's make closing the window end the program
 71:             win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 73:             // Let's make the OK button DO something (colour the buttons)
 74:             okButton.addActionListener(e -> {
 75:                 // Let's change colors of the buttons
 76:                 okButton.setBackground(c1);
 77:                 cancelButton.setBackground(c2);
 78:                 cancelButton.setForeground(Color.YELLOW);
 79:             });

 81:             // Let's make the Cancel button DO something (exit the program)
 82:             cancelButton.addActionListener(event -> System.exit(0));

 84:             // put the buttons and a label into the window
 85:             // NORTH = at top of window
 86:             // SOUTH = at bottom of window
 87:             // CENTER = in middle of window
 88:             win.getContentPane().add(okButton, BorderLayout.NORTH);
 89:             win.getContentPane().add(cancelButton, BorderLayout.SOUTH);
 90:             win.getContentPane().add(new JLabel("Press OK to add colours!"),
 91:                     BorderLayout.CENTER);

 93:             // show the window we just created
 94:             win.setVisible(true);

 96:             // program won't end until user clicks the cancel button
 97:             // or closes the window
 98:         }
 99:     }

101: }