Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Scheduler Issue

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scheduler Issue

    I do not know how to do what I am trying to do. I have an array of images that move across the screen. What I want to do is periodically increase this array and add a new element to it. The code from the 2 classes below shows how I attempted to do that. I was reserved doing it this way as I figured it wouldn't like multiple things trying to access the same array. When I ran the program it crashed when it tried to increase the array size, so my assumption was correct. My issue is, I am rusty on my Java and cannot think of a way to get around this.

    import java.util.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.imageio.*;
    import java.lang.Math;
    import java.io.*;
    import java.util.concurrent.*;
    import java.util.concurrent.TimeUnit.*;
     
    public class CellAttack extends JPanel
    {
     
    public static Rectangle screen, bounds;
    public static JFrame frame;
    public static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    public static RedBloodCell[] redCellArray = new RedBloodCell[40];
    public static interactiveParticle[] IPArray = new interactiveParticle[2];
    public static Random RG = new Random();
    public BufferedImage background = null;
    public static CellAttack panel;
     
      public CellAttack()
      {
        super();
        screen = new Rectangle(0, 0, 800, 600);
        bounds = new Rectangle(0, 0, 800, 600); 
        for(int i = 0; i < redCellArray.length; i++)
        {
            redCellArray[i] = new RedBloodCell(RG.nextInt(bounds.width+1),RG.nextInt(bounds.height-130)+50,(RG.nextInt(5)+1),bounds.width,bounds.height-130);
        }
        for(int m = 0; m < IPArray.length; m++)
        {
            IPArray[m] = new interactiveParticle(RG.nextInt(bounds.width+1),RG.nextInt(bounds.height-130)+50,(RG.nextInt(5)+1),bounds.width,bounds.height-130,RG.nextInt(3)+1,panel);
        }
        frame = new JFrame("Cell Attack");
        try
        {
            background = ImageIO.read(new File("background.png"));
        }
        catch(IOException e){}
      }
     
      public void paintComponent(Graphics g)
      {
        bounds = g.getClipBounds();
        g.clearRect(screen.x, screen.y, screen.width, screen.height);
        g.drawImage(background,0,0,this);
        for(int k = 0; k < redCellArray.length; k++)
        {
            g.drawImage(redCellArray[k].img, redCellArray[k].x, redCellArray[k].y, this);
        }
        for(int n = 0; n < IPArray.length; n++)
        {
            g.drawImage(IPArray[n].img, IPArray[n].x, IPArray[n].y, this);
        }
      }
     
      public void increaseIPArray()
      {
          interactiveParticle[] tempIPArray = new interactiveParticle[IPArray.length+1];
          for(int q = 0; q < IPArray.length; q++)
          {
              tempIPArray[q] = IPArray[q];
          }
          tempIPArray[IPArray.length] = new interactiveParticle(bounds.width,RG.nextInt(bounds.height-130)+50,(RG.nextInt(5)+1),bounds.width,bounds.height-130,RG.nextInt(3)+1,panel);
          IPArray = tempIPArray;
      }
     
      public static void main(String arg[])
      {
        panel = new CellAttack();
     
        Runnable mover = new Runnable()
        {
          public void run()
          {
            for(int j = 0; j < redCellArray.length; j++)
            {
              redCellArray[j].move();
            }
            for(int p = 0; p < IPArray.length; p++)
            {
              IPArray[p].move();
            }
            frame.repaint();
          }
        };
        scheduler.scheduleAtFixedRate(mover, 0, 20, TimeUnit.MILLISECONDS);
     
        panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.frame.setSize(panel.screen.width, panel.screen.height+22);
     
        panel.frame.setContentPane(panel);
        panel.frame.setVisible(true);
      }
    }

    import java.util.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.imageio.*;
    import java.lang.Math;
    import java.io.*;
     
    public class interactiveParticle
    {
        public int x, y, width, height, Xstart, Yheight, redCellNum;
        int xVel;
        public Random RG = new Random();
        public BufferedImage img = null;
        public CellAttack CA;
     
        public interactiveParticle(int tempX, int tempY, int tempSpeed, int tempWidth, int tempHeight, int IP, CellAttack tempCA)
        {
            x = tempX;
            y = tempY;
            CA = tempCA;
            try
            {
                img = ImageIO.read(new File("virus"+String.valueOf(IP)+".png"));
            }
            catch(IOException e){}
            xVel = tempSpeed;
            Xstart = tempWidth;
            Yheight = tempHeight;
        }
     
        public void move()
        {
            x-=xVel;
     
            if(x < 0)
            {
                x = Xstart;
                y = RG.nextInt(Yheight)+50;
                xVel = RG.nextInt(4)+1;
                try
                {
                    img = ImageIO.read(new File("virus"+String.valueOf(RG.nextInt(3)+1)+".png"));
                }
                catch(IOException e){}
                CA.increaseIPArray();
            }
        }
    }
    Last edited by ercfrtz; July 6th, 2012 at 09:12 PM. Reason: rewording the question


Similar Threads

  1. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  2. A GUI issue.
    By LasOz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2012, 11:46 PM
  3. Help with my CPU Scheduler
    By HacKofDeatH in forum Object Oriented Programming
    Replies: 6
    Last Post: October 12th, 2011, 12:50 AM
  4. Help w/ my CPU Scheduler!!
    By HacKofDeatH in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2011, 12:58 AM
  5. Help with Quartz scheduler
    By ftom2 in forum Java Theory & Questions
    Replies: 0
    Last Post: March 14th, 2011, 11:28 AM