Question

I have an Avatar class which displays a person made out of shapes (ellipses, rectangles), with two subclasses, which are special Avatars (such as NinjaAvatar) and a Button class which extends a TextBox. I have to set it up so that the Button class constructor takes an Avatar as a parameter. Also I have another class which displays all three of my Avatars on one frame, and each has a "button" underneath, when the button is clicked it should make the Avatar do something, on top of the Avatar already doing something when it, itself is clicked.

This is my class that brings all Avatars to one frame

import wheelsunh.users.*;
import java.awt.Color;
import java.awt.event.MouseEvent;

/** 
* Creates and displays three avatars in three different locations
* and three different colors.
* 
* @author Devin Gero
* Avatar
*/
public class Program5
{
private Button  _basic, _smoking, _ninja;
public Avatar _a1;
public SmokingAvatar _s1;
public NinjaAvatar _n1;



public Program5 ( )
{

 Avatar _a1 = new Avatar( 100, 225 );
 SmokingAvatar _s1 = new SmokingAvatar( 300, 225 );
 NinjaAvatar _n1 = new NinjaAvatar( 500, 225 );

    Button _basic = new Button ( 200, 300, _a1 );
    _basic.setText( " This Avatar is basic. " );
    _basic.setColor( _a1.getColor( ) );
    _basic.setLocation( _a1.getXLocation( ) - 70, _a1.getYLocation( ) + 130 );

    Button _smoking = new Button ( 200, 300, _s1 );
    _smoking.setText( " This Avatar is Smoking. " );
    _smoking.setColor( _s1.getColor( ) );
    _smoking.setLocation( _s1.getXLocation( ) - 60, _s1.getYLocation( ) + 130 );

    Button _ninja = new Button ( 200, 300, _n1 );
    _ninja.setText( " This Avatar is a ninja! " );
    _ninja.setColor( _n1.getColor( ) );
    _ninja.setLocation( _n1.getXLocation( ) - 50, _n1.getYLocation( ) + 130 );

}   
public static void main( String[] args )
{
    Frame f = new Frame();
   Program5 a = new Program5 ( );


}

}

Now, When I click on each one of the three buttons it does what I want them to do, it changes the Avatars shirt colors to cyan, and then back to yellow. But the problem is, I need to have it so that each button, when "activated" will make it's Avatar do something different. the first Avatar I only want it's shirt to change to blue when the button is clicked, which it does, but, say, the NinjaAvatars button, I want it to change the eyes to red, and the sword to gold, or anything. Just something different from what the basic Avatars button does.

Here is my Button Class

import wheelsunh.users.*;
import java.awt.Color;
import java.awt.event.MouseEvent;

public class Button extends TextBox
{  
Color _curr;

public Avatar _bs, _nj;
public SmokingAvatar _sm;

public Button ( int x, int y, Avatar _a1 )
{   
    setSize( x, y );

     _bs =  _a1;

    _curr = _a1.getColor();
    this.setColor( _curr );

}

public Button ( int x, int y, SmokingAvatar _s1 )
{   
    super( );

     _sm =  _s1;

    _curr = _s1.getColor();
    this.setColor( _curr );

}

public void mousePressed( MouseEvent e )
{

   activate( e );   
}

public void mouseReleased( MouseEvent e)
{
    deactivate( e );
}

public void activate( MouseEvent e )
{
 _bs.setColor( Color.CYAN );
 _sm.setColor( Color.BLACK );

}



public void deactivate( MouseEvent e )
{
_bs.setColor( Color.YELLOW );

}

}
Was it helpful?

Solution

You can move that changing logic (whatever you want to do on button activation) into the Avatar class. declare a method called doChange() in the Avatar and override that method in the subclasses (NinjaAvatar/SmokingAvatar). In the activate method of button class, just call this doChange method on the Avatar instance passed to button constructor. You can follow the same stratedy for deactivation as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top