Hi everyone! I have a homework problem and I can't seem to get it to work.

We're supposed to make a snowman melt when you click on it with the mouse, but my snowman refuses to melt. I know it's a problem with the mousePressed method in the MeltingSnowman class because it even refuses to print out the line, so it's not a fault with the toMeltMove method.

Anyway, here it goes:

SnowmanCartoon.java

import wheels.users.*;
     
     
    public class SnowmanCartoon extends Frame{
        private MeltingSnowman snowman;
        private Ellipse sun;
     
        public SnowmanCartoon(){
          sun = new Ellipse(java.awt.Color.YELLOW);
          sun.setSize(150,150);
          sun.setLocation(520,10);
          snowman = new MeltingSnowman();
     
        }
     
        public static void main (String[] args){
          SnowmanCartoon app = new SnowmanCartoon();
        }
    }

Snowman.java

import wheels.users.*;
     
    public class Snowman{
        protected Ellipse body, head, leftEye, rightEye;
        protected int b1, b2, h1, h2;
        protected int bx, by, hx, hy;
        protected int y;
        protected Rectangle hat1, hat2;
        protected int z;
      
        public Snowman(){
          
          b1 = 180;
          b2 = 180;
          
          h1 = 100;
          h2 = 100;
          
          bx = 50;
          by = 300;
          
          hx = 90;
          hy = 200;
          
          y = 230;
          
          head = new Ellipse(java.awt.Color.WHITE);
          head.setFrameColor(java.awt.Color.BLACK);
          head.setSize(h1, h2);
          head.setLocation(hx, hy);
          
          body = new Ellipse(java.awt.Color.WHITE);
          body.setFrameColor(java.awt.Color.BLACK);
          body.setSize(b1, b2);
          body.setLocation(bx, by);
          
          leftEye = new Ellipse(java.awt.Color.BLACK);
          leftEye.setSize(10,10);
          leftEye.setLocation(120, y);
          
          rightEye = new Ellipse(java.awt.Color.BLACK);
          rightEye.setSize(10,10);
          rightEye.setLocation(150, y);
     
          
          z = 140;
        
          hat1 = new Rectangle(java.awt.Color.black);
          hat1.setSize(90,15);
          hat1.setLocation(95, z + 50);
          hat2 = new Rectangle(java.awt.Color.BLACK);
          hat2.setSize(60,60);
          hat2.setLocation(110, z);
        
        }
      
      
        
    }

MeltingSnowman.java

import wheels.users.*;
     
    public class MeltingSnowman extends Snowman{
     
      public void toMeltMove(int g, int h, int j, int k, int l, int m, int n, int p, int y, int z){
          while ((g > 0 && h > 0) || (j > 0 && k > 0)){
            try {
              g = g - 2;
              h = h - 2;
              j = j - 2;
              k = k - 2;
              body.setSize(g - 2 , h - 2);
              head.setSize(j - 2, k - 2);
              l = l + 1;
              m = m + 2;
              n = n + 1;
              p = p + 4;
              body.setLocation(l + 1, m + 2);
              head.setLocation(n + 1, p + 4);
              y = y + 2;
              leftEye.setLocation(120, y + 2);
              rightEye.setLocation(150, y + 2);
              
              z = z + 3;       
              hat1.setLocation(95, z + 50);
              hat2.setLocation(110, z);
              Thread.currentThread().sleep(250);
            }
            catch (InterruptedException e){
            y = 400;
            leftEye.setLocation(120, y);
            rightEye.setLocation(150, y);
            }
          }
          
      }
      
      public void mousePressed(java.awt.event.MouseEvent e){
      //public MeltingSnowman(){ 
        System.out.println("boo");
        this.toMeltMove(b1, b2, h1, h2, bx, by, hx, hy, y, z);
      }
      
      
    }

The files imported are from the wheels class (and subclasses), which you can find http://cs.stuy.edu/common/index.py?root=apcs&basename=code&mod=code&dir=src/ml1x/platek/wheelsDocs.

Thanks for any help! :)

None of your classes implement MouseListener, which has a couple more methods than just mousePressed(). If your MeltingSnowman class implemented MouseListener, you could add it as a mouse listener to your Frame. Or you could add an anonymous inner class that extends MouseAdapter and forward the mousePressed() event to your own mousePressed() method in the MeltingSnowman class, but your professor probably isn't looking for things like that right now.

This tutorial on writing a mouse listener might help a bit: http://java.sun.com/docs/books/tutorial/post1.0/ui/mouselistener.html

I'm trying to add a MouseListener, but I'm really kind of lost...

import java.awt.event.*;
import java.awt.*;
import wheels.users.*;

public class MeltingSnowman extends Snowman implements MouseListener{

  public MeltingSnowman() {
    addMouseListener(this);
  }

    
    
  public void mouseClicked(MouseEvent e){}
        
  public void mouseEntered(MouseEvent e) {}

  public void mouseExited(MouseEvent e) {}

  
  
  public void mousePressed(java.awt.event.MouseEvent e){
  //public MeltingSnowman(){ 
    
    System.out.println("boo");
    this.toMeltMove(b1, b2, h1, h2, bx, by, hx, hy, y, z);
  }
  
  public void mouseReleased(MouseEvent e) {}

}

I have absolutely no idea what to do with the addMouseListener... It keeps giving me an error:

cannot find symbol
symbol : method addMouseListener(MeltingSnowman)
location: class MeltingSnowman

so, that means that the method does not exist...?

I'm sure I'm not even doing this correctly. Any help would be appreciated. :)

Put that call to addMouseListener() in your frame class and pass your MeltingSnowman instance as the parameter.

This article has been dead for over six months. Start a new discussion instead.