Resources:

Fall 2016 Notes

Outline:


Respond
Forward
08/06/2013 04:54PM

Project 1 - a strategy

Note # 2
by Nick Duchon
Note edited by Nick Duchon on 09/15/2013 04:47PM

Respond
Forward
09/10/2013 07:03AM

Project 1 -current issues

Note # 40
by Kenneth Clark

Ok, so now my project works ALMOST like its suppose to. Now I'm wondering how get my file to appear inside my GUI instead of only in my output. And inside my GUI I'm printing out the titles but not the information.  Anybody have any suggestions how to get around this?  I've attached a couple of pics to help explain my dilemma.  Also, here is snapshot of my code:

public void readFile () {
        JFileChooser jfc = new JFileChooser(".");
        int returnVal = jfc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION){
            System.out.println("You chose to open this file: " +
                    jfc.getSelectedFile().getName());
        }
        // open and read file
          try {
              Scanner scan = new Scanner(jfc.getSelectedFile()).useDelimiter(":").skip("//...");
              while (scan.hasNextLine()) {
              String line = scan.nextLine();
              if (line.length() == 0) continue;
              Scanner sLine = new Scanner (line).useDelimiter("\\s*:\\s*");
              switch (line.charAt(0)){
                  case 'p':
                  case 'P': addParty(sLine); break;
                  case 'c':
                  case 'C': addCreature (sLine); break;
                  case 't':
                  case 'T': addTreasure (sLine); break;
                  case 'a':
                  case 'A': addArtifacts (sLine); break;   
              } // end switch structure
            } // end while loop
                  } // end try
          catch(FileNotFoundException e) {
              JOptionPane.showMessageDialog(null, "File not found.");
          } // end catch
    } // end method readFile
   


    public void displayCave () {
        textArea.append ("Display Cave button pressed\n");
        textArea.setText ("" + cave);  
    } // end method displayCave
   
    public void search (String type, String target) {
        textArea.append (String.format ("Search button pressed, type: >%s<,"
                + "target: >%s<\n", type, target));
     
    } // end method search


   
    void addParty (Scanner sLine){
        cave.parties.add (new Party ("party A"));
        System.out.println(sLine.nextLine());
    }// end method addParty
   
    void addCreature (Scanner sLine) {
     cave.stuff.add(new Creature ("Creature CA"));     
     System.out.println(sLine.nextLine());
    }// end method addCreature
   
    void addTreasure (Scanner sLine) {
        cave.stuff.add (new Treasure ("trs ta"));
        System.out.println(sLine.nextLine());
    } // end method addTreasure
   
    void addArtifacts (Scanner sLine) {
        cave.stuff.add (new Artifact ("art aa"));
        cave.stuff.add (new Artifact ("art ab"));
        cave.stuff.add (new Artifact ("art ac"));
        System.out.println(sLine.nextLine());
    } // end method addArtifacts

 

class Cave {
    ArrayList <Party> parties = new ArrayList <Party> ();
    ArrayList <CaveElement> stuff = new ArrayList <CaveElement> ();
     
     String found = "Matches found: \n";
       
    @Override
    public String toString () {
        String st = "Cave.toString: \nThe Parties\n";
        for (Party p: parties)
            st += p + "\n";
        st += "\n+++++++++++\nThe unassociated stuff:\n";
        for (CaveElement e: stuff)
            st += e + "\n";  
        for (Party p: parties)
            found += p.found(st, st);
        return st;
       
    } // end toString method
} // end class Cave

 


Respond
Forward
08/06/2013 04:54PM

JTree and the multi-tree data structures - v1

Note # 1
by Nick Duchon

About Project 2 (NOT Project 1)

