Results 1 to 4 of 4
  1. #1
    vas
    vas is offline Member
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default Best approach to repaint background

    I am making an application that monitors a virtual agent in a virtual world.When the agent moves the background must be painted properly.My problem is the following:
    How can i repaint the background without calling repaint() outside Event dispatch thread?The PaintWorld class holds methods to paint the world.

    Here is my code.
    Java Code:
    
    public static void main(String[] args) {
    		
    		World.ReadWorldFromFile("C://Users/map.txt");
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();// will be executed on the event dispatch
    									// thread
    
    			}
    		});
    		)
    		Vector<Agent> agent=World.getAgents();//vector of agents objects
    		while (true) {
    		
    		for(int i=0;i<ag.size();i++)//looping through agents
    		{
    			
    			ag=agent.get(i);
               ag.move();//move the agent
    			//here i need to call repaint()
    		}
    		 
    		
    		}
    	}
    
    
    protected static void createAndShowGUI() {
    		System.out.println("Created GUI  EDT? "
    				+ SwingUtilities.isEventDispatchThread());
    
    		JFrame f = new JFrame(" ARTIFICIAL INTELLIGENCE");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		f.add(new PaintWorld(), BorderLayout.CENTER);
    		f.pack();
    	}
    Also i don't want to add agent's logic inside EDT.
    for example
    Java Code:
    protected static void createAndShowGUI() {
    		System.out.println("Created GUI  EDT? "
    				+ SwingUtilities.isEventDispatchThread());
    
    		JFrame f = new JFrame("DISTRIBULAR ARTIFICIAL INTELLIGENCE");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		f.add(new PaintWorld(), BorderLayout.CENTER);
    		f.pack();
    	
    //for example
    agent.move();
    g.repaint();
    }
    Last edited by vas; 07-24-2014 at 06:22 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Best approach to repaint background

    I believe you are looking for a Swing timer which you can use to create a steady rate at which your application updates (such as your move()) and paints. You do -not- want that while(true) loop in a Swing application, that's called a "busy loop" and it will only eat up one of your CPU cores 100%.

    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    One thing is for sure: painting is not going to happen unless -something- triggers it and that generally happens through a call to repaint().
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Best approach to repaint background

    Quote Originally Posted by vas View Post
    How can i repaint the background without calling repaint() outside Event dispatch thread?
    Why are you trying to avoid calling the repaint() function? Note that the repaint() function can be called from whatever Thread you want, as it simply schedules the EDT to do the painting.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    vas
    vas is offline Member
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default Re: Best approach to repaint background

    I didn't considered using a Swing timer because i thought that i would not need to continuously update the background but only update it after agent's moving.Thanks for the answers!!

Similar Threads

  1. good programming practise
    By shruti7 in forum New To Java
    Replies: 10
    Last Post: 01-24-2013, 11:35 AM
  2. New App for Practise SCJP Certification
    By EnterpriseConsulting in forum Reviews / Advertising
    Replies: 2
    Last Post: 01-18-2012, 07:19 PM
  3. repaint every
    By 3xpr1ment in forum AWT / Swing
    Replies: 10
    Last Post: 03-23-2010, 06:39 PM
  4. SCJP Practise
    By himavanth in forum Java Certification
    Replies: 0
    Last Post: 02-26-2009, 07:38 PM
  5. Suggest me the best practise for accesing database by JSP page
    By saurabh.joshi in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 11-27-2007, 07:39 AM

Posting Permissions

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