Java Program: Line Mania


You are to write a program that allows the user to draw a series of interconnected lines. The user will use the mouse to select specific points on the screen. You are to draw all possible lines connecting the various points. In other words, for every two points that aren’t equal to each other, you are to draw a line between those two points.


  • A main class that creates an instance of a frame; a frame class that is a subclass of JFrame that has the control buttons in a panel on top and the painting panel in the center; and a panel class that is a subclass of JPanel. All controls (buttons, mouse listener, etc) should be defined in the frame, not the panel.
  • Be sure you write your loops so that you don’t draw the same line twice.
  • In addition to the lines, you are to draw a circle of diameter 8 at each of the points.
  • Your program should also have four buttons labeled: Quit, Foreground, Background and Clear.
    • The Quit button should exit the program.
    • The Foreground and Background buttons should change the foreground and background color of the JPanel.
    • The Clear button should go back to the original state of 0 points and no lines.
  • In changing the foreground and background colors, you are to cycle through at least 8 different colors of your choice.
    • As the user clicks on one of these buttons, the color will change and cycle through a sequence of distinct colors that is at least 8 long.
    • You should use the same sequence of colors for both the foreground and background.
    • That means that as you cycle through the colors, the drawing will be invisible once during the cycle when the foreground and background colors match.
  • Your program should also display at the bottom of the panel the current number of points (initially 0, increasing by 1 each time the user selects another point, reset to 0 when the Reset button is pressed).
    • You should use an 18-point bold Serif font.
    • The text is to be centered horizontally and the baseline should be set to the bottom of the panel minus half the point-size of the font.
  • You may use “magic numbers” at certain points in your program without introducing program constants.
    • In particular, in setting the JPanel to 500 by 400 and in defining the font to be 18-point, bold Serif, you can “hardcode” the parameters.
    • But the rest of your code should be general. So if you were to go back and change the panel size or change the font definition, the rest of your program should adjust appropriately.
  • You may use whatever data structures you like, but you may not limit the maximum number of points to be selected by the user.
  • The number of points should be drawn in the foreground color and you should correctly display “1 point” versus “2 points”, “0 points”, “3 points”, etc.


This was a rather interesting project to work on. It was my first program in Java to take input from a mouse click on something other than a button. While it was by no means a complex program it did a good job of showing me how to handle mouse clicks and a bit of graphics work. If nothing else it sure is fun to play with!


Comments: [Add a Comment]

Creative Commons License   This webpage and all of its contents are licensed under a
Creative Commons License by