Advertisement
lgfjj

Untitled

Feb 21st, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.81 KB | None | 0 0
  1. package magnetPackage;
  2. import objectdraw.*;
  3. import java.awt.*;
  4.  
  5.  
  6. // Skeletal definition of class of objects used to represent bar magnets
  7. //
  8. // Please complete the implementation of these methods and add the others
  9. // described in the lab handout.
  10. public class Magnet {
  11.  
  12. private static final double MAGNET_WIDTH = 150; // dimensions of magnets
  13. private static final double MAGNET_HEIGHT = 50;
  14. private static final double POLE_DISTANCE = MAGNET_HEIGHT/2;
  15.  
  16.  
  17. private Pole northPole; // North pole
  18. private Pole southPole; // South pole
  19. private FramedRect box; // Box representing perimeter of magnet
  20.  
  21. public Magnet ( Location point, DrawingCanvas canvas)
  22. {
  23. northPole = new Pole ( this, point.getX() + POLE_DISTANCE, point.getY() + POLE_DISTANCE, "N", canvas);
  24. box = new FramedRect (point.getX(),point.getY(),150,50,canvas);
  25. southPole = new Pole (this, point.getX() + (MAGNET_WIDTH-POLE_DISTANCE), point.getY() + POLE_DISTANCE, "S", canvas);
  26.  
  27.  
  28.  
  29.  
  30. }
  31.  
  32. // accessor methods
  33. // returns the upper-left coordinates of the magnet
  34. public Location getLocation()
  35. {
  36. return box.getLocation();
  37. }
  38. public Pole getNorth ()
  39. {
  40. return northPole;
  41. }
  42.  
  43. public Pole getSouth ()
  44. {
  45. return southPole;
  46. }
  47.  
  48. // This should return true if the given point is within the magnet. The current
  49. // implementation does not do this!!! You must change the body of this method so
  50. // that it has the proper functionality!!!
  51. public boolean contains( Location point ) {
  52. if (box.contains(point))
  53. return true;
  54. return false; // REPLACE THIS LINE OF CODE WITH THE CORRECT IMPLEMENTATION
  55. }
  56.  
  57. // returns the width of the magnet
  58. public double getWidth() {
  59. return MAGNET_WIDTH;
  60. }
  61.  
  62. // returns the height of the magnet
  63. public double getHeight() {
  64. return MAGNET_HEIGHT;
  65. }
  66.  
  67.  
  68. public void move(double xoff, double yoff)
  69. {
  70. box.move(xoff, yoff);
  71. northPole.move(xoff, yoff);
  72. southPole.move(xoff, yoff);
  73. }
  74.  
  75. // mutator methods
  76. public void moveTo( Location point) {
  77. double dx, dy;
  78. dx = point.getX() - box.getX();
  79. dy = point.getY() - box.getY();
  80. box.moveTo(point);
  81. northPole.move(dx,dy);
  82. southPole.move(dx,dy);
  83. }
  84.  
  85. public void flip ()
  86. {
  87. double x1,x2;
  88. x1 = MAGNET_WIDTH-POLE_DISTANCE;
  89. x2 = -MAGNET_WIDTH+POLE_DISTANCE;
  90.  
  91.  
  92.  
  93. northPole.move(x1,0);
  94. southPole.move(x2,0);
  95.  
  96.  
  97. }
  98.  
  99.  
  100. /* public void interact ( Magnet other)
  101. {
  102.  
  103.  
  104.  
  105.  
  106. }*/
  107.  
  108. }
  109.  
  110. package magnetPackage;
  111. import objectdraw.*;
  112. import java.awt.*;
  113.  
  114. public class MagnetGame extends WindowController
  115. { private Magnet firstMagnet;
  116. private Location lastPoint;
  117. private boolean magnetDragged;
  118. private Magnet secondMagnet;
  119. private Magnet movingMagnet;
  120. private Magnet restingMagnet;
  121.  
  122.  
  123. public void begin()
  124. {
  125. Location upperLeft = new Location (100,100);
  126. Location upperLeft2 = new Location (200,200);
  127.  
  128. secondMagnet = new Magnet (upperLeft2,canvas);
  129. firstMagnet = new Magnet (upperLeft,canvas);
  130.  
  131. }
  132.  
  133. public void onMousePress(Location point)
  134. {
  135. if (firstMagnet.contains(point))
  136. {
  137. magnetDragged = true;
  138. lastPoint = point;
  139. restingMagnet = secondMagnet;
  140. movingMagnet = firstMagnet;
  141. }
  142.  
  143. if (secondMagnet.contains(point))
  144. {
  145. magnetDragged = true;
  146. lastPoint = point;
  147. movingMagnet = secondMagnet;
  148. restingMagnet = firstMagnet;
  149. }
  150. }
  151.  
  152. public void onMouseDrag(Location point)
  153. {
  154. if (magnetDragged)
  155. {
  156. movingMagnet.move(point.getX()-lastPoint.getX(), point.getY() - lastPoint.getY());
  157. lastPoint = point;
  158. }
  159.  
  160.  
  161.  
  162. }
  163.  
  164. public void onMouseClick( Location point)
  165. {
  166. if (firstMagnet.contains(point))
  167. {
  168. firstMagnet.flip();
  169. }
  170. if (secondMagnet.contains(point))
  171. {
  172. secondMagnet.flip();
  173. }
  174. }
  175.  
  176. }
  177.  
  178. package magnetPackage;
  179. import objectdraw.*;
  180. import java.awt.*;
  181.  
  182.  
  183. // A class of objects used to represent poles of bar magnets
  184. //
  185. // T. Murtagh + K. Bruce 9/16/99
  186. public class Pole {
  187.  
  188. // Arbitrarily chosen range within which poles interact
  189. private static final double AttractionRange = 90.0;
  190.  
  191. private Location location; // Location of the center of the pole
  192. private Text label; // The pole's type ("N" or "S").
  193.  
  194. private Magnet mom; // The magnet which contains this pole
  195.  
  196.  
  197. // Create a new pole.
  198. //
  199. // parent - magnet that contains this pole
  200. // x,y - coordinates for center of the pole
  201. // name - "N" or "S"
  202. // canvas - the canvas on which magnet and poles should be drawn
  203. //
  204. public Pole ( Magnet parent, double x, double y, String name, DrawingCanvas canvas ) {
  205. mom = parent;
  206. this.location = new Location(x,y);
  207. label = new Text( name, location, canvas );
  208. label.setFontSize(36);
  209. label.move( -label.getWidth()/2, -label.getHeight()/2);
  210. }
  211.  
  212. // Return the magnet that contains this pole
  213. public Magnet getParent() {
  214. return mom;
  215. }
  216.  
  217. // Return x coordinate of pole's center
  218. public double getX() {
  219. return location.getX();
  220. }
  221.  
  222. // Return y coordinate of pole's center
  223. public double getY() {
  224. return location.getY();
  225. }
  226.  
  227. // Return coordinates of pole's center
  228. public Location getLocation() {
  229. return location;
  230. }
  231.  
  232. // Move pole to a location specified relative to its current location
  233. public void move( double xoff, double yoff){
  234. location.translate(xoff,yoff);
  235. label.move(xoff,yoff);
  236. }
  237.  
  238.  
  239. // Try to attract a pole of opposite polarity.
  240. //
  241. // opposite - another pole whose label ("N" or "S") is the opposite of
  242. // this pole's.
  243. //
  244. // If the other pole is within "AttractionRange" of this pole, move the other
  245. // magnet so that it is next to this pole's containing magnet
  246. public void attract( Pole opposite ) {
  247. double xoff, yoff;
  248.  
  249. xoff = opposite.getX() - location.getX();
  250. yoff = opposite.getY() - location.getY();
  251.  
  252. // Check to see if the poles are close enough
  253. if ( location.distanceTo(opposite.getLocation()) < AttractionRange ) {
  254.  
  255. // make the two magnets overlap
  256. opposite.getParent().moveTo( mom.getLocation());
  257.  
  258. if ( Math.abs(xoff) >= Math.abs(yoff) ) {
  259. // move magnets so that they meet end-to-end
  260. if ( xoff < 0 ) {
  261. opposite.getParent().move(-mom.getWidth(), 0);
  262. } else {
  263. opposite.getParent().move(mom.getWidth(), 0);
  264. }
  265. } else {
  266. // Move magnets so they will be above one another with attracting pole's
  267. // aligned vertically
  268.  
  269. opposite.getParent().move( location.getX()-opposite.getLocation().getX(),0);
  270. if ( yoff < 0 ) {
  271. opposite.getParent().move(0,-mom.getHeight());
  272. } else {
  273. opposite.getParent().move(0,mom.getHeight());
  274. }
  275.  
  276.  
  277. }
  278. }
  279. }
  280.  
  281. // Try to repel a pole of opposite polarity.
  282. //
  283. // opposite - another pole whose label ("N" or "S") is the opposite of
  284. // this pole's.
  285. //
  286. // If the other pole is within "AttractionRange" of this pole, move the other
  287. // magnet so that the other pole is "AttractionRange" away from this one.
  288. public void repel( Pole opposite ) {
  289.  
  290. double xoff, yoff;
  291. double distance = location.distanceTo( opposite.getLocation());
  292.  
  293. xoff = opposite.getX() - location.getX();
  294. yoff = opposite.getY() - location.getY();
  295.  
  296. // check to see if the poles are close enough
  297. if ( distance < AttractionRange - 1) {
  298. // Push other magnet away in the direction of the line between
  299. // the two poles.
  300. xoff = (AttractionRange-distance) * xoff /distance;
  301. yoff = (AttractionRange-distance) * yoff /distance;
  302.  
  303. opposite.getParent().move( xoff,yoff );
  304. }
  305.  
  306. }
  307.  
  308.  
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement