Results 1 to 4 of 4
  1. #1
    gligor_kot is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default Looped repaint of display area

    Hi everyone,

    I am a new member of this forum and I need your help for a little problem I have.

    I have this piece of code in my main function:

    Java Code:
    while (true) {
       if (simRunning) {
          da.repaint();
          fpsGA.repaint();
          vehiclesNumberGA.repaint();
       }
    }
    The purpose of the code is to update the display area, and a few graph areas while my simulation is running. This worked perfectly for a while, but recently I think I've updated to a newer version of JDK and now the program doesn't seem to run the repaints if my simRunning boolean is true.

    What is even more interesting, when I go to debug mode it does run the repaints when my simRunning is true. So only when I normally run my application I have the problem with the repaints.

    Any suggestion will be appreciated.

    Thanks,

    Gix

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Just updating the JDK couldn't cause for this, hard to believe that. Can you show up your code here, which we can have an idea what's you've done.

  3. #3
    gligor_kot is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default

    My code is pretty huge, so i will just post the part I guess you need to determine what the problem is. The trick is that the code hasn't changed from what it was before I upgraded and after I upgraded, and while before it worked now it doesn't.

    This is the display area (da) class paint function, the bolded code part functions will be given below:
    Java Code:
    public void paintComponent(Graphics g) {
    			statBar.setMessage(lastStatusMessage
    					+ "                                      x = " + mouseXMove
    					+ ",  y = " + mouseYMove);
    
    			Graphics2D g2 = (Graphics2D) g;
    
    			g2.translate(xOffset, yOffset);
    			g2.scale(ZOOM, ZOOM);
    			g2.setBackground(new Color(162, 205, 90));
    
    			g2.clearRect((int) ((xOffset * -1) / ZOOM),
    					(int) ((yOffset * -1) / ZOOM), (int) (getWidth() / ZOOM),
    					(int) (getHeight() / ZOOM));
    			g2.setClip((int) ((xOffset * -1) / ZOOM),
    					(int) ((yOffset * -1) / ZOOM), (int) (getWidth() / ZOOM),
    					(int) (getHeight() / ZOOM));
    
    			g2.addRenderingHints(new RenderingHints(
    					RenderingHints.KEY_ANTIALIASING,
    					RenderingHints.VALUE_ANTIALIAS_ON));
    
    			[B]trafficMap.moveVehicles(frameRate);
    			trafficMap.drawMap(g2);[/B]
    
    			// Start and End Positions
    			g2.setFont(new Font("Trebuchet MS", 0, 6));
    			g2.setColor(new Color(238, 154, 0));
    			if (routeStart != null) {
    				g2.fillOval(routeStart.getXLoc() - 10,
    						routeStart.getYLoc() - 10, 20, 20);
    				g2.setColor(Color.black);
    				g2.drawString("Start", routeStart.getXLoc() - 7, routeStart
    						.getYLoc() + 2);
    			}
    
    			g2.setColor(new Color(238, 154, 0));
    			if (routeEnd != null) {
    				g2.fillOval(routeEnd.getXLoc() - 10, routeEnd.getYLoc() - 10,
    						20, 20);
    				g2.setColor(Color.black);
    				g2.drawString("End", routeEnd.getXLoc() - 5,
    						routeEnd.getYLoc() + 2);
    			}
    
    			if (showMouseDrag) {
    				g2.setColor(Color.black);
    				g2.drawLine(mouseX1, mouseY1, mouseX2, mouseY2);
    			}
    		}
    Here's the trafficMap draw function:
    Java Code:
    public void drawMap(Graphics g) {
    		TrafficSim.startRefreshTime = System.currentTimeMillis();
    
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setFont(new Font("Trebuchet MS", 0, 6));
    
    		// if we have put background Map image
    		if (bckgImage != null)
    			g2.drawImage(bckgImage, 0, 0, null);
    
    		// Roads
    		for (Road r : roads) {
    			[B]r.drawRoad(g2, new Color(139, 139, 131));[/B]
    		}
    
    		for (Road r : roads) {
    			[B]r.drawVehicles(g2);[/B]
    		}
    
    		TrafficSim.endRefreshTime = System.currentTimeMillis();
    
    		TrafficSim.simulationTime = (int) (TrafficSim.endRefreshTime - TrafficSim.simulationStart) / 100;
    
    		TrafficSim.waitRefresh = TrafficSim.endRefreshTime
    				- TrafficSim.startRefreshTime;
    		if (TrafficSim.waitRefresh < 1)
    			TrafficSim.waitRefresh = 1;
    
    		TrafficSim.frameRate = (double) 1000;
    		TrafficSim.frameRate /= (double) TrafficSim.waitRefresh;
    	}
    The draw road function and the draw vehicle functions only draws polygons of those classes at the appropriate spots.

    But the code i am posting is not a problem at all... the problem is in the while loop I posted in my first post. Here's my main function:
    Java Code:
    public static void main(String args[]) {
    		new TrafficSim();
    		
    		while (true) {
    			if (simRunning) {
    				da.repaint();
    				fpsGA.repaint();
    				vehiclesNumberGA.repaint();
    			}
    		}
    	}
    So basically, the program is stuck in the while loop during execution and should repaint my components when I set simRunning to true. But now, even if I put something of the sort:
    Java Code:
    System.out.println(simRunning);
    which will print "true", it will still refuse to go inside and execute the repaints in the if statement...

  4. #4
    gligor_kot is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default

    I forgot to add that I am running this on Ubuntu Linux 9.04

Similar Threads

  1. repaint() problems
    By Emily1100125 in forum AWT / Swing
    Replies: 5
    Last Post: 02-03-2009, 04:11 PM
  2. Help with repaint() command
    By GeoffTK in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 04:33 AM
  3. Problem in repaint
    By Preethi in forum AWT / Swing
    Replies: 16
    Last Post: 03-18-2008, 08:10 PM
  4. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 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
  •