Results 1 to 14 of 14
Thread: Help with canvas
 10282008, 07:55 PM #1Member
 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))); } public void advance() { 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; } }
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.
 10282008, 07:55 PM #2Member
 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); window.add(canvas); run = new JButton("Run"); run.addActionListener(this); window.add(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); d.advance(); g.setColor(d.getColor()); g.fillOval(d.getX(), d.getY(), 6, 6); } }while(time <= (StartTime + 10000)); } public static void main(String[] args) { new SpinningDots(); } }
 10282008, 09:28 PM #3the dots all stop when they hit about the 9 o clock position
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?

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
 10282008, 10:17 PM #5Member
 Join Date
 Oct 2008
 Posts
 10
 Rep Power
 0
Java Code:x pos: 169.97101431583582 y pos: 250.00195732369585 angle: 1.9864818412147502E5 x pos: 169.97101430261705 y pos: 250.00100910198697 angle: 8.016340004459174E6
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
 10282008, 11:10 PM #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.
 10292008, 12:20 AM #7Member
 Join Date
 Oct 2008
 Posts
 10
 Rep Power
 0
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)));
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
 10292008, 01:49 AM #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.
 10292008, 03:56 AM #9Member
 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.
 10292008, 05:10 AM #10Member
 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); }
 10292008, 01:53 PM #11
Use Thread.sleep() in the loop that computes the dots' new postions before calling repaint() to have the new positions drawn.
 10292008, 05:57 PM #12Member
 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)));
 10292008, 07:45 PM #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.
 10292008, 08:08 PM #14Member
 Join Date
 Oct 2008
 Posts
 10
 Rep Power
 0
Similar Threads

Using a Canvas
By a_klasanov in forum CLDC and MIDPReplies: 3Last Post: 12082008, 10:36 AM 
Canvas Example
By Java Tip in forum SWT TipsReplies: 0Last Post: 07252008, 02:22 PM 
Using SWT Canvas
By Java Tip in forum Java TipReplies: 0Last Post: 01082008, 09:06 AM 
Canvas question
By christina in forum Advanced JavaReplies: 1Last Post: 08032007, 07:21 PM
Bookmarks