File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes problem in creating more than one objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in creating more than one objects" Watch "problem in creating more than one objects" New topic
Author

problem in creating more than one objects

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
i have 5 classes, here is:-

Cbubble.java


Cfish.java



Cpoint.java


Dynamic.java



Ctank.java



my Cpoint class has one object of my Cfish class, and one object of my Cbubble class,
but whenever i m trying to add more objects of my Cfish class, still only single fish is visible to me on the frame..
and when i m slowing down my timer to 500, fishes are visible to me but speed of movement gets slow..what is the problem in this??

one more thing i want to know i want to generate fishes at random positions, i mean fishes should appear to any random position not at any fix position, i tired that using Random class, but by using Random class, my fishes are coming randomly, i mean not their positions they are coming randomly, (ie. first time 3, second time 4, so on..).
so how can i do this also??
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
one more thing objects of Cbubble class are visible to me, as much as i am creating, no problem in that..
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> and when i m slowing down my timer to 500, fishes are visible to me but speed of movement gets slow..what is the problem in this??

now you try 250, if all visible and still slow, try 125 etc.

eventually you'll find a point where the repainting time is not enough to repaint all items,
so add back a bit more until display and movement is fine.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay, well it's working for me on 100, but the speed is still slow, compared to what i want..
so what to do if want more speed??

and what for??


one more thing i want to know i want to generate fishes at random positions

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> so what to do if want more speed??

speed is distance and time, try increasing the distance each fish/bubble travels each time the timer fires.

> one more thing i want to know i want to generate fishes at random positions

depends on your definition of 'random positions'
1)
appearing at random all over the tank (this would look a bit silly)

or

2)
more likely to be a random starting position for each fish/bubble.
in this case, all bubbles start at the same 'y', so its a matter or generating a random 'x'
similarly for fish, they have the same 'x' (unless you want them to randomly swim
left-to-right as well as right-to-left, then you have 2 * 'x's to factor in).

if you don't want the fish/bubbles to possibly overlap each other, preset all the starting points,
e.g. for bubbles you might want them to start on each 10th x-point, and if you wanted no
duplicates, you could add each of those to an arraylist (so it would contain 10,20,30,40,50 etc),
then you'd pick a random arraylist element for the starting position, remove that selected number
from the arraylist, then when the bubble reaches the top/disappears, add back its starting 'x' so
that starting 'x' is once again available for random selection.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

speed is distance and time, try increasing the distance each fish/bubble travels each time the timer fires.


You mean i should try to increase distance of x and y positions of fishes??
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> You mean i should try to increase distance of x and y positions of fishes??

not sure what your numbers are, so the numbers here are just an example:

if your fish is swimming left-to-right and starts at 100,100, you start a timer
so it is repainted every 100ms adding 2 to the x, so the fish is then repainted
at 102,100 then 104/100 then 106/100 etc

instead of adding 2 to 'x', add 4 and see how it goes, then try adding 6 etc.

if you add too much extra, the fish will have a 'hopping' effect, so then bring it back.

(if swimming right-to-left, the 'x' numbers are deducted)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay my movement speed gets faster now..


more likely to be a random starting position for each fish/bubble.
in this case, all bubbles start at the same 'y', so its a matter or generating a random 'x'
similarly for fish, they have the same 'x' (unless you want them to randomly swim
left-to-right as well as right-to-left, then you have 2 * 'x's to factor in).


fishes are not moving from left to right only, they are moving throughout the frame, left to right and top to bottom..

one more thing i want to know, i added a button to my dynamic class, and on action listener of that button i want all the fishes should die, that button is oxygen level, for that i m doing like this: on button action listener i am setting x,y,dx,dy of fish class to zero, is it work by doing only this, or i have to do somthing else??

i am trying like this:


public void actionPerformed(ActionEvent e) {
Cfish fish = new Cfish();
if(e.getActionCommand().equals("button"))
{
fish.x = 0;
fish.y = 0;
fish.dx = 0;
fish.dy = 0;
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> x,y,dx,dy of fish class to zero, is it work by doing only this, or i have to do somthing else??

doesn't matter what x,y is, if they have no size i.e. dx = 0 and dy = 0, they'll disappear,
but you should stop the timer - pointless repainting 0-size fishes at a new x,y
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

but you should stop the timer - pointless repainting 0-size fishes at a new x,y


You mean to say, i should stop the time by creating Cpoint class object, as well as repaint that, isn't it?
i tried like this, but doesn't work..


Cfish fish = new Cfish();

Cpoint c = new Cpoint();
if(e.getActionCommand().equals("button"))
{
fish.dx = 0;
fish.dy = 0;
c.timer.stop();
c.repaint();
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> on button action listener i am setting x,y,dx,dy of fish class to zero, is it work by doing only this, or i have to do somthing else??

if it's working OK like this, then stick with it.

the only addition is to stop the timer.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
no it's not working like this...
and i m stopping the timer also..


c.timer.stop();

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
oops, I read
is it work
as
it is work

here's your earlier posted code, with a few changes (noted), of one way you could do it.
a number of the changes are just to use the 'test' button already there, these are not
required if you include a button in Cpoint.

this will fit into a single .java file, compile/run and see if it's what you're after.

>
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
yap, this is exactly i was asking...
thank you very much...
i got it what you did..
one more thing i want to know, i want to give it a natural look, i mean my these fishes are looking like balloons not fishes,
what can i do for this, that makes it like a natural fish aquarium?

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i want to give it a natural look, i mean my these fishes are looking like balloons not fishes,

one way might be to search the internet for fish images you like, then, instead of drawing your fish,
you add the images to JLabels. Add the labels to a JPanel (set as null layout), then it's similar to
the drawing of the fish - you simply use the timer to reset the x,y of each label then repaint(),
the bubbles can remain as they are.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay i will try this as you suggest..
thank you..
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
i have created JLabel and ImageIcon in my Cfish class


ImageIcon image = new ImageIcon("c:\\Fish.jpg");
public JLabel label=new JLabel(image);


but i m getting confused how do i add this to my Cpoint class.??
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I posted a bit hasty in suggesting using JLabels, given the way you're setup passing the grahics object around.

I think it might be easier to use that grahics object to draw the fish image

comment out your old Cfish class (to you can use it again later, if needed)
here's the modified Cfish class, with just a few of changes, the major change being in DisplayFish()



and to create your Cfish in Cpoint
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay working now thanks, but why you put this:

radius = fishImg.getWidth(null)/2;

i mean null?

one more thing i want to ask, how to some water level, how to add water at some level here, right now i m using some blue background, instead of that or onto that i want to add some water level and some motion on that (ie. some type of fluctuate in water).
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
is anything which can i do with bubbles also to make them more natural??
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> radius = fishImg.getWidth(null)/2;
> i mean null?

you need to show your investigative skills here:

fishImg is an Image
getWidth(null) is a compiler-happy method signature

so, read the relevant apidocs to understand what/why the argument/parameter is/can be null

> one more thing i want to ask, how to some water level, how to add water at some level here,
> right now i m using some blue background, instead of that or onto that i want to add some
> water level and some motion on that (ie. some type of fluctuate in water).

instead of having your fish tank occupy all of the space on the right-hand-side, use the layoutManager
to create an 'air' space above (JPanel with suitably colored background) and in paintComponent() you
could draw a bunch of side-by-side polygons (I think that's what you need for triangles), different sizes,
different shapes, moving left-to-right or right-to-left via a timer.

> is anything which can i do with bubbles also to make them more natural??

a bubble is/'always will be' round, you could experiment wih images, or different sizes with differing
internal colors from white to semi-transparent to clear.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
can i add paintComponent directly to my container..
i mean i created a class,

class Cair extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("Hello", 20, 20);
}
}


In the Ctank i m calling it like this:


Cair ca= new Cair();
c.add(ca, BorderLayout.EAST);


but not visible to me..
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the layoutManager uses the panel's preferredSize to allocate space.
because the panel has no components, it's preferredSize is 0,0

you need to set the preferredSize of the panel.

because you're adding it to east, the size of the height doesn't really matter,
but the width does, so make it say 50 and see how it looks.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay, working now...
can i do some more animations in fishes,
i mean in fins of fishes can i add animation??

also i found some ripple effects for water or some more water effects, when i was surfing on internet, could you help me in that also, i got some working/running applets on internet, but don't know how to do that..
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i mean in fins of fishes can i add animation??
>also i found some ripple effects for water or some more water effects

it's not something I've done, but I'm sure it's do-able.

probably time to start a new topic about animation, and hopefully get the
attention of those who really know that stuff.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
As you told me in this thread this:




i did like this:


but what i m thinking to do is when i press button remove oxygen, all the fishes should go at the bottom of the frame??
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but what i m thinking to do is when i press button remove oxygen, all the fishes should go at the bottom of the frame??

you would need to keep the timer running, but as there are no more bubbles,
the x position of the fish no longer changes, only the y position increases so
the fish gradually 'fall' to the bottom. you would need to keep a count of the
number of fish that have reached the bottom, so that when they're all there
you stop the timer.

this could even be a second timer (to save confusion with the 'swimming' timer)

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
well i got the logic and i tried like this also, but what the problem i faced is to use x and y of my Cfish class,
i mean how do i refer or use positions of my Cfish class, i know it's a silly question i m asking but i tried it and doesn't work for me..
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i mean how do i refer or use positions of my Cfish class

your Cfish class has x,y positions which you change in movementFish().

when the bubbles stop, movementFish() should no longer change 'x', so the
fish no longer moves left or right, and changing 'y' should only be to increase
'y' so the fish gets lower in the tank.

if the fish is already swimming down, y continues to increase until the bottom
is reached. If the fish is swimming up, i.e. y decreasing, then this needs to go the other way.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
yes this i got, what i am asking is i have to use it in my ActionListener of my Cdynamic class,
that how do i use??
i mean here:


how do i use x and y of my Cfish class..?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
at the moment, is the fish are dead, they disappear,
so that bit of code is now useless, unless you want
them to eventually disappear, but I'm reading this
that you just want them on the bottom (lifeless).

you're going to have to rebuild movementFish() so
it checks whether there are bubbles - if bubbles,
continue doing what it is doing, but if no bubbles,
then the movement changes to that described earlier,
and you will need to keep a count of the number of
fish that are already on the bottom so that when
they're all there is when the timer is to stop.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay, suppose i checked it, if bubbles are there or not, and if not than i need to eventually increase the y, until fishes reach to the bottom, but my question is i will increase my 'y' in ActionListener of my Cdynamic class, that how i do is my question??
i mean how do i use y in Cdynamic simply by using 'Objectname.y' ??

and one more thing the code you told me in the earlier post which makes the fishes and bubbles disappearos click of fishDead button, is working when i am using only one class, i mean when i m just copying the earlier code in a single file, but when i am again distributing the code into my classes, that time when i m clicking on fishDead button, fishes and bubbles are not disappear, but they are just stopping wherever they are..
it should not happen, but it's happening..
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> ...increase my 'y' in ActionListener of my Cdynamic class, that how i do is my question??

you don't do it there. re-read my previous post, you have to change movementFish().

at present, movementFish() changes the x position. you need to change this so it only
recalculates a new x position if there are bubbles, if not, do nothing.

similarly for y, if bubbles, y keeps going up or down, but if no bubbles, y can only increase
(i.e. go down, sinking) until bottom is reached
 
Consider Paul's rocket mass heater.
 
subject: problem in creating more than one objects