• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dragging Components Relative To Eachother

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, whew...

Ive been breaking my back over this program, which is about 85% done. Currently running Blue J. Heres the description, and ill follow up with my code.

1. If you drag on any part of one of your spaceships, the spaceship should move with the
mouse, flying away and leaving its landing gear behind. The movement should be
smooth and not jumpy.
2. If you drag on any part of the landing gear for one of your spaceships, both the landing
gear and the spaceship should move together with the mouse. Even if the spaceship has
flown away from its landing gear, both the landing gear and spaceship should move the
same amount, maintaining the same relative positions to each other.

Here the code...

---------------------------------------------------------

\\ShipApp.java//

import wheels.users.*;

public class ShipApp extends Frame
{
private SpaceShip _ship1, _ship2;

public ShipApp()
{
_ship1 = new SpaceShip(java.awt.Color.blue);
_ship1.setLocation(100,100);
_ship2 = new SpaceShip(java.awt.Color.red);
_ship2.setLocation(300,300);
}

public static void main(String [] args)
{
ShipApp app = new ShipApp();
}
}


---------------------------------------------------------

\\Spaceship.java//

import wheels.users.*;

public class SpaceShip
{
private ShipBody _shipBody;
private LandingGear _landingGear;
private int _x, _y;
private java.awt.Point _lastMousePosition;

public SpaceShip(java.awt.Color color)
{
_landingGear = new LandingGear(this);
_shipBody = new ShipBody(color,this);

}

public void setLocation(int x, int y)
{
_x = x;
_y = y;
_shipBody.setLocation(x,y);
_landingGear.setLocation(x + 85, y + 115);

}

public void mousePressed(java.awt.event.MouseEvent e)
{
_lastMousePosition = e.getPoint();
}

public void mouseDragged(java.awt.event.MouseEvent e)
{
java.awt.Point currentPoint = e.getPoint();
int diffx = currentPoint.x - _lastMousePosition.x;
int diffy = currentPoint.y - _lastMousePosition.y;
this.setLocation(_x + diffx, _y + diffy);
_lastMousePosition = currentPoint;
}
}


---------------------------------------------------------

\\ShipBody.java//

import wheels.users.*;

public class ShipBody
{
private ShipPart _shipSaucer, _shipBubble, _shipWindow1, _shipWindow2, _shipWindow3, _shipStripe;
private java.awt.Point _lastMousePosition;
private int _x, _y;
private SpaceShip _ship;

public ShipBody(java.awt.Color color, SpaceShip ship)
{
java.awt.Color seagreen = new java.awt.Color(60, 179, 113);

_ship = ship;
_shipBubble = new ShipPart(seagreen, 115,200, this);
_shipSaucer = new ShipPart(color, 300,125, this);
_shipWindow1 = new ShipPart(seagreen, 44,32, this);
_shipWindow2 = new ShipPart(seagreen, 44,32, this);
_shipWindow3 = new ShipPart(seagreen, 44,32, this);
_shipStripe = new ShipPart(seagreen, 335, 10, this);
}

public void setLocation(int x, int y)
{
_x = x;
_y = y;
_shipSaucer.setLocation(x,y);
_shipBubble.setLocation(x + 90, y - 40);
_shipWindow1.setLocation(x+ 65,y + 20);
_shipWindow2.setLocation(x + 125, y + 20);
_shipWindow3.setLocation(x + 185, y + 20 );
_shipStripe.setLocation(x-10, y + 30);

}

public void mousePressed(java.awt.event.MouseEvent e)
{
_lastMousePosition = e.getPoint();
/*_ship.mousePressed(e);*/

}

public void mouseDragged(java.awt.event.MouseEvent e)
{
java.awt.Point currentPoint = e.getPoint();
int diffx = currentPoint.x - _lastMousePosition.x;
int diffy = currentPoint.y - _lastMousePosition.y;
this.setLocation(_x + diffx, _y + diffy);
_lastMousePosition = currentPoint;
/*_ship.mouseDragged(e);*/
}
}


---------------------------------------------------------

\\ShipPart.java//


import wheels.users.*;

public class ShipPart extends Ellipse
{
private java.awt.Point _lastMousePosition;
private java.awt.Color _color;
private ShipBody _body;

public ShipPart(java.awt.Color color, int w, int h, ShipBody body)
{
_lastMousePosition = new java.awt.Point();
this.setColor(color);
this.setSize(w,h);
_color = color;
_body = body;
}

public void mousePressed(java.awt.event.MouseEvent e)
{
_lastMousePosition = e.getPoint();
_body.mousePressed(e);
}

public void mouseDragged(java.awt.event.MouseEvent e)
{
java.awt.Point currentPoint = e.getPoint();
int diffx = currentPoint.x - _lastMousePosition.x;
int diffy = currentPoint.y - _lastMousePosition.y;
this.setLocation(this.getLocation().x + diffx, this.getLocation().y + diffy);
_lastMousePosition = currentPoint;
_body.mouseDragged(e);
}
}


