1. Member
Join Date
Oct 2008
Posts
10
Rep Power
0

## Help with canvas

alright im having trouble getting this program to work correctly im sure its one of my equations but im not real good with trig and it works up to a point.

the goal: make a random number of dots that circle around a center point (int this case 250, 250) and stay at a constant distance from said point. each dot is to travel at different speeds.

Java Code:
```import java.awt.Color;
import java.math.*;
import java.lang.*;

public class dots {
private double xposition;
private double yposition;

private double angle;
private double distance;
private double x;
private double y;
private double yChange;
private double xChange;

private Color color;

public dots()
{
reset();
}

public void reset()
{
xposition = (Math.random() * 400 + 1);
yposition = (Math.random() * 400 + 1);

color = new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256) );

xChange = (Math.random() * .0001);
yChange = (Math.random() * .0001);

x = 250 - xposition;
y = 250 - yposition;

distance = Math.sqrt((Math.pow(x, 2) + Math.pow(y, 2)));
}

{

x = 250 - xposition;

double a = x / distance;

angle = Math.acos(a);

xposition = 250 - (distance * Math.cos((angle - xChange)));
yposition = 250 - (distance * Math.sin((angle - yChange)));

System.out.println("angle: " + angle);
System.out.println("x pos: " + xposition);
System.out.println("y pos: " + yposition);
}

public Color getColor()
{
return color;
}

public int getX()
{
return (int)xposition;
}

public int getY()
{
return (int)yposition;
}

}```
this is my dot class which is activated by my gui class. there is a problem in advance somewhere and ive tried several different equations but this is the only one that produces any resutls.

my problem is the dots all stop when they hit about the 9 o clock position, like they are hitting an invisible wall.

any help would be great! and i know my equations prob are very wrong. again not good at trig.

2. Member
Join Date
Oct 2008
Posts
10
Rep Power
0
this is my gui code if you need it.

Java Code:
```import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.math.*;
import java.lang.*;

public class SpinningDots implements ActionListener{
private JFrame window;
private LayoutManager layout;
private Canvas canvas;
private JButton run;

public SpinningDots(){
window = new JFrame("Spinning Dots");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
layout = new FlowLayout();
window.setLayout(layout);
window.setBounds(300, 250, 550, 600);

canvas = new Canvas();
canvas.setBackground(Color.BLACK);
canvas.setSize(500,500);

run = new JButton("Run");

window.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
Graphics g = canvas.getGraphics();

dots[] dots = new dots[15];

for(int i = 0; i < dots.length; i++)
{
dots[i] = new dots();
}

double StartTime = System.currentTimeMillis();
double time;

do{
time = System.currentTimeMillis();
for(dots d : dots)
{
g.setColor(Color.BLACK);
g.fillOval(d.getX(), d.getY(), 6, 6);
g.setColor(d.getColor());
g.fillOval(d.getX(), d.getY(), 6, 6);
}
}while(time <= (StartTime + 10000));

}

public static void main(String[] args)
{
new SpinningDots();
}

}```

3. the dots all stop when they hit about the 9 o clock position
How are the values changing before they stop?
And then what do they do after they stop?
Can you post the values with notes showing where the values change and where they stop?

4. You may want to read up on Graphics coding at the Sun Graphics tutorial. A few suggestions:

1) Don't mix AWT components (the Canvas) with Swing components (JFrame and everything else). Stick with Swing and draw on a JPanel or JComponent.
2) Don't get a Graphics object by extracting it from a component. Instead paint within the JPanel or JComponent's paintComponent(Graphics g) method and use the Graphics object, g, that is passed to the program from the JVM.

HTH and good luck.

1

5. Member
Join Date
Oct 2008
Posts
10
Rep Power
0
Java Code:
```x pos: 169.97101431583582
y pos: 250.00195732369585
angle: 1.9864818412147502E-5
x pos: 169.97101430261705
y pos: 250.00100910198697
angle: 8.016340004459174E-6```
that is what it repeats the entire time.

and for the mixing of the types, this is for my intro to java class and so far this is how he has taught us to go about using canvas and stuff. i will indeed read the tut though.

edit: this is from a single dot not all 15

