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

Balls are not moving

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello here is code to move multiple balls, but my balls are not moving...

Cball.java


Cpoint.java


Main.java


well when i m removing the ball object from while loop and putting it into try block inside while, ball is moving but it leaving blue background wherever it is moving..
and also multiple balls are not working..
can anyone tell me what i m doing wrong??
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ball is moving but it leaving blue background wherever it is moving..





Get rid of the "s" in "paintComponents".
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
great, it's working now..
but again not able to get multiple balls, although i have created two objects of Cball class and calling paint and movement method..
can you please help me this also...
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also when i m putting my 'public static void main()' into Cpoint.java, and getting rid pf my Main class, it showing me multiple balls and but balls are not moving..
what i m doing wrong here??
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from your 2nd last post
> but again not able to get multiple balls

works fine using a timer
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please help me, how do i use timer here??
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also i updated my code like this:

Cball.java



point.java


what is happening here, frame moving a green ball, and when i m commenting ball3(green ball), it showing the ball2(blue ball), and when i m commenting ball2, it showing red ball, but not showing all the three balls same time, and also i m displaying the current position for all three balls, it correctly showing me the positions, although positions are updated continually than why balls are overlapping each other, not showing at different-2 positions...?

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> can you please help me, how do i use timer here??

you were given a link to the Swing Timer tutorial in your other thread.

you ignored that, so there'es not a lot we can do for you.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i was not ignored that, even i never ignored any of reply from javaranch, but i don't understand where should i use that, i mean instead of what i use timer in my code..?
thanks..
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i used timer class like this, and i replaced Cpoint.java like this:

Cpoint.java



but still not working.....
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> but still not working.....

look at the code - what do you expect that code (as written) to do?

a timer does 'something' at a given interval, what does yours do?

go back to the tutorial, follow the examples, run them.
search this forum for timer examples

(hint: the code in your run() does nothing, and it shouldn't be there)
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, i got it, i need to put the run method into actionPerformed because Timer class required second parameter as actionPerformed, isn't it??
i tried like this also, but result is still same.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is from my last post
"a timer does 'something' at a given interval, what does yours do?"

again, look at what yours does:
while(true) - are you serious, you want an infinite loop to be repeated every 10/1000 of a second? --> new Timer(10, this);

the timer does something once, and once only, then repeats it at a given interval
that something is to a) change the position of the ball b) repaint the ball at the new position.

and all of this is just repeating what I posted in your other thread


you give x,y initial values of (say) 10,100
then you start a timer to change x and/or y
and at the same time call panel.repaint()
which will 'repaint' the ball at its new location.

 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i was also doing this, i have already set the value of x and y in my Cpoint class, also in my above posts, and in the movement method i m also changing the values of x and y, and calling the movement methods in actionPerformed, and as of now i want to move the balls for forever, therefore my loop goes infinite times..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"you want an infinite loop to be repeated every 10/1000 of a second? --> new Timer(10, this); "

looks like I need to explsin the above a bit better:
the infinite loop will be re-created every
10/1000 of a second. In theory that means
in one second you will have 100 separate
infinite loops running, but it will probably
freeze/lock up at some point.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fine, it will be freeze, at some point, but atleast the balls should move once in this loop.
i also tried the while loop with some static values, but dosen't work..
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got you Michael Dunn, there will be no while loop, i done it and it's working for me...
thank you very much....
here is the code:

Cpoint.java



one last thing i want to know, why my previous code was not working, when i was using thread, although that code was perfectly working with Applets....

Thanks again...
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to know one more thing, my balls are collide to each other, how do i remove the collision, so my balls should not touch to each other...??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
each ball is drawn in a rectangle, so you know where the center is.

draw ball1, calculate central point
before drawing ball2, calculate the central point of its new position
check the distance between the 2 points
if less than the combined radius of the 2 balls, they will collide,
so determine/set new x,y for ball2 and repeat until no collision.

after ball2's position is set, and the ball is drawn, repeat above for ball1's new position,
checking whether ball1 is going to collide with ball2's drawn position.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how my balls are drawn in rectangle, and further i m going to convert my balls into fishes, and can you explain little bit more..?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> and can you explain little bit more..?

there's not a lot to it, so what part are you having trouble with

calculating central point
calculating distance between 2 points
adding the 2 radius
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well in my Cpoint class, where i m drawing balls,
so as you said:

int newx;
int newy;
int cp;
ball = new Cball(10,20);
newx = ball.getx();
newy = ball.gety();
cp = newx+newy/2;
ball1 = new Cball(40,80);
// here do i need to again calculate new x and y for ball1???