---------------------------------------------------------

\\LandingGear.java//


import wheels.users.*;

public class LandingGear
{
private LandingGearPart _gear1, _gear2, _gear3, _wheel1, _wheel2, _wheel3;
private int _x, _y;
private java.awt.Point _lastMousePosition;
private SpaceShip _landing;


public LandingGear(SpaceShip landing)
{
java.awt.Color lightgrey = new java.awt.Color(211, 211, 211);
java.awt.Color darkgrey = new java.awt.Color(13, 13, 13);

_landing = landing;
_gear1 = new LandingGearPart(lightgrey, 8,70, this);
_gear2 = new LandingGearPart(lightgrey, 8,70, this);
_gear3 = new LandingGearPart(lightgrey, 8,70, this);
_wheel1 = new LandingGearPart(darkgrey, 25,25, this);
_wheel2 = new LandingGearPart(darkgrey, 25,25, this);
_wheel3 = new LandingGearPart(darkgrey, 25,25, this);

_gear1.setRotation(220);
_gear2.setRotation(180);
_gear3.setRotation(320);

}


public void setLocation(int x, int y)
{
_x = x;
_y = y;
_gear1.setLocation(x,y);
_gear2.setLocation(x + 60,y);
_gear3.setLocation(x + 120,y);
_wheel1.setLocation(x - 40, y + 60);
_wheel2.setLocation(x + 50, y + 70);
_wheel3.setLocation(x + 140, y + 60);
}

public void mousePressed(java.awt.event.MouseEvent e)
{
_lastMousePosition = e.getPoint();
_landing.mousePressed(e);
}

public void mouseDragged(java.awt.event.MouseEvent e)
{
java.awt.Point currentPoint = e.getPoint();
int diffx = currentPoint.x - _lastMousePosition.x;
int diffy = currentPoint.y - _lastMousePosition.y;
this.setLocation(_x + diffx, _y + diffy);
_lastMousePosition = currentPoint;
_landing.mouseDragged(e);
}
}


---------------------------------------------------------

\\LandingGearPart.java//


import wheels.users.*;

public class LandingGearPart extends Ellipse
{
private java.awt.Point _lastMousePosition;
private java.awt.Color _color;
private LandingGear _landingGear;

public LandingGearPart(java.awt.Color color,int w, int h, LandingGear landinggear)
{
_lastMousePosition = new java.awt.Point();
this.setSize(w,h);
this.setColor(color);
_landingGear = landinggear;
}

public void mousePressed(java.awt.event.MouseEvent e)
{
_lastMousePosition = e.getPoint();
_landingGear.mousePressed(e);
}

public void mouseDragged(java.awt.event.MouseEvent e)
{
java.awt.Point currentPoint = e.getPoint();
int diffx = currentPoint.x - _lastMousePosition.x;
int diffy = currentPoint.y - _lastMousePosition.y;
this.setLocation(this.getLocation().x + diffx, this.getLocation().y + diffy);
_lastMousePosition = currentPoint;
_landingGear.mouseDragged(e);
}
}


---------------------------------------------------------

AND uses wheels graphics library Wheels Graphics Library

The program is totally functional. Lemme explain:

Can choose color and location of the space ships within the frame from the ShipApp class. If you click on the saucer of the ship, it breaks away from the landing gear and can move it around, works fine. If you click on the landing gear to drag, the whole ship moves as one, works. BUTTT once their separated, if you drag the landing gear its supposed to drag relatively to the saucer, which i can NOT figure out!!! Help would be MUCH appreciated, for the program has eaten up my time and any sanity i have left. Been scrounging for some examples or anything resembling what I need, but to no avail. THANK YOU for any help that can be offered!!!

P.S- I THINK I have to add some sort of loop or boolean to one of the landing gear mouse events to stop it from snapping back to the saucer by default, but im not sure. Going back to trying to find something, somewhere, anywhere while you amazing gurus hopefully help me out ha

[ February 25, 2008: Message edited by: Kingsta ]

[ February 25, 2008: Message edited by: Kingsta ]

[ February 25, 2008: Message edited by: Kingsta ]
[ February 26, 2008: Message edited by: King8654 ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch. Please don't call your posts Urgent. Please check the JavaRanch naming policy and adjust your displayed name accordingly.
 
reply
    Bookmark Topic Watch Topic
  • New Topic