java - My treeset seems to have a size value of 1, where it should be undefined -


for reason, list allow 1 element. when delete element, allows creation of another, cannot ever have 2 circles @ once.

here applet:

import java.applet.applet;import java.awt.graphics;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.util.iterator;import java.util.random;import java.util.set;import java.util.treeset; import javax.swing.timer; public class game extends applet implements actionlistener{     int b_width=500, b_height=500;     int counter=0;     random gen=new random(); timer t; set circles;      public void init() {         setsize(b_width, b_height); circles = new treeset();      }     public void circleconstr(){         circle cl=new circle(b_width,(int)(gen.nextdouble()*b_height), gen.nextint()%190, -1);         circles.add(cl);         circle cr=new circle(0, (int)(gen.nextdouble()*b_height), gen.nextint()%190, 1);         circles.add(cr);     } 

so there ^^^ should have made 2 circles, eer makes one.

    public void circlemover(set circles){         iterator<circle> i=circles.iterator();         while(i.hasnext()){            circle c=i.next();c.move();            if(c._x>b_width+b_width/5||c._x<b_width/5||c._y>b_height+b_height/5||c._y<-b_height/5){i.remove();}         }     }     public void paint(graphics g){         iterator<circle> i=circles.iterator();         while(i.hasnext()){             i.next().paint(g);         }      }     public void start(){            t = new timer(100, this); //calls actionpreformed every .01 seconds(10 miliseconds)        t.start(); //starts timer        }   

up there timer , painter

    @override     public void actionperformed(actionevent e){         if(counter%5==0){             circleconstr();             } 

additionally here ^^^^, should creating new circle every .5 seconds, instead waits until first circle killed.

        counter++;         circlemover(circles);         repaint();     } } 

here circle class:

import java.awt.color;import java.awt.graphics;import      java.util.random;  public class circle implements comparable {     int _x, _y, _yb, _sp, multiplier; double _m,  _radiusrandom gen=new random();     int r=(int)(gen.nextdouble()*255), b=(int)(gen.nextdouble()*255), g=(int)(gen.nextdouble()*255);     public circle (int x, int y, int range, int multiplier){         _x=x;_y=y;         _radius=(int)(gen.nextdouble()*range); if(_radius<0){_radius*=-1;}         _m=gen.nextdouble()+gen.nextint()%2+.5;_sp=(int)(gen.nextdouble()*7)+1;_sp*=multiplier;     }     public void paint(graphics h){         color color= new color (r, b, g);          h.setcolor(color);         h.filloval(_x, _y, (int)_radius, (int) _radius);     }     public void move(){         _x+=_sp; _y=(int)(_m*_x+_yb);     }      @override     public int compareto(object t) {         return 0;     } } 

a set not allow more 1 element compares equal. use of treeset have relying on calling compareto.

[a] treeset instance performs element comparisons using compareto (or compare) method

you have compareto defined in circle. however, compareto method returning 0, all circle objects compare equal each other, , 1 make in treeset.

you need define compareto method.

in addition, may want use generics on treeset class , implementing generic form of comparable in circle, e.g.:

set<circle> circles; 

and

circles = new treeset<circle>();  

then in circle:

public class circle implements comparable<circle> { 

and

@override public int compareto(circle t) {     // return -1, 0, or 1 if object <, equal, or > "t" } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -