Lab10: ChatApp Design & Simple Threads

November 6, 2012

Concepts

  1. Java GUIs run in their own single thread. RMI runs in its own single thread. Your code will run in a third thread of its own. When communicating between threads, either through data accesses or function calls, you need to be careful.
  2. Java has provided a super-simple way to guarantee that any operation you may want to perform with GUI objects, can be executed in a thread-safe manner: SwingUtilities.invokeLater(…);
    SwingUtilities.invokeLater( new Runnable() {
    public void run() {
    // Your code here
    });
  3. Blocking Queues help the Producer-Consumer model of asynchronous processing work. Blocking Queues block the consumer, not the producer, allowing data to arrive as fast as it wants.
  4. The class then broke into groups to decide:
    1. What should the return value of a sent data packet RMI invocation be?
    2. What is the protocol for figuring out how to handle unknown commands?
  5. No decisions were reached.

Resources

  1. Lab 10 webpage: http://www.clear.rice.edu/comp310/f12/labs/lab10/
  2. More reading on Cross-Thread Invocations: http://www.clear.rice.edu/comp310/JavaResources/crossthread.html
  3. Group Design Brainstorms Wiki: https://owlspace-ccm.rice.edu/portal/tool/6207c4c3-bbc0-47d3-8161-8e88cf4286b2?pageName=%2Fsite%2FCOMP-310-F12%2Flab10&action=view&panel=Main&realm=%2Fsite%2FCOMP-310-F12

Lab09: ChatApp Design

October 30, 2012

Concepts

  1. Creately online spec design
    1. Design
    2. Code
    3. Goto step 1
  2. When done, export it as an image, and post it to the Lab09 wiki.
  3. After about 20 minutes of lecture, we broke into groups to do steps 1 and 2 above.

Resources

  1. Lab09 webpage: http://www.clear.rice.edu/comp310/f12/labs/lab09/
  2. Creately online spec design software: http://creately.com
  3. COMP310 Owlspace Wiki Home: https://owlspace-ccm.rice.edu/portal/site/COMP-310-F12/page/57ffcbae-4fb2-4126-8976-734432cce374

Lab 08: RMI Setup

October 23, 2012

Concepts

  1. Don’t run on the “Rice Visitor” network
  2. Make sure that the following ports are opened on your machines: 2001, 2002, 2099 through 2102
  3. The first 10 minutes were lecture, the rest was “practice setting up RMI with someone else in the lab.”

Resources

  1. Lab 08 webpage: http://www.clear.rice.edu/comp310/f12/labs/lab08/

Lab 07: Playing Music in Java

October 16, 2012

Concepts

  1. Set up Eclipse to pull the provided code from the course’s SVN repository.
  2. The format and meaning of ABC notation explained on the lab webpage.
  3. Traditional music notation may have modifiers (tempo, key, etc) that change the sound that a given note should produce. MIDI has no notion of these modifiers, so you must be sure to properly apply any modifiers in the ABC file to the notes before setting them up to play via MIDI.

Quotes

  1. None

Resources

  1. Lab 07 webpage: http://www.clear.rice.edu/comp310/f12/labs/lab07/
  2. Setting up Eclipse to load the provided code: http://www.clear.rice.edu/comp310/f12/assignments/hw06/setup.html

Lab 06: Debugging in Eclipse

October 2, 2012

Concepts

  1. A “Debugger” is a program that can attach itself to a JVM running your Java program in order to let you pause the execution of your program at specific points in your source code called “break points.” Once your program is paused, you can do many useful things. The two most useful things a Debugger lets you do are:
    1. See the value that variables actually have at run-time.
    2. “Step through” the execution of your code one line at a time.
  2. Together, these two features of Debuggers make it possible to see what your program is actually doing when it is running, which can be very useful in tracking down certain bugs.
  3. Debugging in Eclipse is very easy: Click the “Bug” icon Eclipse Debug Button to run your program with a debugger attached.
  4. Not very much interesting will happen unless you have set up some breakpoints. To add a breakpoint, double-click to the left of any line of code:
    No Breakpoint

    No Breakpoint

    Double-click on line 142…

    Breakpoint Added

    Breakpoint Added

    Now, the Debugger will pause the program right before line 142 runs.

  5. When the Debugger pauses a program at a breakpoint, Eclipse will open the “Debug perspective,” and provide some controls to you
    1.  “Step Over” will move to the next function call or line of code in the file that contained the breakpoint. In the example above, the FIRST “step over” will run until _default_strategy.make_strategy(). The SECOND “step over” will run all of the code in _default_strategy.make_strategy(), and pause still on line 142, ready to run new MotionSwitchStrategy(…). The THIRD “step over” will finish line 142’s new MotionSwitchStrategy, and pause at line 145. “Step Over” is the way to get to the line that you really care about.
    2.  “Step Into” will run the very next line of code, wherever it may be. In the example above, the FIRST “Step Into” will also run until it finds _default_strategy.make_strategy(), as that is the very first thing that must happen on line 142. The SECOND “step into” will go to the file that defines whatever type of object _default_strategy is – in this case, it is an anonymous inner class created in the Controller, so we’ll be sitting at line 1 in the controller. The THIRD “step into” will go to the first line of the make_strategy() method. You could then use “step over” to walk through the lines of code in make_strategy(). “Step Into” is a good way to dig into the inner workings of the line of code you care about.
    3.  “Step Return” will run through lines of code in whichever method you are currently in, until a “return” line is found. It will complete the return, and then pause again. In the example above, clicking “Step Return” anywhere inside the make_strategy() method will bring us back to line 143, where make_strategy() has just finished completing. “Step Return” is the quick way to get yourself out of a hole you dug yourself into with “Step Into.”
  6. When debugging, you can help yourself understand what is going on by only having one method call per line of code. Instead of the example code above, the following would be much easier to debug:

    Easier to Debug

    Easier to Debug

Resources

  1. Debugging in Eclipse: http://www.clear.rice.edu/comp310/Eclipse/debugging.html
  2. Screenshot of Eclipse’s “Debug Perspective:” http://www.clear.rice.edu/comp310/Eclipse/debug_screen_annotated.png

Lab 05

September 25, 2012

Concepts

Today, Dr. Wong only lectured for about 8 minutes. He encourages us to work on command dispatchers and on creating an infectious strategy.

Resources

  1. Lab 05: http://www.clear.rice.edu/comp310/f12/labs/lab05/

Lab 04: Transforming Shapes

September 18, 2012

This lab is really meant to assist with HW04, but due to scheduling, we’re doing it now. Dr. Wong suggests that you branch your HW02 or HW03 (depending on which is more complete) into Lab 04.

Concepts

  1. In your MVC, the line “import  model…” and “import view…” should only ever appear in the Controller. The line “import controller…” should not appear anywhere in your Model or View.
  2. Basically, we want to turn all shapes into unit-sized entities centered on the origin, as this will most easily permit us to use affine transformations in drawing. For a circle, “unit size” seems pretty clear: unit radius or maybe unit diameter. For a strange shape, however (Dr. Wong draws something that looks like a squashed bug), “unit size” is less  defined. Maybe it is the circle that most tightly encloses it? Maybe it is a circle whose radius is some average of the distances between the points around the perimeter and the geometric center? etc.
  3. It’s important to center your objects around the origin before you start transforming. Otherwise, transformations can easily affect them in drastic and undesired ways.
  4. Dr. Wong suggests trying to load and draw an image as part of Lab 04. Images can be tougher, as images’ origins are in their upper left-hand corners. Dr. Wong suggests using paintCfg(…) to reposition the image somehow to put it at the origin. Keep in mind that you’ll need to maintain the aspect ratio of non-square images when transforming them to unit size. Also, if your image contains a large whitespace border around the content, you may need to use test collision with a shape that is somewhat to substantially smaller than the rectangle of the image. Also, make sure the content of your images is centered within the image :D.
  5. Some of your shapes (including images) will have a non-identity “initial transformation” that will bring them to unit-size at the origin. Then, you can layer other transformations on top of this initial one to draw it wherever you need it. You should do this because you really don’t want to alter image data to bring it to an initial state: scaling images would lose resolution, etc. Instead, just composite multiple transformations to achieve your goal.
  6. Note that all of these transformations are reapplied during every draw; you never permanently alter your images or other shapes, but instead just set up the Graphics2D correctly each frame to draw through the transformations.
  7. If Dr. Wong or another staff member has not yet looked at your MVC design, he urges you to have it looked at to avoid losing points on HW03 and beyond.
After a 25 minute lecture, it looks like we’re now going to have work time for the rest of the lab.

Quotes

  1. “It’s like a never-ending battle, and I’m always losing.” — Dr. Wong on trying to adjust his class schedule year after year.

Resources

  1. Lab 04: http://www.clear.rice.edu/comp310/f12/labs/lab04/

Lab 03: MVC Implementation

September 11, 2012

Dr. Wong began the lab by asking who had already implemented MVC in their HW02 solution. Most people responded that they had. It seems this lab will focus on MVC and miscellaneous tips regarding HW03.

Concepts

  1. There are two approaches to implementing adapters in the context of MVC. Dr. Wong reminds us that, in the end, it all comes down to decoupling the view from the model.
    1. Every single event that the View can generate (each individual button press, etc.), gets its own individual adapter to connect it properly to the model.
    2. There is a single IAdapter interface, with as many method specifications as are necessary to capture all of the events generated by the View.
    3. Dr. Wong advocates a mixed approach, wherein semantically related functionality is grouped into a single adapter, and a Controller may instantiate and distribute more than one kind of these adapters. For instance, adding a ball and clearing all balls are somewhat similar in that they both manipulate the set of balls. Therefore, an IBallAdapter interface could specify the addBall and removeBalls methods. On the other hand, methods for updating and drawing the balls (updateBalls, paintBalls) based on a Timer tick may reside in an IUpdateAdapter.
  2. To get started on HW03, Dr. Wong recommends to hold off on fancy painting (He seems to mean painting that somehow varies with time (something that changes shape or rotates / etc.)). We’ll get to that later. Also, until you have your Balls correctly using various Strategies, it is recommended to just hard-wire your dynamic class loader to skip the whole process of dynamic class loading, and instead to simply return an instance of some concrete Strategy.
  3. Many HW02 submissions were still called HW01 in Eclipse. This is because SVN branching will only affect the SVN folder names, whereas Eclipse knows the project name based on metadata stored in the “.project” file. Be sure to rename the project inside of Eclipse after branching; otherwise Eclipse will overwrite the copy of HW01 that is on your computer.
After 25 minutes of lecture, it appears that we’re now just going to have independent work time for the rest of the lab.

Quotes

  1. Never delete something before its replacement works. In electrical engineering, they call that ‘make before break’” – Dr. Wong.

Resources

  1. Lab 3: http://www.clear.rice.edu/comp310/f12/labs/lab03/
  2. COMP-310 endorsed overview of MVC: http://cnx.org/content/m26104/latest/
  3. MVC Explained (in more detail, with picture): http://comp310.blogs.rice.edu/2012/09/11/mvc-explained/

Lab 02: Animation

September 4, 2012

Concepts

  1. Don’t ever call paintComponent or new Graphics(…); because they are provided for you by Java’s graphics framework, which is exists before your code starts running and whose purpose is to make it easier for us to do graphics.
  2. Model-View-Cotroller[2]Design Pattern:
    1. The “Model” knows what your program is, and what its state is.
    2. The “View” knows how to display objects.
    3. The “Controller” knows how to tell a View to display a Model, and how to update the Model based on user input.
    4. The Model and the View never interact with each other; they only talk to the Controller.
    5. Use the MVC design pattern for Homework 2 even though it’s not required, because it will be required for Homework 3.
    6. WindowBuilder’s MVC will start the View instead of the Controller, which is wrong. Change the generated code to start the Controller instead.
  3. Balls (in Homework 2) move discretely, not continuously.
    1. If a ball passes the edge of your screen and bounces back and forth on the edge, it’s because you are making it bounce every time it is across a wall, rather than once per wall.
    2. How do you know where a ball would have been, if it discretely passes the edge? http://www.clear.rice.edu/comp310/f12/assignments/hw02/BallBounce.png
  4. Dr. Wong says “SVN Branch your Homework 1 code to use as the starting point for Homework 2.”
    1. Consider instead:
      1. Duplicate HW01 in Eclipse.
      2. Rename HW01 to HW02 in eclipse.
      3. Commit HW02 to SVN.

Quotes

  1. “Assume a spherical cow (in a vaccum).” – Dr. Wong (and peanut gallery)
  2. “I had an older brother, and to him, I provided the ‘punchingBag’ service. I didn’t know when he would call on that service; he could just walk up whenever he wanted and OOMPH” – Dr. Wong

Resources

  1. Animations: http://www.clear.rice.edu/comp310/JavaResources/animation.html
  2. MVC Design Pattern: http://cnx.org/content/m26104/latest/
  3. Copying a Source-Controlled Project: http://www.clear.rice.edu/comp310/Eclipse/Subclipse/copying/