So here are some thoughts about the built-in structures and our project - the Sorcerer's Cave.

  1. It would be really nice to avoid any warnings, most of which come from unchecked types and can be addressed using generics. Unfortunately, there are some cases where generics won't work.

  2. I have asked you to use JTree to present the multi-tree structure (Cave <-- Party <-- Creature <-- [Artifact | Treasure].

  3. The JTree wants a TreeNode as the top level of the tree structure it will use. TreeNode is an interface, which is nice, but that means that we really need to use a concrete class that implements that interface. There are very few such classes, but DefaultMutableTreeNode is one of them. The DefaultMutableTreeNode class has lots of nice features, including presenting the tree in a variety of orders, such as pre-order and post-order.

    DefaultMutableTreeNode (DMTN just to make less typing) references (points to) an Object, but is NOT generic. It will list all its elements in the various orders, but only as an Enumeration without a generic definition. The lack of the generic specification means that the Enumeration returned by the various listing methods will necessarily result in casting warnings - which is a real annoyance.

    One nice feature of the DMTN is that this structure could, with a little modification, be used to implement the Sorcerer's Cave data structure. Except that we want to sort the children in various ways, and after a little research, it looks like this class can be sorted, because: the data structure holding the children is a protected Vector, which implements AbstractList, which in turn implements List <E>, which is all that is required to use Collections.sort (). So this is looking pretty good!

    Using this approach, we should be able to eliminate the ArrayList's of children in the Cave, Party and Creature classes, using the DMTN's to link the tree objects together.

    Also, using this approach means that we may wish to use toString in different ways, since it looks like JTree will use this method to label its tree. If you wish to use other presentations of the data in the objects in the tree, which we do when we walk the data structure as a text display, we will want a way to do that also - either using flags in toString, or another version of toString, perhaps toDataString. This will let us use the enumerations and the JTree presentation with relatively small changes in our classes, and keep the impact of those changes as local as possible.

  4. The other problem with DMTN is that Enumerations cannot be directly used in the advanced for loop (for each), which requires a data structure that implements Iterator. This is really kind of annoying.

  5. Comparing DMTN to the classes in the Collections framework. All the classes in Collections are generic, but the problem there is that all the entries in the data structure must be the same type, which is not exactly what we want in our example - different levels of the multi-tree are different classes (Party, Creature, Artifact, Treasure).

    If we create an umbrella class for the entire system, say CaveElement, then we are confronted with casting at various times, which is annoying.

    If we work the other way, make each level generic in its specific type (eg, Creature), then we don't have a single structure. This latter is the way I have suggested we work, but that makes it harder to create a list of elements that are just lying around.

    And the entire Sorcerer's Cave is not a single data structure. And, in any case, we cannot just send any of these structures to JTree directly.

 Here is some code demonstrating some of the features of JTree, DMTN, and JSplitPane - this is not perfect since the desired splitting doesn't seem to work until the JFrame is resized. I am still working on fixing that problem.

// File: JTreeExample2
// Date: Jul 19, 2013
// Author: Nicholas Duchon
// Purpose: a quick demonstration of populating and presenting a JTree
//   createDataTree creates a tree structure directly as a JTree using DefaultMutableTreeNode's as elements of the tree
//   the DMTN references Element's, which are really any Object you wish to define.
//   Display - this code shows how to display the results of various traversals also.
//   Also added JSplitPane's to present traversals in GUI's
// Notes -
//   DMTN only has one set of children at each node, so if your project is more complex, you will need to fix this somehow.

   import javax.swing.tree.DefaultMutableTreeNode;
   import javax.swing.JTree;
   import javax.swing.JScrollPane;
   import javax.swing.JFrame;
   import javax.swing.JTextArea;
   import javax.swing.JSplitPane;
   import java.awt.BorderLayout;
   import java.awt.event.ComponentAdapter;
   import java.awt.event.ComponentEvent;
   import java.util.Enumeration; // used for display, note that this is normally generic, but DMTN enumerations are NOT!

   public class JTreeExample2 {
      int paneCount = 5;
      JSplitPane jsppA;
      JSplitPane jsppB;
      JSplitPane jsppC;
      JSplitPane jsppD;
      JFrame jf;
  
      public static void main (String args []) {
         JTreeExample2 j = new JTreeExample2 ();
      } // end main
  
      @SuppressWarnings ("unchecked")
      public JTreeExample2 () {
         DefaultMutableTreeNode root = createNodes ();
         JTree tree = new JTree(root);
     
     
         JScrollPane treeView = new JScrollPane(tree);
     
         JTextArea jtaA = new JTextArea ();
         JScrollPane jspA = new JScrollPane (jtaA);
     
         JTextArea jtaB = new JTextArea ();
         JScrollPane jspB = new JScrollPane (jtaB);
     
         JTextArea jtaC = new JTextArea ();
         JScrollPane jspC = new JScrollPane (jtaC);
     
         JTextArea jtaD = new JTextArea ();
         JScrollPane jspD = new JScrollPane (jtaD);
     
         jsppA = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, treeView, jspA);
         jsppB = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, jsppA   , jspB);
         jsppC = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, jsppB   , jspC);
         jsppD = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, jsppC   , jspD);
     
         jf = new JFrame ("JTree example - ND");
         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
         jf.add (jsppD, BorderLayout.CENTER);
     
         jtaA.setText ("--- Post Order:");
         for (Enumeration <DefaultMutableTreeNode> e = root.postorderEnumeration(); e.hasMoreElements();) {
            DefaultMutableTreeNode d = e.nextElement();
            jtaA.append (String.format ("Node: %" + (1+d.getLevel()*3) + "s - %s\n", "", d.toString()));
         } // end printing tree
     
         jtaB.setText ("--- breadthFirstEnumeration Order:");
         for (Enumeration <DefaultMutableTreeNode> e = root.breadthFirstEnumeration(); e.hasMoreElements();) {
            DefaultMutableTreeNode d = e.nextElement();
            jtaB.append (String.format ("Node: %" + (1+d.getLevel()*3) + "s - %s\n", "", d.toString()));
         } // end printing tree
     
         jtaC.setText ("--- depthFirstEnumeration Order:");
         for (Enumeration <DefaultMutableTreeNode> e = root.depthFirstEnumeration(); e.hasMoreElements();) {
            DefaultMutableTreeNode d = e.nextElement();
            jtaC.append (String.format ("Node: %" + (1+d.getLevel()*3) + "s - %s\n", "", d.toString()));
         } // end printing tree
     
         jtaD.setText ("--- preorderEnumeration Order:");
         for (Enumeration <DefaultMutableTreeNode> e = root.preorderEnumeration(); e.hasMoreElements();) {
            DefaultMutableTreeNode d = e.nextElement();
            jtaD.append (String.format ("Node: %" + (1+d.getLevel()*3) + "s - %s\n", "", d.toString()));
         } // end printing tree
     
         jf.pack ();
         jf.setLocationRelativeTo (null);
         jf.setVisible (true);
     
         jf.addComponentListener (
               new ComponentAdapter () {
                  public void componentResized (ComponentEvent e) {resizeMe();}
                  public void componentShown   (ComponentEvent e) {resizeMe();}
               });
      } // end constructor
  
      void resizeMe () {
         int n = paneCount;
         jsppD.setDividerLocation (1.0 - 1.0 / n--);
         jsppC.setDividerLocation (1.0 - 1.0 / n--);
         jsppB.setDividerLocation (1.0 - 1.0 / n--);
         jsppA.setDividerLocation (1.0 - 1.0 / n--);
      } // end resizeMe method
  
      DefaultMutableTreeNode createNodes () {
         DefaultMutableTreeNode t, u, v, w, x, y, z;
     
         z = new DefaultMutableTreeNode (new Element ("zero"      ));
         z.add (t       = new DefaultMutableTreeNode (new Element ("one A"     )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoA A"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoA B"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoA C"    )));
         u.add     (w   = new DefaultMutableTreeNode (new Element ("threeAC A" )));
         u.add     (w   = new DefaultMutableTreeNode (new Element ("threeAC B" )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoA D"    )));
         z.add (t       = new DefaultMutableTreeNode (new Element ("one B"     )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoB A"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoB B"    )));
         u.add     (w   = new DefaultMutableTreeNode (new Element ("threeBB A" )));
         u.add     (w   = new DefaultMutableTreeNode (new Element ("threeBB B" )));
         w.add       (x = new DefaultMutableTreeNode (new Element ("threeBBB A")));
         w.add       (x = new DefaultMutableTreeNode (new Element ("threeBBB B")));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoB C"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoB D"    )));
         z.add (t       = new DefaultMutableTreeNode (new Element ("one C"     )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoC A"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoC B"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoC C"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoC D"    )));
         z.add (t       = new DefaultMutableTreeNode (new Element ("one D"     )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoD A"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoD B"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoD C"    )));
         t.add   (u     = new DefaultMutableTreeNode (new Element ("twoD D"    )));
         return z;
      } // end createDataTree
   } // end class JTreeExample

   class Element {
      String data;
  
      public Element (String s) {data = s;}
  
      public String toString () {
         return data;
     }
   } // end Node

 

 

Respond
Forward
08/30/2013 10:26PM

GUI's - Box and Grid Layouts

Note # 36
by Nick Duchon

Benjamin -

Actually, I think everyone will benefit from this discussion, so I am going to post this part in the Work Conference. I am not really worried about anyone just copying anyone else's GUI - they are very different from each other and copies will be really obvious.

Try this - mostly GridLayout, and use the default FlowLayout for each line.

I have highlighted most of the interesting changes I made to your code. I have yet to get any kind of warm and fuzzy for BoxLayout.

Here's what it looks like:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

public class TestCode extends JFrame {
   public static final long serialVersionUID = 11;  // ND: get rid of warning
   JTextArea textArea = new JTextArea (1, 1); // ND: small is ok since scrollpane
   JComboBox <String> typeComboBox;
   JTextField searchField;
   JTextField fileField;
      
   public TestCode(){
  
      setTitle ("GUI Test");
      setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
          
          
      JScrollPane scrollPane = new JScrollPane(textArea);
          
      JButton readButton = new JButton("Read File");
      JButton displayButton = new JButton("Display");
      JButton searchButton = new JButton("Search");
      JButton browseButton = new JButton("Browse");
          
          
      searchField = new JTextField(10);
      fileField = new JTextField(10);
          
      typeComboBox = new JComboBox <String> ();
      typeComboBox.addItem("Index");
      typeComboBox.addItem("Type");
      typeComboBox.addItem("Name");
          
          
      JPanel filePanelTop = new JPanel();
//       filePanelTop.setLayout(new BoxLayout(filePanelTop, BoxLayout.LINE_AXIS));
      filePanelTop.add(fileField);
      filePanelTop.add(browseButton);
     
      JPanel filePanelBottom = new JPanel();
//       filePanelBottom.setLayout(new BoxLayout(filePanelBottom, BoxLayout.LINE_AXIS));
      filePanelBottom.add(readButton);
      filePanelBottom.add(displayButton);

      JPanel filePanel = new JPanel();
      filePanel.setLayout (new GridLayout (0, 1)); // ND
//       filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.PAGE_AXIS));
      filePanel.setBorder(BorderFactory.createTitledBorder("Import File"));
      filePanel.add(filePanelTop);
      filePanel.add(filePanelBottom);
      filePanel.setMaximumSize(filePanel.getPreferredSize());
          
      JPanel searchPanelTop = new JPanel();
//       searchPanelTop.setLayout(new BoxLayout(searchPanelTop, BoxLayout.LINE_AXIS));
      searchPanelTop.add(searchField);
      searchPanelTop.add(typeComboBox);

      JPanel searchPanelBottom = new JPanel();
//       searchPanelBottom.setLayout(new BoxLayout(searchPanelBottom, BoxLayout.LINE_AXIS));
      searchPanelBottom.add(searchButton);

      JPanel searchPanel = new JPanel();
      searchPanel.setLayout (new GridLayout (0, 1)); // ND
//       searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.PAGE_AXIS));
      searchPanel.setBorder(BorderFactory.createTitledBorder("Search Objects"));
      searchPanel.add(searchPanelTop);
      searchPanel.add(searchPanelBottom);
      searchPanel.setMaximumSize(searchPanel.getPreferredSize());

      JPanel leftMenu = new JPanel();
   //                 leftMenu.setLayout(new BoxLayout(leftMenu, BoxLayout.PAGE_AXIS));
      JPanel leftMenuG = new JPanel ();             // ND
      leftMenuG.setLayout (new GridLayout (0, 1));  // ND
      leftMenuG.add(filePanel);                     // ND
      leftMenuG.add(searchPanel);                   // ND
      leftMenu.add (leftMenuG);                     // ND
              
   //             JPanel container = new JPanel();
   //                 container.setLayout(new BorderLayout());
   //                 container.add(scrollPane, BorderLayout.CENTER);
   //                 container.add(leftMenu, BorderLayout.WEST);
   //               
   //             add(container);        
      add(scrollPane, BorderLayout.CENTER);
      add(leftMenu, BorderLayout.WEST);
  
   //             pack();
      setSize (600,400); // ND
      setVisible(true);
          
   // Don't need this JFrame - JFileChooser opens its own frame
   //             final JFrame fileWindow = new JFrame("File");
      browseButton.addActionListener(
            new ActionListener() {
                //Handle open button action.
               public void actionPerformed(ActionEvent e) {
                  final JFileChooser fc = new JFileChooser("."); // ND: start at dot
                  int returnVal = fc.showOpenDialog(null);
               //                     int returnVal = fc.showOpenDialog(fileWindow);
                  if (returnVal == JFileChooser.APPROVE_OPTION) {
                     File file;
                     file = fc.getSelectedFile();
                        //This is where a real application would open the file.
                     fileField.setText(file.toString());  
                  }
                  else {
                     System.out.println("Open command cancelled by user.");
                  }
                  System.out.println(returnVal);
               }
            });
          
          
          
   } // end method toString
      
   public static void main(String[] args){
      TestCode run = new TestCode();
   }
} // end class Treasure

ND.


From: Benjamin L Ferry
Subject: RE: GUI problems
Sent: Fri Aug 30 2013 18:27:00 GMT-0400 (EDT)

I created a TestCode project which I use constantly to re-create my issue in a smaller scale so I can tamper with it fearlessly. This TestCode.java code I'm including is merely the GUI elemnets I've been working with. After I sent this e-mail I spent the better half of the day re-reading all the Oracle tutorials on how to use each layout manager and found a GUI guru on StackOverflow and read a bunch of their comments on other people's problems.

With all the knew knowledge, I scrapped everything I had and started over. I still have only one tiny issue that I do not know how to resolve and I want to know how to fix it. The menu items on the left are in a Panel with the BoxLayout (PAGE_AXIS) hence the two panels filePanelTop and filePanelBottom stacking on top of each other. What I would LIKE to accomplish, and have been trying to accomplish all along, is I would like to get the  "import file" and "search objects" panels or blocks to stretch evenly from the left side of the window to the textArea without scaling even larger with the window. I want the buttons to stay the size they are without growing larger and ridiculousl looking, but the panels they are in should be slightly bigger to fill the space available. As they are, now, the Title border really shows you how they arent the same size and making them bigger has the side-effectof making them stretch when the UI scales.

