Homework 3

This homework is meant to give you practice with event-handling in Java.

  1. Modify your temperature application from homework 2 so that the user can interactively change the temperature using the mouse. Specifically add the following two behaviors to your application:

    1. If the user mouses down in the thermometer stem within 10 pixels of the top of the stem, then have the temperature gauge track the mouse. If the mouse goes above the maximum value or below the minimum value, then peg the temperature to the minimum or maximum value. The user does not have to keep the mouse within the stem while moving the gauge.
    2. If the user mouses down in the filled arrow polyline in the gauge view, then have the arrowline follow the mouse. If the mouse moves below the horizontal line, then peg the arrowline to the minimum or maximum value, whichever is most appropriate. Use the mouse coordinates returned by the MouseEvent object to determine the appropriate angle for the arrow line, and then determine the temperature from that angle. The user does not have to keep the mouse within the gauge while moving the arrow.
    The pie-arc will remain read-only. You may incorporate your controllers with the respective views. Please place the gauge view in the center region of the border layout, rather than the west region, as was done in HW2. This change will allow us to resize the window, to make sure that something reasonable happens to your gauge (either it gets re-sized appropriately or it maintains the same size, but preferably would get re-centered in the window).

    CS567 Students Only: Make the gauge be an oval arc with a height that is 1/4 the width. You can do this by drawing an arc whose height is 1/2 the width. Since only half the arc will appear, the arc that gets drawn will be only 1/4 the width of the arc. Make the arrowline always terminate exactly on the arc's chord. You can use an Arc2D object's getStartPoint or getEndPoint methods to get the point where a ray starting at the center of the arc intersects the arc's elliptical boundary.

  2. In this problem you are going to display a set of text strings, find the character at a given mouse coordinate and place a cursor before that character. You will then be able to move the cursor using keyboard commands. Your program should meet the following specifications:

    1. It should be placed in a package named textselection and the file containing main should be named selectText.java. You should have additional files for the model and the view/controller.
    2. It should take a point size and a series of strings as command line arguments. You can create a multiple word string by putting quotes around it. For example:
          java selectText 20 "Vander Zanden" "Hooty And The Blowfish" "Boo Hoo"
    3. Your application will display each string on a separate line, in a SERIF font, putting 10 pixels between each line.
    4. The window that displays the text strings should be just large enough to accommodate all the text strings with a 10 pixel border of whitespace around the entire window.
    5. Once the text strings are drawn, your code should allow the user to mousedown anywhere in the window and if one of the strings contains the (x,y) point, then draw a line between that character and the previous character. If the mouse appears within one of the lines of text, then you should use the algorithm that appears in my drawing basics notes to find the appropriate place to position the cursor. Here are some general guidelines:

      • In general, if the mouse is positioned in the left half of a character, the cursor will appear before that character and if the mouse is positioned in the right half of a character, the cursor will appear after that character.
      • If the (x,y) coordinate is within five pixels of the rightmost character in any line, then the cursor line should be drawn after the last character in the line.
      • If the (x,y) coordinate is between lines, above any of the lines, below any of the lines, to the left of any of the lines, or more than 5 pixels to the right of any of the lines, then no line should be drawn.
    6. The user should be able to click as many times as the user likes and the cursor should move each time to the new mouse position. If the mouse position is outside the text or between lines, then the cursor should disappear.
    7. Once the cursor has been positioned, the following key strokes should move the cursor in the specified ways:
      • Left Arrow: Move the cursor one position to the left. Do nothing if the cursor is already in front of the left-most character in the string.
      • Right Arrow: Move the cursor one position to the right. Do nothing if the cursor is already behind the right-most character in the string.
      • Ctrl-a: Place the cursor before the leftmost character in the string.
      • Ctrl-e: Place the cursor after the rightmost character in the string.
      • h: same action as left arrow. (CS494 students only-CS567 students will insert this character into the string per the instructions below)
      • l: same action as right arrow.(CS494 students only-CS567 students will insert this character into the string per the instructions below)

    CS567 Students Only: Implement the following additional keyboard actions:

    1. Del: Delete the previous character if you are not at the beginning on the line and move the remaining character string to the left
    2. Any printable character: Add the character to the string before the caret and move the remaining portion of the string to the right.
    3. Carriage Return: Resize the window so that it fits the new strings. To do this, first call revalidate on the JComponent/JPanel on which you're writing characters, then call pack on the JFrame.


  1. In order to make the window just big enough to accommodate the strings and the borders, you will need to have your getPreferredSize method compute the maximum width of any of the strings and then add 20 to it. Similarly you will need to compute the height of the strings, and add 20 pixels for the vertical borders and an appropriate sum for the vertical space between the lines.

  2. In order to make the window have a border of 10 pixels of whitespace, you can do one of 2 things:

    1. Learn about Java borders and create an empty 10 pixel border to go around the entire window.
    2. Start the strings at location (10,10).

  3. Do not call any of your methods either getWidth() or getHeight. These methods are defined by a JComponent and it determines the width and height of your component. If you accidentally override these methods, as I did in my initial program, you will cause your component to be sized incorrectly and you will see some of your text cut off.
  4. Look at the documentation for the KeyEvent class if you want information about key events.
  5. The special keys, like left and right arrow can be handled by the keyPressed method. The remaining keys can be handled by the keyTyped method.
  6. Look at InputEvent to see how to test for the Control key, Shift key, Alt key, etc. As an example, you can test for the control key using the isControlDown method. You will have to ensure that the other modifier keys are not pressed. For example, Ctrl-Alt-a should not move the cursor.
  7. Java returns a ^A key character when you type Ctrl-A. Unfortunately, Java does not make it easy to encode the ^A key character as a literal. For example, the character literal '^A' will produce an error message. Some text editors allow you to insert a control character by typing Ctrl-x where x is your control character but others do not. If your text editor does not insert control characters try printing out key events and then copying and pasting the control character into your program.

  8. VK_LEFT and VK_RIGHT are the key codes for the arrow keys.
  9. Remember to put repaint into your listener methods. If you do not do so, then your application will not repaint itself and you will find yourself wondering why your cursor is not moving when you click the mouse or press keys.

Example Executables

You can look at ~bvz/gui/hw/hw3/thermometer.jar and ~bvz/gui/hw/hw3/textselection.jar to get an idea of how your solutions should generally look and behave. Your solution does not have to identically match mine, so please do not ask questions about getting several pixel discrepancies between my answer and your answer.

What To Submit

For each of the three problems, jar up your source and class files into the named jar file shown below and include a manifest.txt file so that we can execute your jar file:

  1. temperature.jar
  2. textselection.jar