Java Program: Picture

Description:

You are to provide a definition for a class called PicturePanel that animates a particular kind of picture. Your class should be a subclass of JPanel. At any given time, your panel will be displaying a picture that looks something like this:

The instructor has written the GUI code for the frame, the slider and so on. Your class should have a constructor that takes as parameters the string to display and the number of increments to use in the animation (more on the animation in a moment). The instructor ’s GUI code passes the string displayed above, but your code should work for any string.

Criteria:

  • You are to write the code that draws the actual picture. The general picture parameters are as follows:
    • Set the background color of your picture to cyan.
    • Your picture should draw the string so that it fills up 90% of the horizontal width of the panel. You won’t be able to get exactly 90% for a number of reasons, including the fact that font sizes are specified using integers.
    • You should always use a bold serif font.
    • You should use vertical space to separate parts of this figure.
      • You should use 10 points for vertical spacing.
      • Draw the top line 10 points below the top of the panel.
      • Draw the string directly below the top line (no extra vertical space other than the height of the font).
      • Draw the lower line 10 points below the baseline of the text.
      • That means that the text takes up 20 points more than the font size of the text.
      • Some parts of the text may be drawn below the lower line because they appear below the baseline of the text.
    • Below the text you should draw a red circle with a vertical and horizontal chord drawn in black.
      • You should draw a circle whose diameter is half of the overall width.
      • You should center the circle vertically in the space below the text area
  • Your panel must animate this picture by drawing slightly different versions as it is repainted.
    • As noted above, your constructor will be told the total number of animation increments to support (in the sample program, 100).
    • Think of the panel as being divided into this number of columns. Each time the paintComponent method is called, the figures shift to the next column (the text shifting left and the ball shifting right).
  • You should in general try to draw the string so that it would fill up 90% of the horizontal width of the panel, but that might not always be appropriate.
    • You will approximate this scaling by keeping track of a current font and potentially resetting the font each time the panel is painted. Start with a font size of 10.
    • Then each time your paintComponent method is called, you should compute the width of the string in the current font. Use this width relative to the panel width to compute a new font size that would scale the text to 90%.
      • For example, suppose that the current font size is 12. If the text in that font size would fill up 45% of the overall width, then your best guess is to switch to a 24-point font to scale the text to 90%. If the text had instead been filling up 30% of the overall width, then you’d switch to a 36-point font.
      • Be careful not to let the integers give you choppy scaling. For example, if the text fills 66% of the width, scale the font to 16 points (i.e., the font might change by a relatively small amount, not just increments of the font size).
      • You can round if you want to, but it’s also okay to simply truncate the result to the nearest integer as long as you aren’t getting choppy scaling. Note that this scaling can reduce the font size. For example, if you find that the text would fill 180% of the overall width, you’d switch to a 6-point font.
      • You should never use less than a 6point font and you should reset the font only if the new font differs by at least 2 points from the previous one.
      • There is one particular case where you won’t be able to have the font take up 90% of the width of the panel that deserves comment. Consider the case where the panel is very wide and not very tall. A font that would fill 90% of the width of the panel would be so large that you couldn’t read the letters (because they are too tall). There is a particular point where this rule kicks in. Below are specifications for the smallest possible red ball that you are allowed to draw.
      • This along with the rules about vertical spacing place a limit on how tall you can make the font. At some point this becomes the limiting factor for the font height even though it doesn’t give you text that fills 90% of the width.
  • The red ball should never be drawn smaller than a diameter of 10 points.
    • You should draw the largest circle possible but it should never be taller than the vertical space below the text area (i.e., below the lower line) and it should never be wider than half of the overall width.
    • You should center the circle vertically in the space below the text area.
    • Your panel should sometimes decide not to draw the text area, in which case it draws only the red circle.
      • If the font size ever drops below the minimum of 6 points, you shouldn’t draw the text.
      • You should also not draw the text if there isn’t enough vertical space for the entire text area and the standard vertical space of 10 points for the red ball.

Conclusion:

While the instructor provided alot of the glue code for this program, there was two main focuses he was trying to achieve:

1) He wanted us to learn more about how to handle drawing and moving the drawings.
2) Learn to follow a detailed spec than handled both initial states as well as changing demensions of a window.

Links:

Comments: [Add a Comment]

Creative Commons License   This webpage and all of its contents are licensed under a
Creative Commons License by Dr-EagleEyes.com.