What I have now:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

    public class TestCode extends JFrame {
        JTextArea textArea = new JTextArea (20, 40);
        JComboBox <String> typeComboBox;
        JTextField searchField;
        JTextField fileField;
       
        public TestCode(){

            setTitle ("GUI Test");
            setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
           
           
            JScrollPane scrollPane = new JScrollPane(textArea);
           
            JButton readButton = new JButton("Read File");
            JButton displayButton = new JButton("Display");
            JButton searchButton = new JButton("Search");
            JButton browseButton = new JButton("Browse");
           
           
            searchField = new JTextField(10);
            fileField = new JTextField(10);
           
            typeComboBox = new JComboBox <String> ();
            typeComboBox.addItem("Index");
            typeComboBox.addItem("Type");
            typeComboBox.addItem("Name");
           
           
            JPanel filePanelTop = new JPanel();
                filePanelTop.setLayout(new BoxLayout(filePanelTop, BoxLayout.LINE_AXIS));
                filePanelTop.add(fileField);
                filePanelTop.add(browseButton);
            JPanel filePanelBottom = new JPanel();
                filePanelBottom.setLayout(new BoxLayout(filePanelBottom, BoxLayout.LINE_AXIS));
                filePanelBottom.add(readButton);
                filePanelBottom.add(displayButton);
            JPanel filePanel = new JPanel();
                filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.PAGE_AXIS));
                filePanel.setBorder(BorderFactory.createTitledBorder("Import File"));
                filePanel.add(filePanelTop);
                filePanel.add(filePanelBottom);
                filePanel.setMaximumSize(filePanel.getPreferredSize());
           
            JPanel searchPanelTop = new JPanel();
                searchPanelTop.setLayout(new BoxLayout(searchPanelTop, BoxLayout.LINE_AXIS));
                searchPanelTop.add(searchField);
                searchPanelTop.add(typeComboBox);
            JPanel searchPanelBottom = new JPanel();
                searchPanelBottom.setLayout(new BoxLayout(searchPanelBottom, BoxLayout.LINE_AXIS));
                searchPanelBottom.add(searchButton);
            JPanel searchPanel = new JPanel();
                searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.PAGE_AXIS));
                searchPanel.setBorder(BorderFactory.createTitledBorder("Search Objects"));
                searchPanel.add(searchPanelTop);
                searchPanel.add(searchPanelBottom);
                searchPanel.setMaximumSize(searchPanel.getPreferredSize());
            JPanel leftMenu = new JPanel();
                leftMenu.setLayout(new BoxLayout(leftMenu, BoxLayout.PAGE_AXIS));
                leftMenu.add(filePanel);
                leftMenu.add(searchPanel);
               
            JPanel container = new JPanel();
                container.setLayout(new BorderLayout());
                container.add(scrollPane, BorderLayout.CENTER);
                container.add(leftMenu, BorderLayout.WEST);
               
            add(container);         
            pack();
            setVisible(true);
           
            final JFrame fileWindow = new JFrame("File");
            browseButton.addActionListener(new ActionListener() {
                //Handle open button action.
                public void actionPerformed(ActionEvent e) {
                    final JFileChooser fc = new JFileChooser();
                    int returnVal = fc.showOpenDialog(fileWindow);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        File file;
                        file = fc.getSelectedFile();
                        //This is where a real application would open the file.
                        fileField.setText(file.toString());   
                    } else {
                        System.out.println("Open command cancelled by user.");
                    }
                    System.out.println(returnVal);
                }
            });
           
           
           
        } // end method toString
       
        public static void main(String[] args){
            TestCode run = new TestCode();
        }
    } // end class Treasure

Respond
Forward
08/06/2013 04:54PM

Layout Managers

Note # 3
by Nick Duchon

Here is a little study in some of the layout managers. You should try this program, and resize the display to see how the various layout managers work.

Respond
Forward
08/06/2013 04:54PM

Other Musings

Note # 4
by Nick Duchon

Here is a link to some other musings I have developed relating to Java programming over the years, you might find the examples helpful:

 

Respond
Forward
08/06/2013 04:54PM

Sample Projects 1, 2 and 3

Note # 5
by Nick Duchon

Here are some screen shots of previous projects - this posting may be updated as the class goes along.

 

P3 - Sorcerer's Cave

 

P2 - Sorcerer's Cave

P1 - Sorcerer's Cave

This display looks pretty nice, but the main text area is NOT OK - ie, it will not resize nicely because it is not using BorderLayout.CENTER. The vertical scroll bar has appeared, as it should.

This would probably look better with columns using a monospace font, perhaps Courier New or just Monospaced:

Respond
Forward
09/09/2013 08:33AM

Re: Sample Projects 1, 2 and 3

Note # 37
by Jason Scott Foley

For Project 2 samples, are all three of the pictures just different sorting layouts we could go with?

Respond
Forward
09/09/2013 09:32AM

Re: Sample Projects 1, 2 and 3

Note # 38
by Nick Duchon

Jason -

The actual layout you use is pretty much up to you as long as the functionality is there.

Thus, I have selected a variety of layouts from previous semesters to give you all something to think about, rather than a "do it this way" approach.

And in my mind, none of the ones I have shown are perfect, or even stupendously better than the others.

You should consider them when you are making your own layout.

ND.


Jason Scott Foley wrote:

For Project 2 samples, are all three of the pictures just different sorting layouts we could go with?

Respond
Forward
09/09/2013 09:51PM

Re: Sample Projects 1, 2 and 3

Note # 39
by Benjamin L Ferry

I love this thread. Thank you for letting us see other peoples work. That gives me a sort of guidline to follow on how awesome to try and make my next GUI. I have to say they're all better than my project 1 gui so I'll have to spend a lot of time trying to fix it up.

Respond
Forward
09/10/2013 10:41AM

Re: Sample Projects 1, 2 and 3

Note # 42
by Nick Duchon

Benjamin - 

Making the GUI look pretty is fun, but make sure the real goals of your project are working before putting all your energy into artwork.

ND.


Benjamin L Ferry wrote:

I love this thread. Thank you for letting us see other peoples work. That gives me a sort of guidline to follow on how awesome to try and make my next GUI. I have to say they're all better than my project 1 gui so I'll have to spend a lot of time trying to fix it up.

Respond
Forward
08/06/2013 04:54PM

Sample Sorcerers Cave Project 4

Note # 6
by Nick Duchon

Here are two samples of Project 4.


Sample 1 for Project 4:

Note that in this version, people seem to be doing more than one thing at a time - this is really not ok, but the rest of the display looks nice.

 


Sample 2 for Project 4:

Here all objects are in a single resource pool, which is really not right either, each party should have its own resource pool, but again, the output is getting there.

 