6. What is your algorithm for the moving of the dot?
Have you worked out on a piece of graph paper the correct values of x,y and angle for what you want to happen?
What are the values for x,x and angle you need computed to have the dot move where you want it to be?
Work with a single dot to figure out how to change the x, y and angle to have it move where you want.

7. Member
Join Date
Oct 2008
Posts
10
Rep Power
0
Originally Posted by Norm
What is your algorithm for the moving of the dot?
Have you worked out on a piece of graph paper the correct values of x,y and angle for what you want to happen?
What are the values for x,x and angle you need computed to have the dot move where you want it to be?
Work with a single dot to figure out how to change the x, y and angle to have it move where you want.
i havent worked it out on paper yet i will have to sit down and do that.

as for the angle it is random based on where the dot is formed from the center which is (250, 250).
Java Code:
```x = 250 - xposition;
y = 250 - yposition;
distance = Math.sqrt((Math.pow(x, 2) + Math.pow(y, 2)));```
that finds the dots x and y and distance from the center. this is called when the dot is created.

below is called when the dot is to move.
Java Code:
```x = 250 - xposition; //again finds updated x location
//only x is needed because it is used to find the angle

double a = x / distance; //sets up to find the cos

angle = Math.acos(a); //finds the current angle from the center using acos, this should be the correct way to find the angle it is all i could find

xposition = 250 - (distance * Math.cos((angle - xChange))); //finds new x position by taking the center point and subtracting the distance to keep it the same distance away, times angle minus the angle of change which is determined when dot is made
yposition = 250 - (distance * Math.sin((angle - yChange))); //same as above```
thanks for the suggestions and ill sit down and plot out the dot on a few points

8. I've seen some programs that have display the sweep hands for clocks. If you could find one of those, you could get the code for moving a position in a circle.

You don't say if the dot moves clockwise or counterclockwise.

A comment on using hardcoded values in code. Don't.
Make the 250 a final int variable and use that in your code.

9. Member
Join Date
Oct 2008
Posts
10
Rep Power
0
ah sorry yeah they are supposed to be going counter clockwise and ill try to find some examples like you said. also thanks for pointing that out i didnt even think of doing that.

10. Member
Join Date
Oct 2008
Posts
10
Rep Power
0
well i got it to go all the way around i wasnt using pi in my equation but now it is going way to fast and i havent found a good way to slow it down.

Java Code:
```public void advance()
{

x = 250 - xposition;
y = 250 - yposition;

double a = x / distance;

angle = Math.acos(a);

/*if(getY() >= 248){
xposition = 250 - (distance * Math.cos((angle - xChange)));
yposition = 250 - (distance * Math.sin((angle - yChange)));
}*/
//else{
xposition = 250 + (distance * Math.cos(((angle - xChange) * Math.PI)));
yposition = 250 + (distance * Math.sin(((angle - yChange) * Math.PI)));
//}

//System.out.println("angle: " + angle);
//System.out.println("x pos: " + xposition);
//System.out.println("y pos: " + yposition);

}```

11. Use Thread.sleep() in the loop that computes the dots' new postions before calling repaint() to have the new positions drawn.

12. Member
Join Date
Oct 2008
Posts
10
Rep Power
0
i tried doing that and its not that they are coming too fast sorry i wasnt clear its that now with my updated code

Java Code:
```xposition = 250 + (distance * Math.cos(((angle - xChange) * Math.PI)));
yposition = 250 + (distance * Math.sin(((angle - yChange) * Math.PI)));```
because its times PI now that the values are jumping to great so it is skipping wide spaces, there for i believe i need to divide something my somehting but ive tried many diff things and nothing has helped.

13. You haven't stated what your algorithm is, so I can't tell what your code is supposed to do.

A design could be:
Start the dot at a given angle from the zero line. For example it could be straight up (the noon position). At each time increment change the angle by a small amount and compute the new x,y for that angle and radius. draw the dot at its new position.

14. Member
Join Date
Oct 2008
Posts
10
Rep Power
0
thanks for all the replies and help! i finally figured it out i was remaking the angle everytime the dots changed positions and that was causing it to hit a wall. so once i moved the angle up to where the dots is created everything was fixed.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•