and is it what you said??


 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have a rectangle (100,100,200,200)
x = 100
y = 100
width = 200
height = 200

and you calculate that the central point is x(100) + y(100) /2 = 100?

draw it on paper - what do you come up with?

 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i got a formula from the internet and i tried like this to calculate the midpoints for first ball....

ball = new Cball(10,100);
getradius = ball.getRadius();
newx =ball.getX();
midx = (newx+(getradius*8))/2;
newy = ball.getY();
midy = (newy+(getradius*8))/2;

and i am drawing ball like this:


g.fillOval(x-radius*7, y-radius*1, radius*8,radius*2);
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the circle is in a rectangle/square,
wouldn't the central point be simply
half way along then half way down.

again, draw it on paper, to get a visual idea
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay i draw it on paper,
let say width = 100, height = 50, x=0, y=0..

so midpoint should be something like this:

If(x==(ball.getWidth/2))&&(y==(getHeight/2))
//it is a midpoint


 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a midpoint is a point. it has an x and a y.
it is not an if().

also, don't use 0,0 as your starting point,
it could cause (later) confusion.

on paper calculate the midX and midY of 100,100,200,200
now write a program to print out midX and midY,
and see if they agree
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay the x and y is 150,150 in paper
used this logic in my code:

ball = new Cball(100,100);
getradius = ball.getRadius();
newx =ball.getX();
newy = ball.getY();
newwidth = 200;//getradius*8;
newheight = 200;//getradius*2;
midx = (newx+newwidth)/2; //width
midy = (newy+newheight)/2; //height
System.out.println(""+midx+"\t"+midy);

it giving me same 150 and 150...is that okay??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> it giving me same 150 and 150...is that okay??

draw a larger rectangle 800,600 (to represent a frame)
inside that frame draw an other ectangle at 100,100,200,200
now put a dot at 150,150
is the dot in the center of 100,100,200,200?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
upps, dot is not in center, and when i m putting a dot at 200,200, then it's in center, but don't know how..?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x(100) + width(200) = 300

x + half-width = ?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:x(100) + width(200) = 300

x + half-width = ?




ya now it's 200, so now:

newx=x+halfwidth
newy = y+halfheight.
isn't it??

but if i m trying for this:

g.setColor(Color.GREEN);
g.drawRect(300, 200, 150, 250);
g.setColor(Color.RED);
g.drawLine(425, 275, 425, 275);

now it's not in center...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> now it's not in center...

it might be if you swap the 150,250

g.drawRect(300, 200, 150, 250);

 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i swapped both of them, but still it's not in center...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> i swapped both of them, but still it's not in center...

works perfectly for me, you'll have to dig out your pencil and paper again to work out where you've made an error.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but as i know i m not doing anything wrong, i tried for few more combinations:


the commented are not coming in the center, and uncommented are in center...?

 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more thing i want to know, when i m trying to set a background image here it overlap my background to my Cpoint class, i mean i m not able to add two components to ContentPane, only one thing is visible at a time, either Cpoint class or background...

i tried like this:

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> //g.drawLine(375, 325, 375, 325); //this is not in center

center of what? you have 5 drawRect()'s before it
honestly, this is so basic, if you haven't grasped it by now, dump the project and try another.

> frame.getContentPane().add(panel);
> frame.getContentPane().add(point);

you need to read the apidocs
lookup what the default layoutManager is for the frame's contentPane
then lookup the default behavior of that layoutManager.

this is a separate issue to 'balls not moving' / colliding,
so should have been in its own thread
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i have 5 rect and i draw 5 dots, all are correspond to the rect, i mean the first rect has the first dot in the center as per the formula and so on...
and center i mean if i have 5 rects and 5 dots then each dot correspond to the rect is comes at the center of that rectangle..
and sorry but i m not going to dump this project, i m going to do this anyhow...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, but this is my final post in this thread, it is just going around and around in circles

couple of posts ago you had this

but if i m trying for this:

g.setColor(Color.GREEN);
g.drawRect(300, 200, 150, 250);
g.setColor(Color.RED);
g.drawLine(425, 275, 425, 275);

now it's not in center...



and my reply

it might be if you swap the 150,250

g.drawRect(300, 200, 150, 250);



then you post (the 2 relevant lines only)

g.drawRect(300, 200, 250, 150);
g.drawLine(375, 325, 375, 325); //this is not in center



so, why has
g.drawLine(425, 275, 425, 275);

changed to
g.drawLine(375, 325, 375, 325);

like I said, just going around in circles
 
reply
    Bookmark Topic Watch Topic
  • New Topic