Respond
Forward
08/06/2013 04:54PM

Variables and methods

Note # 7
by Nick Duchon

I have attached a MS PowerPoint presentation in the ppt format to give you some idea of how I picture objects, references, instance and class/static variables.

You should be able to view the attachment nicely using Internet Explorer. This file is about 70 KB.

Let me know if you find this helpful.

Respond
Forward
08/06/2013 04:54PM

Developing a program step by gruesome step.

Note # 8
by Nick Duchon

Here's the link. The problem is very small, but the approach can be used in large projects.

 

Respond
Forward
08/06/2013 04:54PM

UML Tools

Note # 9
by Nick Duchon
Note edited by Nick Duchon on 08/24/2013 06:04PM

If you find a good UML tool (free is best), let us know and I'll post a link to it.

ND.

Respond
Forward
08/18/2013 07:51PM

Re: UML Tools

Note # 24
by James Albert Watson

I like StarUML

http://staruml.sourceforge.net/en/

Respond
Forward
08/24/2013 12:25PM

Re: UML Tools

Note # 26
by Aaron Christopher Hanson

I haven't used a lot of UML editors before, in my previous classes I just used Adobe or Powerpoint. For our week 1 conference post I used the Violet UML Editor and like it a lot. So much easier than trying to do everything by hand.

 


Nick Duchon wrote:

If you find a good UML tool (free is best), let us know and I'll post a link to it.

ND.

Respond
Forward
08/25/2013 11:44AM

Re: UML Tools

Note # 30
by Jason Scott Foley
Note edited by Jason Scott Foley on 08/25/2013 11:44AM

 Aaron i agree, was very user friendly and gave the UML a clean look.

Respond
Forward
08/06/2013 04:54PM

Basic applet and application

Note # 10
by Nick Duchon

Here's an example of a pretty simple applet with a few buttons and text fields with a main method that will create a JFrame to hold it, or it can be run using a web page - with comments in the code explaining how that would work. Note that main creates TWO frames and two applets, just to show how to do it.

// File: BasicApplet.java
// Author: Nicholas Duchon
// Date: March 22, 2007

// To run as a web page, see the javadoc comments

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
The following is a simple web page that will run this program, 
remove the spaces after each < to use this page:
< html> < body>
< center>
< h1>The BasicApplet by Nicholas Duchon< /h1>
< applet code=BasicApplet.java width=300 height=200>
< /applet>
< /center>
< /body> < /html>
    < ul>
      < li>
        < a href="BasicApplet.java">The source code.
      < /li>
      < li>
        < a href="runBasicApplet.html">Run the BasicApplet applet.
      < /li>
      < li>
        < a href="PrintfExample.java">PrintfExample.java source code.
      < /li>
      < li>
        < a href="ScannerExample.java">ScannerExample.java source code.
      < /li>
    < /ul>
*/

public class      BasicApplet 
       extends    JApplet 
       implements ActionListener 
       {
  
  JLabel jhb = new JLabel ("Press buttons", JLabel.CENTER);
  JLabel jht = new JLabel ("Hit return for text fields", JLabel.CENTER);
  
  JButton jb1 = new JButton ("one");
  JButton jb2 = new JButton ("two");
  JButton jb3 = new JButton ("three");
  JButton jb4 = new JButton ("four");
  JButton jb5 = new JButton ("five");
  JButton jb6 = new JButton ("six");
  JTextField jt1 = new JTextField ("one");
  JTextField jt2 = new JTextField ("one");
  JTextField jt3 = new JTextField ("one");
  JTextField jt4 = new JTextField ("one");
  JTextField jt5 = new JTextField ("one");
  JTextField jt6 = new JTextField ("one");
  JLabel jL1 = new JLabel ("Input echo:", JLabel.RIGHT);
  JTextField jtz = new JTextField ();
  
  
  public void init () {
    Container c = getContentPane ();
    c.setLayout (new GridLayout (0, 2));
    
    c.add (jhb);
    c.add (jht);
    
    c.add (jb1);
    c.add (jt1);
    c.add (jb2);
    c.add (jt2);
    c.add (jb3);
    c.add (jt3);
    c.add (jb4);
    c.add (jt4);
    c.add (jb5);
    c.add (jt5);
    c.add (jb6);
    c.add (jt6);
    c.add (jL1);
    c.add (jtz);
    
    jb1.addActionListener (this);
    jb2.addActionListener (this);
    jb3.addActionListener (this);
    jb4.addActionListener (this);
    jb5.addActionListener (this);
    jb6.addActionListener (this);
    
    jt1.addActionListener (this);
    jt2.addActionListener (this);
    jt3.addActionListener (this);
    jt4.addActionListener (this);
    jt5.addActionListener (this);
    jt6.addActionListener (this);
  } // end method init
  
  public void actionPerformed (ActionEvent e) {
    Object s = e.getSource ();
    if (s == jb1) jtz.setText ("Button one");
    if (s == jb2) jtz.setText ("Button two");
    if (s == jb3) jtz.setText ("Button three");
    if (s == jb4) jtz.setText ("Button four");
    if (s == jb5) jtz.setText ("Button five");
    if (s == jb6) jtz.setText ("Button six");
    
    if (s == jt1) jtz.setText (jt1.getText());
    if (s == jt2) jtz.setText (jt2.getText());
    if (s == jt3) jtz.setText (jt3.getText());
    if (s == jt4) jtz.setText (jt4.getText());
    if (s == jt5) jtz.setText (jt5.getText());
    if (s == jt6) jtz.setText (jt6.getText());
  } // end method ActionPerformed
  
  public static void main (String args []) {
         String title = "BasicApplet by Nick Duchon";
         JFrame hostFrame = new JFrame ();  // create a display window
         hostFrame.setTitle (title);        // sets display window's title
         hostFrame.setSize (300,200);       // set the display window size, MUST set this value
         BasicApplet a = new BasicApplet ();          // instantiate the applet
         hostFrame.add(a);                  // add applet to window frame
         a.init();                          // init applet
         a.start();                         // start applet
      
         String title2 = "BasicApplet - Number 2"; // title of second window
         JFrame hostFrame2 = new JFrame ();  // create a second display window
         hostFrame2.setTitle (title2);        // sets display window's title
         hostFrame2.setSize (300,200);       // set the display window size, MUST set this value
         BasicApplet a2 = new BasicApplet ();          // instantiate the applet a second time
         hostFrame2.add(a2);                  // add the second applet to the second window frame
         a2.init();                          // init applet number 2
         a2.start();                         // start applet number 2
         hostFrame2.setVisible (true);       // show the second applet
      
         hostFrame.setVisible (true);       // show first applet, default: window hidden
                                            // next line exits on x button of window
         hostFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

// The OLD style, EXIT_ON_CLOSE since JDK 1.3
//          hostFrame.addWindowListener(       // make sure application quits on x button
//                 new WindowAdapter() {
//                    public void windowClosing (WindowEvent e) {
//                      System.exit (0);
//                   } // end window closing event handler
//                } // end window adapter
//             ); // end window listenter

  } // end call with main, also.
  
} // end class BasicApplet
Respond
Forward
08/06/2013 04:54PM

Development sequence - JApplet to Interface - MyIF

Note # 11
by Nick Duchon

Comments to come - here's the development sequence leading to the large interface example. That attachment contains a number of stages of the development of this project.

// File: MyIFa
// Author: Nicholas Duchon
// Date: Feb 19, 2006
// Part a of the interface demonstrations

import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class MyIFa extends JApplet implements ActionListener {
private JButton jb = new JButton ("Button: 0");
private int count = 0;

public void init () {
setLayout (new FlowLayout ());
add (jb);
jb.addActionListener (this);
} // end method init

public void actionPerformed (ActionEvent e) {
count++;
jb.setText ("button: " + count);
} // end actionPerformed - required by interface
} // end class MyIFa
Respond
Forward
08/06/2013 04:54PM

JTextArea simple example with scrolling

Note # 12
by Nick Duchon

This example produces two JFrames, each with four buttons in a panel on the bottom and a scrolling JTextArea in the center of the frame.

Notice the use of the JTextArea as an INSTANCE variable.

Since putStuff is an instance method, and jta (the JTextArea) is an instance variable, the method can access jta directly, and the method accesses the jta of the particular instance - which you can see is specified in the use of the variables jb and jf in main.

ND.

// File: TextAreaExample.java// Author: Nicholas Duchon
// Date: Oct 28, 2010
// Purpose: A simple example showing how to use 
//    JTextArea in the CENTER of a BorderLayout
//    such as the default in a JFrame.
// > Also, add a panel of buttons to the bottom
// > This creates TWO JFrames, one on top of the other
//       each with its own text area

import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;

public class TextAreaExample extends JFrame {
  static final long serialVersionUID = 25;
  static Random rn = new Random ();
  
  JTextArea jta = new JTextArea ();  

  public TextAreaExample (String t) {
    setTitle (t);
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo (null);
    setSize (300, 300);
    setVisible (true);

    // put scroll bars around the text area
    JScrollPane sp = new JScrollPane (jta);
    add (sp, BorderLayout.CENTER); 

    // buttons on the bottom
    JPanel pb = new JPanel (); // FlowLayout
    pb.add (new JButton ("one"));
    pb.add (new JButton ("two"));
    pb.add (new JButton ("three"));
    pb.add (new JButton ("four"));
    add (pb, BorderLayout.PAGE_END);
  } // end constructor
  
  public void putStuff () {
    StringBuffer sb = new StringBuffer ();
    for (int i = 1; i <= 3000; i++)
      sb.append ((i%100 == 0)?'\n':(char)(rn.nextInt(26) + 'a'));
    jta.append (sb.toString ());
  } // end putStuff
  
  public static void main (String args []) {
    TextAreaExample jf = new TextAreaExample ("First");
    jf.putStuff ();
    
    TextAreaExample jb = new TextAreaExample ("Second");
    jb.putStuff ();
  } // end main
} // end class TextAreaExample
Respond
Forward
08/06/2013 04:54PM

Drawing panel with scrolling

Note # 13
by Nick Duchon

Here's a (relatively) simple way to make a drawing scrollable. 

// File: cmsc330project1.java
// Author: Ted Malley - modifications by Nicholas Duchon
// Date: February 4, 2011
// Purpose: A simple GUI that shows various train tracks
// and trains, as well as switches, denoted by colored
// dots.
// > Also, add a panel of buttons to the bottom

import javax.swing.*;
import java.awt.*;

class Project1 {
  public static void main(String[] args) {
    ProjectWindow window = new ProjectWindow();
    window.setVisible(true);
  } // end main
} // end class Project1

class ProjectWindow extends JFrame {
  public ProjectWindow() {
    //Window characteristics
    setTitle("one");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null); //centers the window
    setSize(300, 300);

    //Adds buttons
    JPanel pb = new JPanel ();
    pb.add(new JButton("one"));
    pb.add(new JButton("two"));
    pb.add(new JButton("three"));
    pb.add(new JButton("four"));
    add(pb, BorderLayout.PAGE_END);
      
    //Adds train tracks          
    Tracks a = new Tracks(100, 200, 200, 200);
    Tracks b = new Tracks(100, 100, 200, 100);

    // ND: put track and other stuff on graphics panel
    //  see explanation below
    GraphicsPanel gp = new GraphicsPanel ();
    add (gp, BorderLayout.CENTER);
    gp.add (a);
    gp.add (b);
  } // end ProjectWindow method
} // end class ProjectWindow

// The following class is needed so the JScrollPane has a component to 
// paint upon when scroll bar updates happen. Otherwise, the scroll panel will
// not update the drawing area. Other solutions involve setting up scroll bar
// listeners - this is much easier.
class GraphicsPanel extends JPanel {
  public GraphicsPanel() {
    setPreferredSize(new Dimension (500,500)); // works with scroll bars
    setLayout(null);
    setBackground(Color.yellow);
  } // end constructor
    
  public void paintComponent (Graphics g) {
    super.paintComponent(g); // get background color to work
    for (Component x: this.getComponents()) x.paint (g); // draw children
  } // end method paintMe Graphics2D
} // end class GraphicsPanel

// class Tracks extends JPanel
class Tracks extends JComponent {
  private double x1 = 0;
  private double y1 = 0;
  private double x2 = 0;
  private double y2 = 0;

  public  Tracks(double x1, double y1, double x2, double y2) {
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
  } // end double,double,double,double constructor
    
  public void paint (Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g.setColor (Color.black);
    g.drawLine ((int)x1, (int)y2, (int)x2, (int)y2);
  } // end paint
} // end class Track
Respond
Forward
08/06/2013 04:54PM

JTable testing framework

Note # 14
by Nick Duchon

Here's some simple code to support testing various features of JTable:

import javax.swing.JTable;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.util.Scanner;

public class MyTable {
 JTable jt = null;
 
 public MyTable () {
   // Standard JFrame stuff
   JFrame jf = new JFrame ("Testing JTable");
   jf.setSize (500, 500);
   jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
   jf.setVisible (true);
   
   // The table data
   Object [][] data = {
        { 110,  912,  13,  14, "one", "two", "three"},
        { 210,  812,  13,  14, "one", "two", "three"},
        { 310,  712,  13,  14, "one", "two", "three"},
        { 410,  612,  13,  14, "one", "two", "three"},
        { 510,  512,  13,  14, "one", "two", "three"},
        { 610,  412,  13,  14, "one", "two", "three"},
        { 710,  312,  13,  14, "one", "two", "three"},
        { 810,  212,  13,  14, "one", "two", "three"},
        { 910,  112,  13,  14, "one", "two", "three"},
        { 110,  112,  13,  14, "one", "two", "three"},
        { 210,  212,  13,  14, "one", "two", "three"},
        { 310,  312,  13,  14, "one", "two", "three"},
        { 410,  412,  13,  14, "one", "two", "three"},
        { 510,  512,  13,  14, "one", "two", "three"},
        { 610,  612,  13,  14, "one", "two", "three"},
        { 710,  712,  13,  14, "one", "two", "three"},
        { 810,  812,  13,  14, "one", "two", "three"},
        { 910,  912,  13,  14, "one", "two", "three"},
        { 110,  112,  13,  14, "one", "two", "three"},
        { 210,  212,  13,  14, "one", "two", "three"},
        { 310,  312,  13,  14, "one", "two", "three"},
        { 420,  422,  23,  24, "one", "two", "three"},
        { 530,  532,  33,  34, "one", "two", "three"},
        { 640,  642,  43,  44, "one", "two", "three"}
        };
        
   // The header columns titles
   String [] names =   {"a", "b", "c", "d", "e"  , "f"  , "g"};
   
   // creating the table, with data and headers
   jt = new JTable (data, names);
   // MUST use JScrollPane, otherwise headers won't show
   JScrollPane jsp = new JScrollPane(jt);
   // CENTER is a good place to put resizable objects
   jf.add(jsp, BorderLayout.CENTER);
 } // end no-parameter constructor
 
 public void update (String s, int r, int c) {
   jt.setValueAt (s, r, c);
 } // end update
 
 public static void main (String args []) {
   MyTable t = new MyTable ();
   Scanner scan = new Scanner (System.in);
   while (menu(scan, t));
   System.out.println ("Bye");
   System.exit(0);
 } // end main
 
 public static boolean menu (Scanner sc, MyTable t) {
   System.out.print ("Enter string, row, col [q to quit]: ");
   String st = sc.next();
   if (st.equalsIgnoreCase ("q")) return false;
   t.update (st, sc.nextInt(), sc.nextInt());
   return true;
 } // end method menu
} // end class MyTable

 

Respond
Forward
08/06/2013 04:54PM

JTree class notes

Note # 15
by Nick Duchon

You can use the following classes to create a fairly simple tree. In the example I did in an earlier semester, I read in the entire data file, then passed a handle to the structure to create the tree. The example uses the following classes to create and display the tree:

The traversal of the data file should be the same as the tree structure you wish to create.

Here's an example of the sort of code I think will work nicely. In this case, we had the following classes:

The data structure in this case was an array of Furniture, so it could be sorted very nicely and we just had to step through the array in order to create the tree.

The tree items were arranged according to the following levels: Manufacturer --> Style --> Type --> Furniture item

The code has the concepts of:

When one of those changes, a new node at that level is created in the tree.

      public void addManTree (DefaultMutableTreeNode top) {
         sortOn (TYPE);
         sortOn (STYLE);
         sortOn (MAN);
         DefaultMutableTreeNode d = null, s = null, t = null;
         Manufacturer m = null;
         String style   = null;
         String type    = null;
         for (Furniture f: furs) {
            if (f.mf != m) {
               m = f.mf;
               d = new DefaultMutableTreeNode(m.name);
               top.add (d);
            } // end if new manufacturer
            if (!(f.style.equals (style))) {
               style = f.style;
               s = new DefaultMutableTreeNode(f.style);
               d.add (s);
            } // end if new style
            if (!(f.type.equals (type))) {
               type = f.type;
               t = new DefaultMutableTreeNode(f.type);
               s.add (t);
            } // end if new style
            s.add (t);
            t.add (new DefaultMutableTreeNode("Index: " + f.index + ", Price: " + f.price));
         } // end for each manufacturer
      } // end addManTree
 

ND.
 

Respond
Forward
08/06/2013 04:54PM

jconsole

Note # 16
by Nick Duchon

The JConsole Overview tab

This is in the same bin directory as "java" and "javac", typically in a directory similar to:

Here are some useful links about development tools, and jconsole:

Basically, you just start it and follow the screen prompts to connect to a running java program, which means that the simplest way to use this tool is to start your program first, then start jconsole. You should be able to start it by double-clicking on it.

If this doesn't work, we will have to ask what O/S you are using before we can give you better suggestions.

Respond
Forward
08/06/2013 04:54PM

Threads 1

Note # 17
by Nick Duchon

I have attached 3 programs developed by Dr. Kann to demonstrate basic features of threads and the Runable interface. I will eventually comment these codes, but I think there are some comments about them in the course modules.

ND.

Respond
Forward
08/06/2013 04:54PM

Threads - Turning them on and off

Note # 18
by Nick Duchon

I am putting together some web pages on threads and how to control them. See:

 

Respond
Forward
08/06/2013 04:54PM

Threads and concurrency - Bounded Buffer example

Note # 19
by Nick Duchon

The attached example is a modification of code developed by Chuck Kann for his text, Creating Components: Object Oriented, Concurrent, and Distributed Computing in Java, Auerbach Publications, 2004, pp 67-94.

It demonstrates the following:

 

Respond
Forward
08/06/2013 04:54PM

Moving on a Line, Trains

Note # 20
by Nick Duchon

See:

Respond
Forward
08/06/2013 04:54PM

Notes from CMSC 335 Fall 2001

Note # 21
by Nick Duchon

You might find these notes helpful:

Respond
Forward
08/06/2013 04:54PM

Serialization Example

Note # 22
by Nick Duchon
Here is an example of serialization. 
   > class structure
   > code
   > displayed output (to standard out)
   > hex dump of serial output file "data.ser"

