Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Graphics speed?

  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Graphics speed?

    I've been working on an artificial life program that's rather graphics intensive - all the organisms move around and eat each other. It's been working great until now - when the JFrame loads, it's all white for a while, then finally loads the black background, and the animation is very jumpy. Does anyone know why this would happen? I just updated my jre and jdk, but it didn't help.

  • #2
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Could you provide us some code?

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yeah, this is my graphics panel:

    Code:
       import javax.swing.*;
    	import java.awt.*;
    	import java.awt.event.*;
    	import java.awt.image.*;
    	import java.util.ArrayList;
    	import java.util.Iterator;
    	
        public class LifePanel extends JPanel
       {
       //constants
    		private static final int FRAME1 = 550;
          private static final int FRAME2 = 420;   
          private static final Color BACKGROUND = new Color(Color.BLACK);
          private BufferedImage myImage;
          public static Graphics myBuffer;
          private Timer t;
    		public static ArrayList<Organism> orgs = new ArrayList<Organism>();
    		public static ArrayList<Organism> unusedorgs = new ArrayList<Organism>();
    		public static ArrayList<Thread> unusedthreads = new ArrayList<Thread>(); 
    		public static ArrayList<Thread> threads = new ArrayList<Thread>(); 
           public LifePanel()
          {
             myImage =  new BufferedImage(FRAME1, FRAME2, BufferedImage.TYPE_INT_RGB);
             myBuffer = myImage.getGraphics();
             myBuffer.setColor(BACKGROUND);
             myBuffer.fillRect(0, 0, FRAME1, FRAME2);
    		   t = new Timer(1, new Listener());
    		   t.start();
    			for(int i = 0; i < 10; i++) {
    				unusedorgs.add(new Species1());
    			}
    			orgs.add(new Species1((int)(Math.random()*400),(int)(Math.random()*400),100,(int)(Math.random()*100),(int)(Math.random()*50)+50,(int)(Math.random()*50)));
    			orgs.add(new Species1((int)(Math.random()*400),(int)(Math.random()*400),100,(int)(Math.random()*100),(int)(Math.random()*50)+50,(int)(Math.random()*50)));
    			for(int i = 0; i < 2; i++) {
    				threads.add(new Thread(orgs.get(i)));
    				threads.get(i).start();
    			}
    			for(int i = 0; i < 10; i++) {
    				unusedthreads.add(new Thread());
    			}
          }
    		public static void replenish() {
    			for(int i = 0; i < 20; i++) {
    				unusedthreads.add(new Thread());
    			}
    			for(int i = 0; i < 10; i++) {
    				unusedorgs.add(new Species1());
    			}
    		}
           public void paintComponent(Graphics g)
          {
             g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
          }
           private class Listener implements ActionListener
          {
              public void actionPerformed(ActionEvent e)
             {
    				try{
    				int avg = 0;
    				int spec1 = 0;
    				int spec2 = 0;
             	myBuffer.setColor(BACKGROUND);
             	myBuffer.fillRect(0, 0, FRAME1, FRAME2);
    				myBuffer.setColor(new Color(255,255,255));
    				myBuffer.drawLine(0,400,400,400);
    				myBuffer.drawLine(400,0,400,400);
    				for(int i = 0; i < LifePanel.orgs.size(); i++) {
    				LifePanel.orgs.get(i).draw(myBuffer); //draw organism
    				//get stats
    				avg += LifePanel.orgs.get(i).getSize(); 
    				if(LifePanel.orgs.get(i).getSpecies() == 1) {
    				spec1++;
    				}
    				else if(LifePanel.orgs.get(i).getSpecies() == 2) {
    				spec2++;
    				}
    				}
    				//stat bars
    				myBuffer.setColor(Color.WHITE);
    				if(LifePanel.orgs.size() > 0) {
    				avg = avg / LifePanel.orgs.size();
    				}
    				myBuffer.fillRect(400,400-(avg*4),50,avg*4);
    				myBuffer.setColor(Color.GREEN);
    				myBuffer.fillRect(450,400-(spec1*4),50,spec1*4);
    				myBuffer.setColor(Color.BLUE);
    				myBuffer.fillRect(500,400-(spec2*4),50,spec2*4);
    				myBuffer.setColor(new Color(255,255,255));
    				myBuffer.setFont(new Font("Times New Roman",Font.BOLD,15));
    				myBuffer.drawString("SIZE",400,415);
    				myBuffer.drawString("SPEC1",450,415);
    				myBuffer.drawString("SPEC2",500,415);
                repaint();
    				}
    				catch(Exception exc) {
    					System.out.println("Graphics Fail");
    				}
             }
          }
       }
    This is the draw method for the organisms:

    Code:
    public void draw(Graphics myBuffer) {
    		myBuffer.setColor(myColor);
    	   myBuffer.fillOval((int)(x - (size/10)), (int)(y-(size/10)), (int)(size/5), (int)(size/5));
    		myBuffer.setColor(Color.WHITE);
    		myBuffer.setFont(new Font("Monospaced", Font.BOLD, 15));
    		myBuffer.drawString(Integer.toString(-id),x,y);
    	}
    I don't really think the code is the problem though. I ran it last night, and it didn't have any problems. Thousands of organisms were able to simultaneously function, and there wasn't an issue until some 20,000 had been initialized. Then it started throwing OutOfMemoryException. But I haven't been able to get it to run well since then...the frame takes forever to load, and it's using 100% of the CPU before the first generation reproduces. Do you think it could have anything to do with those OutOfMemoryExceptions?

  • #4
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    There could be two different problems:

    1.) instead of
    repaint();

    it's almost always better to use
    invalidate();
    validate();
    repaint();

    2.) OutOfMemoryException: That's a serious issue and you must deal with it. On StackOverflow I've seen a discussion in this topic, here's the link: http://stackoverflow.com/questions/5...yerror-in-java

    I think you should give more memory to the JVM.

  • #5
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks so much - it works a lot better now, especially when I comment out the line for drawing organisms and just look at the stat bars. Thanks again.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •