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 15 of 15

Thread: Why does my code's Fps run faster when i have a program running in background?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does my code's Fps run faster when i have a program running in background?

    Well thats my question.
    When i run my code in eclipse my fps are 100, but if i open for expample a google chrome windows my code's fps goes up to 500. same happens if i open for example itunes, etc.

    i think its some kind of tweak. im looking forward to get this performance without any programs running in background.. so, has anyone experienced this?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    Thread moved from Whats wrong with my code
    By 'fps' I presume you mean 'frames per second' - how are you measuring this value? Can you post an SSCCE?

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    first of all yes, by fps i ment frames per second.
    second, well i have to main methods. One that updates the scene , the other renders it. the fps measures system, in lapses of 1 second measures how many times the method render() is called.

    So, when i run my program render() is called 100 times per second. and if im still running the program and open a web-browser like google chrome , the times that render is called in a lapse of 1 seconds goes up to 500 times.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    If you could post the part of code that calculates it, it's maybe something in the code.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    sure, its very simple.

    long unprocessedTime = 0;
    int frames = 0;
    long nanoPerMove = 1000000000/60;
    long lastFpsTime = System.currentTimeMillis();


    while(running){
    long now = System.nanoTime();
    long passedTime = now - lastTime;
    if(passedTime > 0){
    unprocessedTime+=passedTime;
    while(unprocessedTime >= nanoPerMove){
    move();
    unprocessedTime-=nanoPerMove;
    }
    lastTime = now;
    }
    render(g);
    frames++;

    if(System.currentTimeMillis()-1000>=lastFpsTime){
    lastFpsTime+=1000;
    System.out.println("Fps = " + frames);
    frames = 0;
    }
    }

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    For future reference, please wrap your code in the [highlight=java][/highlight] tags.

    The posted code is not an SSCCE. There are variables without definitions, and the code is out of context. This leaves some guessing on our part...as posted, I can guess that 'g' is a Graphics2D reference, in which case I can further guess that you are trying to draw to a Swing component from a thread other than the EDT. This is not advised, and you should override the appropriate methods of Component, draw there, and repaint by calling the method of that name.
    See Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    oh gosh its not that hard to get. yes im drawing in a Swing component, its just a Canvas. G is a reference to Graphics2D indeed.

    here the full code:

     
    import java.awt.Canvas;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
     
     
    public class MainGame extends Canvas implements Runnable{
     
    	private static final long serialVersionUID = 1L;
     
    	boolean running = false;
    	Graphics2D g;
    	BufferedImage i;
    	Scene scene;
    	static Mouse mouse;
    	long lastTime;
     
    	int counter = 0;
     
    	public MainGame(){
    		i = new BufferedImage(1592,966,BufferedImage.TYPE_INT_ARGB);
    		g = (Graphics2D) i.getGraphics();
    		scene = new Scene();
    		mouse = new Mouse();
    		addMouseListener(mouse);
    		addMouseMotionListener(mouse);
    		lastTime = System.nanoTime();
    	}
     
    	public void start(){
    		if(running)return;
    		new Thread(this).start();
    		running = true;
     
    	}
     
    	public void stop(){
    		running = false;
     
    	}
     
    	public void run(){
    		long unprocessedTime = 0;
    		int frames = 0;
    		long nanoPerMove = 1000000000/60;
    		long lastFpsTime = System.currentTimeMillis();
     
     
    		while(running){
    			long now = System.nanoTime();
    			long passedTime = now - lastTime;
    			if(passedTime > 0){
    				unprocessedTime+=passedTime;
    				while(unprocessedTime >= nanoPerMove){
    					move();
    					unprocessedTime-=nanoPerMove;
    				}
    				lastTime = now;
    			}
    			render(g);
    			frames++;
     
    			if(System.currentTimeMillis()-1000>=lastFpsTime){
    				lastFpsTime+=1000;
    				System.out.println("Fps = " + frames);
    				frames = 0;
    			}
     
    			Graphics2D g2 = (Graphics2D)getGraphics();
    			g2.drawImage(i, 0, 0, 1592,966, null);
    			g2.dispose();
    			try{
    				Thread.sleep(1);
    			}catch (InterruptedException e){
     
    			}
    		}
    	}
     
    	public void move(){
    		scene.move();
    	}
     
    	public void render(Graphics2D g){
    		scene.render(g);
    	}
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    The posted code does not compile without errors: Missing class definitions.
    Also I don't see a main() method for executing the code for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    Quote Originally Posted by NightMFC View Post
    oh gosh its not that hard to get. yes im drawing in a Swing component, its just a Canvas. G is a reference to Graphics2D indeed.
    1) If you do not post all class and reference declarations, you can't expect us to know (or guess) what they are
    2) Read my post above. It alludes to an issue in your code - paint on the EDT. Do not call getGraphics (unless of course you truly understand the implications and what this method entails), rather paint by overriding paint (for AWT) or paintComponent (for Swing) and using the Graphics object passed as a parameter.

    Trail: 2D Graphics (The Java™ Tutorials)[COLOR="Silver"]

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    How do i use the EDT to paint within this example? can you give me a hint?

    the rest is just some silly drawing g.fillPolygon(x[],y[],n);

    import java.awt.Dimension;
    import javax.swing.JFrame;
     
     
    public class Start {
     
    	public static final int W = 1592;
    	public static final int H = 966;
    	public static final int X = W/2;
    	public static final int Y = H/2;
     
    	public static void main(String[] args){
    		MainGame game = new MainGame();
    		JFrame frame = new JFrame("Game Name");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setLocation(frame.getX()-X,frame.getY()-Y);
    		frame.setMaximumSize(new Dimension (W,H));
    		frame.setMinimumSize(new Dimension (W,H));
    		frame.setUndecorated(false);
    		frame.setVisible(true);
    		frame.add(game);
    		frame.pack();
    		game.start();
    		System.out.println("Started");
    	}
    }

    import java.awt.Canvas;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
     
     
    public class MainGame extends Canvas implements Runnable{
     
    	private static final long serialVersionUID = 1L;
     
    	boolean running = false;
    	Graphics2D g;
    	BufferedImage i;
    	Scene scene;
    	static Mouse mouse;
    	long lastTime;
     
    	int counter = 0;
     
    	public MainGame(){
    		i = new BufferedImage(1592,966,BufferedImage.TYPE_INT_ARGB);
    		g = (Graphics2D) i.getGraphics();
    		scene = new Scene();
    		mouse = new Mouse();
    		addMouseListener(mouse);
    		addMouseMotionListener(mouse);
    		lastTime = System.nanoTime();
    	}
     
    	public void start(){
    		if(running)return;
    		new Thread(this).start();
    		running = true;
     
    	}
     
    	public void stop(){
    		running = false;
     
    	}
     
    	public void run(){
    		long unprocessedTime = 0;
    		int frames = 0;
    		long nanoPerMove = 1000000000/60;
    		long lastFpsTime = System.currentTimeMillis();
     
     
    		while(running){
    			long now = System.nanoTime();
    			long passedTime = now - lastTime;
    			if(passedTime > 0){
    				unprocessedTime+=passedTime;
    				while(unprocessedTime >= nanoPerMove){
    					move();
    					unprocessedTime-=nanoPerMove;
    				}
    				lastTime = now;
    			}
    			render(g);
    			frames++;
     
    			if(System.currentTimeMillis()-1000>=lastFpsTime){
    				lastFpsTime+=1000;
    				System.out.println("Fps = " + frames);
    				frames = 0;
    			}
     
    			Graphics2D g2 = (Graphics2D)getGraphics();
    			g2.drawImage(i, 0, 0, 1592,966, null);
    			g2.dispose();
    			try{
    				Thread.sleep(1);
    			}catch (InterruptedException e){
     
    			}
    		}
    	}
     
    	public void move(){
    		scene.move();
    	}
     
    	public void render(Graphics2D g){
    		scene.render(g);
    	}
    }

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    How do i use the EDT to paint within this example? can you give me a hint?
    The links above have several code snippets that demonstrate this. Here's a simple example of using a JPanel and overriding paintComponent

    public class MyPanel extends JPanel{
        @Override
        public void paintComponent(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            super.paintComponent(g2d);
            //draw to the Graphics object, for instance call your method render
     
        }
    }

    Every time you wish to redraw the JPanel, call the method repaint() on the JPanel instance. Note that if you are using AWT (for instance Applet class as opposed to its Swing counterpart), override the paint method

  12. #12
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    Do u suggest i go with something like this? or am i still getting it wrong? because the ball looks soo fuzzy =/


     
    public class Start {
     
    	public static void main(String[]args){
    		Main main = new Main();
    		main.start();
    	}
    }

    import java.awt.BasicStroke;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class Main extends Canvas implements Runnable {
    	private static final long serialVersionUID = 1L;
     
    	boolean running = false;
    	BufferedImage i;
    	Graphics2D g;
    	Thread t;
    	long lastTime;
    	MyPanel panel;
     
    	public Main(){
    		init();
    	}
     
    	public void init(){
    		initGUI();
    		i = new BufferedImage(960,720,BufferedImage.TYPE_INT_ARGB);
    		g = (Graphics2D) i.getGraphics();
    		lastTime = System.nanoTime();
    	}
     
    	public void initGUI(){
     
    		JFrame frame = new JFrame("Game name");
    		panel = new MyPanel();
    		JButton button_1 = new JButton();
    		JButton button_2 = new JButton();
    		Container pane = frame.getContentPane();
    		pane.setLayout(null);
    		pane.add(panel);
    		panel.setBounds(0, 0, 960, 720);
    		pane.add(button_1);
    		button_1.setBounds(1050, 50, 50, 50);
    		pane.add(button_2);
    		button_2.setBounds(1150, 50, 50, 50);
    		frame.setMinimumSize(new Dimension(1250,1000));
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    	public void start(){
    		if(running) return;
    		t = new Thread(this);
    		t.start();
    		running = true;
    	}
     
    	public void stop(){
    		running = false;
    	}
     
    	public void run(){
    		long moveTime = 1000000000/60;
    		int frames = 0;
    		long lastFpsTime = System.currentTimeMillis();
    		long unprocessedTime = 0;
     
    		while(running){
    			long now = System.nanoTime();
    			long passedTime = now - lastTime;
    			if(passedTime > 0){
    				unprocessedTime += passedTime;
    				while(unprocessedTime >= moveTime){
    					unprocessedTime-=moveTime;
    					panel.move();
    				}
    				lastTime = now;
    			}
    			panel.repaint();
    			frames++;
     
    			if(System.currentTimeMillis()-lastFpsTime>= 1000){
    				lastFpsTime+=1000;
    				System.out.println("Fps: "+ frames);
    				frames = 0;
    			}
    		}
    	}	
    }
     
    class MyPanel extends JPanel{
    	private static final long serialVersionUID = 1L;
    	int speedX = 5;
    	int speedY = 5;
    	int x = 0;
    	int y = 0;
     
    	@Override
        public void paintComponent(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            super.paintComponent(g2d);
            render(g2d);
        }
     
    	public void render(Graphics2D g){
    		g.setColor(Color.white);
    		g.fillRect(5, 5, 950, 710);
    		g.setColor(Color.black);
    		g.setStroke(new BasicStroke(5));
    		g.drawRect(5, 5, 950, 710);
    		g.setColor(Color.blue);
    		g.fillOval(x,y,50,50);
    		g.setColor(Color.black);
    		g.setStroke(new BasicStroke(2));
    		g.drawOval(x, y, 50, 50);
    	}
     
    	public void move(){
    		if(x > this.getBounds().width - 50)speedX = -speedX;
    		if(x < 0 ) speedX = -speedX;
     
    		if(y > this.getBounds().height - 50)speedY = -speedY;
    		if(y< 0)speedY = -speedY;
     
    		x+=speedX;
    		y+=speedY;
    	}
    }

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    Looks better - is it behaving as you intend? A few notes:
    - there are a few reference no longer necessary (for instance, remove the call to getGraphics - this is a method that I recommend never using unless you know the implications).
    - Depending upon your needs, if your goal is to animate you may wish to consider using a Timer, which will fire at a periodic rate and allow you to update that way (note that a high frame rate will add overhead without any visual benefits)
    - When you override paint/paintComponent, you should almost always call the super method first (eg super.paint(g)

  14. #14
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    Oh yes. i noticed i dont even need the bufferedImage. i edited my last post adding a ball animation. but it looks so fuzzy, should i try double buffering or something?

  15. #15
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why does my code's Fps run faster when i have a program running in background?

    Swing is already double buffered by default. Try slowing down the frame rate and the frequency of repaints (for instance, only repaint after something has changed - eg after 'move') - as I mentioned above try using a Swing Timer

Similar Threads

  1. Replies: 1
    Last Post: December 12th, 2011, 06:00 AM
  2. Threads priority: low priority run faster than high priority
    By leonixyz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2011, 04:33 PM
  3. Shortcut executed or in-background running Java application
    By xixixao in forum Java Theory & Questions
    Replies: 2
    Last Post: April 22nd, 2011, 04:33 AM
  4. [SOLVED] Faster Run Time - Scanner(file) and Arrays
    By Dogeatdog6 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 7th, 2011, 09:25 AM