Drag and Drop & Drawing in Swing URGENT!

I have write a program in java that uses swing and databasing for my final project. It is due in a week and a half and i really need advice on where to start!!!

I am going to make a program which has a stick man that has limbs that you can drag and drop. It's going to be for yoga and people recovering from injury. My problem is that we only know basic swing and intermediate level java but we havn't done much graphics work.

I need to know how to make, in swing, a stick man that has arms and legs and a head with little circles at the joints so that i can drag the arm and it moves about the joint at the elbow, keeping the same length.

If somebody could point me in the direction of how to make a point, in swing (i don't even know what component to draw on!! I think jpanel? ) I need to know how to make two points in swing with a line drawn that connects them and when you drag one point the line redraws to reflect the drag. From there i'm sure i could work out the rest.

I REALLY REALLY APPRECIATE HELP. I am at a roadblock in my thinking and i don't know how to begin and it's stressing me out to the point where i can't sleep. I'd really appreciate help and a point in the right direction. If i havn't explained properly then feel free to let me clarify.

All the best

Alex

[1335 byte] By [trust_nowuna] at [2007-11-27 10:17:26]
# 1

hmm. Trust no one and it's your first post. Should we trust you?

tjacobs01a at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 2

http://java.sun.com/docs/books/tutorial/2d/index.html is a good starting point

drawing a point --> Ellipse2D.Double

drawing a line --> Line2D.Double

the double part is optional

Message was edited by:

kerryblue19

kerryblue19a at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 3

I probably could code this one and give you some pointers but since its urgent i'll help you in a week or 2.

deAppela at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 4

I'm sorry if my username isn't up to spec.

It is urgent and i wasn't planning on asking for help because i thought this kind of response might result...

I really do need help. I literally am losing sleep on this because it counts a great part of my final result.

If you don't want to help then thank you for letting me know

Theres nothing i can really give you to incentivise but if you need an incentive then i guess you can't help me.

Hope you have a better day

trust_nowuna at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 5

start by reading the java trail on graphics and on drag and drop. no one is going to code it for you

kerryblue19a at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 6

> I'm sorry if my username isn't up to spec.

>

> It is urgent and i wasn't planning on asking for help

> because i thought this kind of response might

> result...

>

> I really do need help. I literally am losing sleep on

> this because it counts a great part of my final

> result.

>

> If you don't want to help then thank you for letting

> me know

>

> Theres nothing i can really give you to incentivise

> but if you need an incentive then i guess you can't

> help me.

>

> Hope you have a better day

What about you show us what you already made so we can comment on it?

deAppela at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I'm sorry if my username isn't up to spec.

Typical troll response.

"I'm so sorry. But actually YOU'RE a very mean person..."

Plus the fact that you're not posting any code.

I do want to give you one piece of advice though, just so that others reading this hopefully will learn something.

What you're talking about doesn't involve drag & drop (dnd) at least not in the way swing implements it. Searching this site or google for my Draggable class will be a much better place to start.

tjacobs01a at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 8

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

public class StickMan extends JPanel {

Limb[] limbs;

public StickMan() {

Point shoulder = new Point(100,100);

Point elbow = new Point(165,200);

Point wrist = new Point(265, 175);

Point tip = new Point(290, 175);

limbs = new Limb[3];

limbs[0] = new Limb(shoulder, elbow);

limbs[1] = new Limb(elbow, wrist);

limbs[2] = new Limb(wrist, tip);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

for(int j = 0; j < limbs.length; j++)

limbs[j].draw(g2);

}

public static void main(String[] args) {

StickMan test = new StickMan();

Articulator articulator = new Articulator(test);

test.addMouseListener(articulator);

test.addMouseMotionListener(articulator);

JFrame f = new JFrame("click line and drag");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

class Limb {

Point proximal;

Point distal;

Line2D.Double line;

double length;

public Limb(Point p1, Point p2) {

proximal = p1;

distal = p2;

line = new Line2D.Double(p1, p2);

length = proximal.distance(distal);

}

public void draw(Graphics2D g2) {

g2.setPaint(Color.blue);

g2.draw(line);

g2.setPaint(Color.red);

g2.draw(new Ellipse2D.Double(proximal.x-2, proximal.y-2, 4, 4));

}

public void rotate(double theta) {

double x = proximal.x + length*Math.cos(theta);

double y = proximal.y + length*Math.sin(theta);

distal.setLocation(x, y);

line.setLine(proximal, distal);

}

public void move(int dx, int dy) {

// proximal was adjusted by the ancestor limb.

distal.translate(dx, dy);

line.setLine(proximal, distal);

}

}

class Articulator extends MouseInputAdapter {

StickMan component;

final int S = 6;

Rectangle locater = new Rectangle(S,S);

int selectedIndex;

boolean dragging = false;

public Articulator(StickMan sm) {

component = sm;

}

public void mousePressed(MouseEvent e) {

Point p = e.getPoint();

locater.setFrameFromCenter(p.x, p.y, p.x+S/2, p.y+S/2);

Limb[] limbs = component.limbs;

for(int j = 0; j < limbs.length; j++) {

if(limbs[j].line.intersects(locater)) {

selectedIndex = j;

dragging = true;

break;

}

}

}

public void mouseReleased(MouseEvent e) {

dragging = false;

}

public void mouseDragged(MouseEvent e) {

if(dragging) {

Point p = e.getPoint();

Limb limb = component.limbs[selectedIndex];

Point origin = limb.proximal;

Point distalBefore = new Point(limb.distal);

double dy = p.y - origin.y;

double dx = p.x - origin.x;

double theta = Math.atan2(dy, dx);

limb.rotate(theta);

updateDistalLimbs(distalBefore, limb.distal);

component.repaint();

}

}

private void updateDistalLimbs(Point start, Point end) {

int dx = end.x - start.x;

int dy = end.y - start.y;

Limb[] limbs = component.limbs;

for(int j = selectedIndex+1; j < limbs.length; j++)

limbs[j].move(dx, dy);

}

}

crwooda at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 9

Troll?

Hmmm.. i think not.

I may not have 27361 posts but that is not a true indication of my intentions or my character.

Does sitting posting little comments like yours until you have a high status ranking on the forum make you feel good? Jeeeez. What is this world coming to?

I take computer science as a subject at school and i come top of my class but we havn't been taught any graphics type stuff other than swing. I only have a few posts because i don't use java as a part of my daily life or my job, it's for a project at school.

I appreciate the help mister crwood. Sadly, my laptop isn't with me but if it turns out that it works then i'm going to get in touch with you and keep you posted, so to speak, on how my project goes and send you something as a token of my gratitude.

Java can be extremely confusing and we havn't been taught anything relating to the awt class...

i refuse to apologise for seeking help online. I do forgive you though. You don't have to be so hostile guys. It's gonna be okay. i promise.

trust_nowuna at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 10

> i refuse to apologise for seeking help online. I do

> forgive you though. You don't have to be so hostile

> guys. It's gonna be okay. i promise.

Remember - it's urgent for you, not for us. You waited until the last two weeks to start working on that assignment, not us. It's your grade, not ours. Nobody is being hostile, it's just that putting "urgent" (and in caps) implies that your problem is more important than everybody else's. It is to you, but not to us.

So chill. It's gonna be okay. I promise.

kirillga at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 11

trust_nowon: Unfortunately you are learning a lesson learned the hard way: mark something as urgent here and at gets people's hackles up. It's generally seen as you saying that your problem is more important than any of the other posters here. Even if that was not your intent, you have to know, that is how it is seen.

I have found that if you post a well thought-out question and post it in a way that shows that you've put effort into it (meaning -- show your code), you will usually get a very quick and helpful resopnse.

Other recommendations to help you get a helpful and prompt response:

1) Make the volunteer helper's job as easy as possible. The more effort you put into describing your problem in a clear and concise manner, the more likely that someone will read, think through, and help solve your problem.

2) If you post code, use code tags. This makes your code easier to read and again makes our job easier.

3) If you post code, don't show too much,... show enough that it demonstrates your problem, and...

4) Don't show too little. Make your code a compilable unit that can stand on its own.

That's it for now. Good luck.

Addendum:

For more info on how to ask a question the right way (the way to get helpful responses quickly), please check out:

http://www.catb.org/~esr/faqs/smart-questions.html

Message was edited by:

petes1234

petes1234a at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...
# 12

> import java.awt.*;

> .............................

> .............................

> .............................

> .............................

>private void updateDistalLimbs(Point start, Point end) {

> int dx = end.x - start.x;

> int dy = end.y - start.y;

> Limb[] limbs = component.limbs;

> for(int j = selectedIndex+1; j < limbs.length; j++)

> limbs[j].move(dx, dy);

>}

> }

The Dukes whre strikes again.

petes1234a at 2007-7-28 15:51:42 > top of Java-index,Desktop,Core GUI APIs...