The classes are:

SerialExample (serialize an entire instance of this class)
      instance variables/fields:
   +--- vax type MyClassA
   +--- vay type MyClassA
   +--- vbm type MyClassB

MyClassA extends Object, implements Serializable
          toString
    +-- MyClassB extends MyClassA (thus is automatically serializable)
          toString (uses call to super, accessing MyClassA.toString stuff first

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class SerialExample implements Serializable {
  MyClassA vax = new MyClassA ();
  MyClassA vay = new MyClassA ();
  MyClassB vbm = new MyClassB ();

  public static void main (String args []) {
    SerialExample se = new SerialExample ();
    writeMe ("data.ser", se);
    System.out.println ("---- before se:\n" + se);
    se = null;
    System.out.println ("\n---- nulled se:\n" + se);
    se = readMe ("data.ser");
    System.out.println ("\n---- readback se:\n" + se);
    
  } // end main
  
  static void writeMe (String fname, SerialExample ob) {
    FileOutputStream fos = null;
    ObjectOutputStream out = null;
    try {
      fos = new FileOutputStream (fname);
    } catch (FileNotFoundException e) {
      System.out.println ("FileOutputStream exception: " + e);
    } // end try/catch for new FileOutputStream
    
    try {
      out = new ObjectOutputStream (fos);
    } catch (IOException e) {
      System.out.println ("ObjectOutputStream exception: " + e);
    } // end try/catch new ObjectOutputStream
    

    try {    
      out.writeObject (ob);
      out.close ();
    } catch (IOException e) {
      System.out.println ("writeObject exception: " + e);
    } // end try writeObject
  } // end writeMe
  
  static SerialExample readMe (String fname) {
    FileInputStream fos = null;
    ObjectInputStream in = null;
    SerialExample ob = null;
    try {
      fos = new FileInputStream (fname);
    } catch (FileNotFoundException e) {
      System.out.println ("FileOutputStream exception: " + e);
    } // end try/catch for new FileOutputStream
    
    try {
      in = new ObjectInputStream (fos);
    } catch (IOException e) {
      System.out.println ("ObjectOutputStream exception: " + e);
    } // end try/catch new ObjectOutputStream
    

    try {    
      ob = (SerialExample) in.readObject ();
      in.close ();
    } catch (ClassNotFoundException e) {
      System.out.println ("readObject exception: " + e);
    } // end try writeObject
    catch (IOException e) {
      System.out.println ("readObject exception: " + e);
    } // end try writeObject
    return ob;
  } // end readMe
  
  public String toString () {
    return vax + "\n" + vay + "\n" + vbm;
  } // end toString
} // end class SerialExample

class MyClassA implements Serializable {
  int varA = 43223;
  double varB = 13.332;
  
  public String toString () {
    return "varA: " + varA + ", varB: " + varB;
  } // end toString
} // end MyClassA

class MyClassB extends MyClassA {
  String varC = "One interesting Day in the life";
  boolean varD = true;
  
  public String toString () {
    return super.toString () + ", varC: " + varC + ", varD: " + varD;
  } // end toString
} // end MyClassB
   
++++++++++++++++++++++++++++++++++++++++++++
Console output:

 ----jGRASP exec: java SerialExample

---- before se:
varA: 43223, varB: 13.332
varA: 43223, varB: 13.332
varA: 43223, varB: 13.332, varC: One interesting Day in the life, varD: true

---- nulled se:
null

---- readback se:
varA: 43223, varB: 13.332
varA: 43223, varB: 13.332
varA: 43223, varB: 13.332, varC: One interesting Day in the life, varD: true

 ----jGRASP: operation complete.
++++++++++++++++++++++++++++++++++++++
Data file hex dump. Unix command:
> hexdump -C data.ser

00000000 ac ed 00 05 73 72 00 0d 53 65 72 69 61 6c 45 78 |....sr..SerialEx| 00000010 61 6d 70 6c 65 df 40 3a 25 b3 b0 cf 9e 02 00 03 |ample.@:%.......| 00000020 4c 00 03 76 61 78 74 00 0a 4c 4d 79 43 6c 61 73 |L..vaxt..LMyClas| 00000030 73 41 3b 4c 00 03 76 61 79 71 00 7e 00 01 4c 00 |sA;L..vayq.~..L.| 00000040 03 76 62 6d 74 00 0a 4c 4d 79 43 6c 61 73 73 42 |.vbmt..LMyClassB| 00000050 3b 78 70 73 72 00 08 4d 79 43 6c 61 73 73 41 fc |;xpsr..MyClassA.| 00000060 f2 4a 2a 21 09 28 e9 02 00 02 49 00 04 76 61 72 |.J*!.(....I..var| 00000070 41 44 00 04 76 61 72 42 78 70 00 00 a8 d7 40 2a |AD..varBxp....@*| 00000080 a9 fb e7 6c 8b 44 73 71 00 7e 00 04 00 00 a8 d7 |...l.Dsq.~......| 00000090 40 2a a9 fb e7 6c 8b 44 73 72 00 08 4d 79 43 6c |@*...l.Dsr..MyCl| 000000a0 61 73 73 42 76 a8 69 ab 4d 94 68 a2 02 00 02 5a |assBv.i.M.h....Z| 000000b0 00 04 76 61 72 44 4c 00 04 76 61 72 43 74 00 12 |..varDL..varCt..| 000000c0 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e |Ljava/lang/Strin| 000000d0 67 3b 78 71 00 7e 00 04 00 00 a8 d7 40 2a a9 fb |g;xq.~......@*..| 000000e0 e7 6c 8b 44 01 74 00 1f 4f 6e 65 20 69 6e 74 65 |.l.D.t..One inte| 000000f0 72 65 73 74 69 6e 67 20 44 61 79 20 69 6e 20 74 |resting Day in t| 00000100 68 65 20 6c 69 66 65 |he life| ++++++++++++++++++++++++++++++++++++++

 

Respond
Forward
08/06/2013 04:54PM

Liang - notes on text

Note # 23
by Nick Duchon

I have attached the web chapters as a zip file to this post. The base files are doc files.

ND.

Respond
Forward
08/21/2013 03:01PM

javafx

Note # 25
by Nick Duchon

I use jGrasp, and I needed to add the javafx library manually to the classpath to get my project to compile

Looks like the path is:

> jdk (wherever that is) --> Contents --> Home --> jre --> lib --> jfxrt.jar

ND.