• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Drawing/moving problem: how to define destination? (code included)

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
(sorry if my english is not understandable enough)

I have created a program where user can define start point and destination point with a mouse. After doing this line is drawn between these points(coordinates using drawLine).

Then I have this Run button and when it is pressed letter X starts to move to a direction which is "hard coded" to method (updateXPosition). This letter X moves once in a second(I am using thread and timer but timer is for other purposes). Starting point of letter X is start point(point what user has pressed first).

So I know:
+ coordinates of the start and destination
+ when to move letter X (thread/timer, it has to move once in a second)
+ coordinates where to start to move letter X
+ my current location (X current location)

My problem is:
How do I get that letter X to moves towards destination point once in a second???
What is a simpliest way to do it with my code??


Below is code of the program:
it is quite long but I really need help with this one.. Tried for days to solve this problem.


"Main"



Class for Global variables
[code]


/* This class exist because it is easiest way to use "global" variables
*
*/
public class Global
{
// Must have some value at beginning otherwise main does not work as it should be
public static int gX1 = 5;
public static int gY1 = 5;
public static int gX2 = 5;
public static int gY2 = 5;
public static int secondFromBeginning = 0;

public static int sailLocationX = 10;
public static int sailLocationY = 10;

public static double distance = 0;

public static int counter=1;


public static int xx = 0;
public static int yy = 0;


}
[code]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried to make minimal changes to answer your question, including:
Changed all class names to avoid name-clashing so you can run as-is.
Eliminated the updateXPosition and startTimer methods.
Made the Timer a member variable (class scope) and implemented ActionListener for
Main2 for the timer to use. Animation code is inside the actionPerformed method.
Changed DrawingPanel to a JPanel and eliminated its update method.
 
Maria Sachhause
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Craig Wood. You helped me a lot

Now I am trying to add one more point. Now if I press mouse twise I get two points(start and destination), but when I try to add third it does not work.

Below is how I try to do it, I don't get it why it doesn't work?


[ October 30, 2007: Message edited by: Maria Sachhause ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Maria Sachhause
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again You are really helping me

What if I want that third mouse pressing draws a line from given coordinate to the center?
So, I mean that if I want that first and second mouse press draws a line between those points like now it does but the third press draws a line from its coordinates toward center.

Should I do like this, where windSetLine x and y are global variables which tells where center is (250 and 250).
g.drawLine(x3, y3, Global2.windSetLineX,Global2.windSetLineY );
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should I do like this, where windSetLine x and y are global variables which tells where
center is (250 and 250).

Sounds like it would do what you want.
 
Maria Sachhause
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, few days have passed and new and last problem has risen.

Now I can make this.

step-by-step

1. First mouse press defines start point
2. Second mouse press defines destination point
3. Line is drawn between start and destination points
4. Third mouse press draws a line towards center

I have this coordinates: north is 0 and clockwise 360 degrees

-----------------360/0-------------------
-------------------*---------------------
270************************************90
-------------------*---------------------
-----------------180---------------------


And my problem is this:

-How can I draw a line from the start point towards degree 140 (which is hardcoded to for example to variable int heading=140)???
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// start at x1, y1
// destination at x2, y2
// third mouse click at x3, y3
// distance from start to third mouseClick
double distance = Point2D.distance(x1, y1, x3, y3);
// or use any arbitrary distance
// angle theta = 140 degrees expressed in radians
double theta = Math.toRadians(140);
// Angles in java are measured positive clockwise from
// zero at 3 o'clock. To start at 12 o'clock you can
// adjust theta by 90 degrees == Math.PI/2 as needed.
theta -= Math.PI/2;
double x = x1 + distance*Math.cos(theta);
double y = y1 + distance*Math.sin(theta);
// draw line from start to (x, y)
// it will have length = "distance" and a
// direction of 140 degrees from start to (x, y)
g.drawLine(x1, y1, x, y);
 
I'm so happy! And I wish to make this tiny ad happy too:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic