Question

Essentially, in this program bugs move around the screen eating food. The issue that I am having is that graphics "circle" repaints fine and moves around the frame, but none of the bugs are repainted (However they are drawn initially).

I've tested it with console outputs and the x and y coordinates of the bugs do update, but the graphic in the frame does not.

So my question is, why is it the x/y of the circle is updated and repainted, but the bugs are not?

(As far as I can see, I'm approaching the task of changing the bugs x/y in the same way as with the circle) (I've removed some of the things like the world getters and setters, and cut the code down a bit. The actual program has a movebug() method which handles the moving of the bugs in a correct fashion with hit detection.)

    public class Run extends JPanel implements ActionListener{

        double x, y, velX, velY, newx, newy;

        public Timer t;
        private static AWorld world1;
        ArrayList<Ellipse2D> graphicBugArrayList = new ArrayList<Ellipse2D>();


        public Run(){
         x = 150;
         y = 150;

         velX = 2;
         velY = 2;

         t      = new Timer(5, this);
         setWorld1(new AWorld());
         getWorld1().populatemap();
         AWorld.direction directionchecker;
         repaint();
         t.start();
        }


        public void paintComponent(Graphics g){

//Creating circle that move
            g.setColor(Color.BLACK);
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            Ellipse2D circle = new Ellipse2D.Double(x, y, 10, 10); 

//creating and drawing the bugs
            for(int i=0; i<world1.getNumberofbugs(); i++)
            {
                Ellipse2D obscircle = new Ellipse2D.Double(getWorld1().bugArrayList.get(i).getHPosition(), getWorld1().bugArrayList.get(i).getVPosition(), 14, 15);
                graphicBugArrayList.add(obscircle);
                g2.setPaint(Color.RED);
                g2.fill(graphicBugArrayList.get(i));
                g2.draw(graphicBugArrayList.get(i));
            }

//Drawing the circle that moves
            g2.setPaint(Color.BLACK);
            g2.draw(circle);

        }

        public void actionPerformed(ActionEvent arg0) {
            //Moving the circle 
            if(x<0 || x > 500)
            {
                velX = -velX;
            }

            if(y<0 || y > 500)
            {
                velY = -velY;
            }
            x += velX;
            y -= velY;

            //Test moving the bugs
    getWorld1().bugArrayList.get(i).setHPosition(getWorld1().bugArrayList.get(i).getHPosition()+1);


            repaint();

        }

    }
Was it helpful?

Solution

You never clear your graphicBugArrayList, but only draw the first few elements over and over. Try this in your loop:

        for(int i=0; i<world1.getNumberofbugs(); i++)
        {
            Ellipse2D obscircle = new Ellipse2D.Double(getWorld1().bugArrayList.get(i).getHPosition(), getWorld1().bugArrayList.get(i).getVPosition(), 14, 15);
            graphicBugArrayList.add(i, obscircle);
            g2.setPaint(Color.RED);
            g2.fill(graphicBugArrayList.get(i));
            g2.draw(graphicBugArrayList.get(i));
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top