Welcome to Computer Programming


HWJ7 - MouseMotion


Create these programs: 2 points each -do 4 of 7

  1. In FindLocation2, have 2 textfields, and if the mouse is moved around the screen the coordinates appear in each textfield. If the mouse is dragged (meaning button is down), have the buttons change background color. [textfield1.setBackground(Color.green);]
  2. Create SnowMan that has a small SnowMan follow the cursor around (draw on temporary layer Graphics g=getTempG() ). (A snowman could be just three circles on top of each other - or an image)
  3. In AnnoyTheUser have a circle that appears at a random location. When the user moves the cursor close to the circle (say within 30 pixels), the circle will move to a new random location. Fun.
    • Code for finding distance to center of circle
          public double distanceToCenter(int centerX, int centerY, int mouseX, int mouseY)
              int a=centerX-mouseX;
              int b=centerY-mouseY;
              return Math.sqrt(a*a+b*b);
      Code for drawing originally in the program
          public void paintScreen (Graphics g)
               //draw on g here
  4. In Rainbow have a rainbow of colors appear when the user moves the mouse. It will be beautiful; kind of like a rainbow.Use g.setColor(Color.blue) or any color Have a reset button.
  5. In drawDynamicRect that when the user presses down and then drags their mouse it will constantly draw (and erase) a rectangle that is that size. When they release the mouse, it will finalize that rectangle. (code will be in mousepressed, mousedragged and mousereleased) [advanced - have it work in any direction]//Code to get a random color g.setColor(findRandomColor());
  6. In Gradient that when the mouse is moved left or right, it will adjust the amount of red in the background. If it goes up or down, it will adjust the amount of green.g.setColor(new Color(r,g,b) where r,g,b are the amount of red green and blue)
  7. In ConstantLine when the mouse is pressed it will start drawing a new line and connect the dots to make the line like good. (so there is no gaps). Each time they press down, a new line will be drawn.



  1. In Operation, there will be a square inside a square. The idea of the game is that the user must move their mouse around the inside of the square without hitting the walls. It will keep track of how much time it takes for them to accomplish this goal. Put this inside paintScreen:
    You probably will want to have a global int corner; Where whenever they make it to a corner it will change which corner you need to hit next. This is hard, but oh what a fun game it would be. 
    Timer here
    That part is required. Additional ideas:
    • give them 2 seconds to get in the safe region originally.
    • put buzzer sound when they hit wall
    • flash red when they hit wall.
    • Have a timer keeping track of how long it takes
    • keep track of best time.