Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package magnetPackage;
- import objectdraw.*;
- import java.awt.*;
- // Skeletal definition of class of objects used to represent bar magnets
- //
- // Please complete the implementation of these methods and add the others
- // described in the lab handout.
- public class Magnet {
- private static final double MAGNET_WIDTH = 150; // dimensions of magnets
- private static final double MAGNET_HEIGHT = 50;
- private static final double POLE_DISTANCE = MAGNET_HEIGHT/2;
- private Pole northPole; // North pole
- private Pole southPole; // South pole
- private FramedRect box; // Box representing perimeter of magnet
- public Magnet ( Location point, DrawingCanvas canvas)
- {
- northPole = new Pole ( this, point.getX() + POLE_DISTANCE, point.getY() + POLE_DISTANCE, "N", canvas);
- box = new FramedRect (point.getX(),point.getY(),150,50,canvas);
- southPole = new Pole (this, point.getX() + (MAGNET_WIDTH-POLE_DISTANCE), point.getY() + POLE_DISTANCE, "S", canvas);
- }
- // accessor methods
- // returns the upper-left coordinates of the magnet
- public Location getLocation()
- {
- return box.getLocation();
- }
- public Pole getNorth ()
- {
- return northPole;
- }
- public Pole getSouth ()
- {
- return southPole;
- }
- // This should return true if the given point is within the magnet. The current
- // implementation does not do this!!! You must change the body of this method so
- // that it has the proper functionality!!!
- public boolean contains( Location point ) {
- if (box.contains(point))
- return true;
- return false; // REPLACE THIS LINE OF CODE WITH THE CORRECT IMPLEMENTATION
- }
- // returns the width of the magnet
- public double getWidth() {
- return MAGNET_WIDTH;
- }
- // returns the height of the magnet
- public double getHeight() {
- return MAGNET_HEIGHT;
- }
- public void move(double xoff, double yoff)
- {
- box.move(xoff, yoff);
- northPole.move(xoff, yoff);
- southPole.move(xoff, yoff);
- }
- // mutator methods
- public void moveTo( Location point) {
- double dx, dy;
- dx = point.getX() - box.getX();
- dy = point.getY() - box.getY();
- box.moveTo(point);
- northPole.move(dx,dy);
- southPole.move(dx,dy);
- }
- public void flip ()
- {
- double x1,x2;
- x1 = MAGNET_WIDTH-POLE_DISTANCE;
- x2 = -MAGNET_WIDTH+POLE_DISTANCE;
- northPole.move(x1,0);
- southPole.move(x2,0);
- }
- /* public void interact ( Magnet other)
- {
- }*/
- }
- package magnetPackage;
- import objectdraw.*;
- import java.awt.*;
- public class MagnetGame extends WindowController
- { private Magnet firstMagnet;
- private Location lastPoint;
- private boolean magnetDragged;
- private Magnet secondMagnet;
- private Magnet movingMagnet;
- private Magnet restingMagnet;
- public void begin()
- {
- Location upperLeft = new Location (100,100);
- Location upperLeft2 = new Location (200,200);
- secondMagnet = new Magnet (upperLeft2,canvas);
- firstMagnet = new Magnet (upperLeft,canvas);
- }
- public void onMousePress(Location point)
- {
- if (firstMagnet.contains(point))
- {
- magnetDragged = true;
- lastPoint = point;
- restingMagnet = secondMagnet;
- movingMagnet = firstMagnet;
- }
- if (secondMagnet.contains(point))
- {
- magnetDragged = true;
- lastPoint = point;
- movingMagnet = secondMagnet;
- restingMagnet = firstMagnet;
- }
- }
- public void onMouseDrag(Location point)
- {
- if (magnetDragged)
- {
- movingMagnet.move(point.getX()-lastPoint.getX(), point.getY() - lastPoint.getY());
- lastPoint = point;
- }
- }
- public void onMouseClick( Location point)
- {
- if (firstMagnet.contains(point))
- {
- firstMagnet.flip();
- }
- if (secondMagnet.contains(point))
- {
- secondMagnet.flip();
- }
- }
- }
- package magnetPackage;
- import objectdraw.*;
- import java.awt.*;
- // A class of objects used to represent poles of bar magnets
- //
- // T. Murtagh + K. Bruce 9/16/99
- public class Pole {
- // Arbitrarily chosen range within which poles interact
- private static final double AttractionRange = 90.0;
- private Location location; // Location of the center of the pole
- private Text label; // The pole's type ("N" or "S").
- private Magnet mom; // The magnet which contains this pole
- // Create a new pole.
- //
- // parent - magnet that contains this pole
- // x,y - coordinates for center of the pole
- // name - "N" or "S"
- // canvas - the canvas on which magnet and poles should be drawn
- //
- public Pole ( Magnet parent, double x, double y, String name, DrawingCanvas canvas ) {
- mom = parent;
- this.location = new Location(x,y);
- label = new Text( name, location, canvas );
- label.setFontSize(36);
- label.move( -label.getWidth()/2, -label.getHeight()/2);
- }
- // Return the magnet that contains this pole
- public Magnet getParent() {
- return mom;
- }
- // Return x coordinate of pole's center
- public double getX() {
- return location.getX();
- }
- // Return y coordinate of pole's center
- public double getY() {
- return location.getY();
- }
- // Return coordinates of pole's center
- public Location getLocation() {
- return location;
- }
- // Move pole to a location specified relative to its current location
- public void move( double xoff, double yoff){
- location.translate(xoff,yoff);
- label.move(xoff,yoff);
- }
- // Try to attract a pole of opposite polarity.
- //
- // opposite - another pole whose label ("N" or "S") is the opposite of
- // this pole's.
- //
- // If the other pole is within "AttractionRange" of this pole, move the other
- // magnet so that it is next to this pole's containing magnet
- public void attract( Pole opposite ) {
- double xoff, yoff;
- xoff = opposite.getX() - location.getX();
- yoff = opposite.getY() - location.getY();
- // Check to see if the poles are close enough
- if ( location.distanceTo(opposite.getLocation()) < AttractionRange ) {
- // make the two magnets overlap
- opposite.getParent().moveTo( mom.getLocation());
- if ( Math.abs(xoff) >= Math.abs(yoff) ) {
- // move magnets so that they meet end-to-end
- if ( xoff < 0 ) {
- opposite.getParent().move(-mom.getWidth(), 0);
- } else {
- opposite.getParent().move(mom.getWidth(), 0);
- }
- } else {
- // Move magnets so they will be above one another with attracting pole's
- // aligned vertically
- opposite.getParent().move( location.getX()-opposite.getLocation().getX(),0);
- if ( yoff < 0 ) {
- opposite.getParent().move(0,-mom.getHeight());
- } else {
- opposite.getParent().move(0,mom.getHeight());
- }
- }
- }
- }
- // Try to repel a pole of opposite polarity.
- //
- // opposite - another pole whose label ("N" or "S") is the opposite of
- // this pole's.
- //
- // If the other pole is within "AttractionRange" of this pole, move the other
- // magnet so that the other pole is "AttractionRange" away from this one.
- public void repel( Pole opposite ) {
- double xoff, yoff;
- double distance = location.distanceTo( opposite.getLocation());
- xoff = opposite.getX() - location.getX();
- yoff = opposite.getY() - location.getY();
- // check to see if the poles are close enough
- if ( distance < AttractionRange - 1) {
- // Push other magnet away in the direction of the line between
- // the two poles.
- xoff = (AttractionRange-distance) * xoff /distance;
- yoff = (AttractionRange-distance) * yoff /distance;
- opposite.getParent().move( xoff,yoff );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement