Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,238 members, 7,807,793 topics. Date: Wednesday, 24 April 2024 at 07:24 PM

Help With Java Programing: Changing Motion Of A Particle In A Jpanel - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With Java Programing: Changing Motion Of A Particle In A Jpanel (1475 Views)

Android Programing And Web Programing, Which One Is Better? / Creating A Proxy Server With Java. / Learning To Program With Java by the Fulaman (2) (3) (4)

(1) (Reply)

Help With Java Programing: Changing Motion Of A Particle In A Jpanel by Nobody: 11:38am On Feb 25, 2016
I wrote a program in in which I created a Java.awt.Point object. and I set the co-ordinates to change with respect to time with a certain function. basically, I used a javax.swing.Timer object. everytime the timer fires, a int variable increases by 1, and the new value of the int is fed into a function, which is then used to change the point's location on the Panel. I then used a graphics object to paint a circle on that point. the resulting effect is that the user sees a ball moving across the screen.

the problem is that, I want the co-ordinates to change their motion when they reach a certain point. for instance if the ball was falling before, when it gets to a certain y co-ordinate, it should start moving horizontally to the right. Here's what my code looks like. but it work. the ball just disappears when it's supposed to change motion.



//all needed stuff are imported here


public class Stopwatch implements ActionListener{

Timer timer = new Timer(20,this);
Graphpaint graphpaint = new Graphpaint();
Point point = new Point(0,0);
Point groundpoint = new Point(0,314);
public double ticks = 0;
public double initialspeed = 70;
public double angle = 40;
double Radian = Math.toRadians(angle);
double Sin = Math.sin(Radian);
double Cos = Math.cos(Radian);

public Stopwatch(){
//I create the JFrame here and add the graphpaint JPanel
}

public static void main(String[]args){
stopwatch = new Stopwatch();
}

public void actionPerformed(ActionEvent arg0) {
// the graphpaint is an object from the graphpaint class. where i paint the ball on to the point.x and point.y
graphpaint.repaint();
ticks+=5;

double ticker = ticks/50;
point.x = (int)(initialspeed * Cos * ticker);
point.y = (int)((initialspeed * Sin * ticker) - (0.5 * 10 * Math.pow(ticker, 2)));
groundpoint.y +=0;

if(point.y <= 0 && ticks !=0){
point.setLocation(point.x, groundpoint.y);
graphpaint.repaint();
/*this is where it all breaks down. when the point.y becomes zero,
the ball should start moving under point.x ,
and groundpoint.y,which is a constant. but instead, the ball just disappears.*/
}

}
}




}



If there is anything else needed from the original code, kindly say. this is an experiment.
Re: Help With Java Programing: Changing Motion Of A Particle In A Jpanel by Booyakasha(f): 4:42pm On Feb 26, 2016
Teempakguy:
I wrote a program in in which I created a Java.awt.Point object. and I set the co-ordinates to change with respect to time with a certain function. basically, I used a javax.swing.Timer object. everytime the timer fires, a int variable increases by 1, and the new value of the int is fed into a function, which is then used to change the point's location on the Panel. I then used a graphics object to paint a circle on that point. the resulting effect is that the user sees a ball moving across the screen.

the problem is that, I want the co-ordinates to change their motion when they reach a certain point. for instance if the ball was falling before, when it gets to a certain y co-ordinate, it should start moving horizontally to the right. Here's what my code looks like. but it work. the ball just disappears when it's supposed to change motion.



//all needed stuff are imported here


public class Stopwatch implements ActionListener{

Timer timer = new Timer(20,this);
Graphpaint graphpaint = new Graphpaint();
Point point = new Point(0,0);
Point groundpoint = new Point(0,314);
public double ticks = 0;
public double initialspeed = 70;
public double angle = 40;
double Radian = Math.toRadians(angle);
double Sin = Math.sin(Radian);
double Cos = Math.cos(Radian);

public Stopwatch(){
//I create the JFrame here and add the graphpaint JPanel
}

public static void main(String[]args){
stopwatch = new Stopwatch();
}

public void actionPerformed(ActionEvent arg0) {
// the graphpaint is an object from the graphpaint class. where i paint the ball on to the point.x and point.y
graphpaint.repaint();
ticks+=5;

double ticker = ticks/50;
point.x = (int)(initialspeed * Cos * ticker);
point.y = (int)((initialspeed * Sin * ticker) - (0.5 * 10 * Math.pow(ticker, 2)));
groundpoint.y +=0;

if(point.y <= 0 && ticks !=0){
point.setLocation(point.x, groundpoint.y);
graphpaint.repaint();
/*this is where it all breaks down. when the point.y becomes zero,
the ball should start moving under point.x ,
and groundpoint.y,which is a constant. but instead, the ball just disappears.*/
}

}
}




}



If there is anything else needed from the original code, kindly say. this is an experiment.

Java is not really my thing, at least for now... but here could be where your problem lies.. "if(point.y <= 0 && ticks !=0)" you know how operator precedence works with the && Operator.
Re: Help With Java Programing: Changing Motion Of A Particle In A Jpanel by Nobody: 3:00pm On Feb 27, 2016
Booyakasha:


Java is not really my thing, at least for now... but here could be where your problem lies.. "if(point.y <= 0 && ticks !=0)" you know how operator precedence works with the && Operator.
Thanks for the input. it turns out my entire program was garbage. the co-ordinates i specified are outside the visible window. cheesy cheesy sad sad

I've solved the problem. but thanks anyway.

(1) (Reply)

Print ("how Long Will It Take To Be An Expert Python Programmer") / Please Help, Which Is The Best Computing Skill To Attain? / Can Someone Learn PHP In Four Contact

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.