Hi guys im new to the list and suprise suprise I was wondering if you guys
could help me out with something. Im working on a program where you have
two identical shapes. When you start the program the two shapes should be
away from each other. One shape should be motionless which will be
black,and the other shape should be able to be dragged by mouse, this shape
will be blue. When the blue shape comes in contact with the black shape it
should move to cover it, and the blue shape will turn gold. At this stage I
should not be able to drag the mouse anymore. There should also be a reset
button which changes the shapes back to their original position. What ive
managed to do so far is draw the shapes and get them colored. I can drag
one while one is still. I also have a reset button. Heres my code so far.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class drag extends Applet {
private int i; //index for loops
private int last_x, last_y; // last position of mouse
private int Dx, Dy; // change in mouse position
private int tx=200, ty=200; // total translation of
// the shape from 'home'
private boolean dragFlag=false; // Are we dragging?
private boolean closeFlag=false;
private Polygon Jig; // The shape itself
Button Reset;
public void init() {
Jig=new Polygon();
Jig.addPoint(50,210);
Jig.addPoint(130,210); // coordinates of the shape
Jig.addPoint(130,178);
Jig.addPoint(170,194);
Jig.addPoint(186,178);
Jig.addPoint(170,154);
Jig.addPoint(130,162);
Jig.addPoint(130,130);
Jig.addPoint(50,130);
Reset = new Button( "Play Again" );
Reset.addActionListener( new ResetHandler() ); // Reset button
Reset.setBounds( 120, 40, 50, 20);
add(Reset);
}
class ResetHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
tx = 200; // coordinates of where the shapes will be reset
to.
ty = 200;
repaint();
}
}
public boolean mouseDown(Event e,int xx,int yy) {
if (Jig.inside(xx-tx,yy-ty)) { // if mouse down inside current shape
position
dragFlag=true; // yes we are dragging
last_x=xx; // remember this mouse position
last_y=yy;
return true;}
else {return false;}
}
public boolean mouseDrag(Event e,int xx,int yy) {
if(dragFlag) { // if we are dragging?
Dx=xx-last_x; // compute change in mosue position
Dy=yy-last_y;
tx+=Dx; // add change to the total translation of the shape
ty+=Dy;
if (tx*tx+ty*ty<8) {closeFlag=true;};
last_x=xx; // current position becomes old mouse position
last_y=yy;
repaint(); // repaint the shape
return true;}
else {
return false;}
}
public boolean mouseUp(Event e, int xx, int yy) {
dragFlag=false; // finished dragging
return false;
}
public void paint (Graphics g) {
int[] xpts = new int[9]; //arrays for drawing the star
int[] ypts = new int[9];
for(i=0;i<Jig.npoints;i++) {
xpts[i]=Jig.xpoints[i]+tx;
ypts[i]=Jig.ypoints[i]+ty;
}
g.setColor(Color.black);
g.fillPolygon(Jig); // draw the shape
g.setColor(Color.blue);
g.fillPolygon(xpts,ypts,9); // draw the shape
if (closeFlag=true) {g.setColor(Color.green);};
}
}
Ok the problem is for the life of my I cant figure out how to make the
shapes stop moving and change color. I was suggested to use this piece of
code
if (tx*tx+ty*ty<8) {closeFlag=true;};
From what I understand of this code tx is the x coordinates and ty is the y
coordinates, when the shape reaches these coordinates where tx and ty are
less than 8 then the closeFlag will be true. I also have another bit of
code which says
if (closeFlag=true) {g.setColor(Color.green);};
Ok in conjunction with this code the shape should turn green...but nothing
happens. Ive tried fiddling around with it and racking my brains but
nothing seems to work. I also have another idea that to stop the shape from
moving I could use dragFlag, instead of closeFlag, so for example I would
have
if (tx*tx+ty*ty<8) {dragFlag=false;};
At another point in the program I would have
if (dragFlag=true) {g.setColor(Color.green);};
I actually tried this and from what I remember is that the shape just
stopped moving, but what I wanted to happen is that the shape should stop
moving when it gets to <8. Ok ermm so what im saying is could someone help
me find out where im going wrong? Would someone be able to show me how I can
make this shape stop moving when it reaches a certain point. If someone was
able to show me this I think I could figure out how to change the color to.
Ive also tried looking at loads of websites to help me with this coursework.
Ive found stuff on making a shape move and how to make a reset button, but
nothing on stopping motion at certain points. Does anyone know a website
that does? Anyway your input would be much appreciated.
P.S. Ignore the > symbols on the right of the email. I had to copy and
paste it from another email because I sent it to the wrong person.
_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
http://join.msn.com/?page=features/virus
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".