Results 1 to 13 of 13
  1. #1
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default slow java on my computer

    Hi everyone

    I made a small animation in java, but I could get only 15 fps, I thought it was a problem of my code, but then I tested it on another computer and I got 150 fps...
    I have an at least decent computer and I use mac os x with JRE 7 update 6...what could the problem be?

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: slow java on my computer

    Post some of your code here. It's difficult to tell without seeing the program.

  3. #3
    Cyclingjico is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: slow java on my computer

    In addition to posting some of the code, consider this:

    If it runs slow on one computer, and fast on another, what are the differences between the two? Also is there a difference between the way you are running the program on each computer? This may not necessarily be a programming question but more of a general troubleshooting theory event. (Although troubleshooting theory is VERY helpful in programming)

  4. #4
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: slow java on my computer

    my computer is an iMac i5 quad core with an ATI Radeon HD 4850 so I shouldn't have problem...first I tried it on a old toshiba laptop running windows seven, this laptop has not even half of the power of my actual computer, and it runs at 58-62 fps, then I though it was an OS problem, so I tried it on a Mac book Pro, and it runs at like 200 fps...in each case I ran it with a .jar file extracted from eclipse
    my code

    mainclass
    Java Code:
    import java.awt.Color;
    import java.awt.DisplayMode;
    import java.awt.Font;
    import java.awt.Graphics2D;
     
     
    public class mainclass {
     
    	long startingTime = System.currentTimeMillis();
    	long cumTime = startingTime;
    	long totTime = 0;
    	long timePassed;
    	int i=0;
    	String st="";
    	private Graphics2D g;
    	private static final DisplayMode modes1[] = {
    		new DisplayMode(800,600,32,0),
    		new DisplayMode(800,600,24,0),
    		new DisplayMode(800,600,16,0),
    		new DisplayMode(640,480,32,0),
    		new DisplayMode(640,480,24,0),
    		new DisplayMode(640,480,16,0)};
     
    	Physics ph = new Physics();
    	Screen s = new Screen();
     
     
     
    	public static void main(String[] args) throws InterruptedException{
    	mainclass p = new mainclass();
    	p.run();
     
    		}
     
     
    	public void setPhysics(){
    		ph.setVelocityX(1f);
    		ph.setVelocityY(0f);
    		ph.setAccelerationX(0f);
    		ph.setAccelerationY(0.000005f);
     
    		}
     
    	public void movieLoop(){
    		while(totTime < 10000){
    			timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
    			totTime +=timePassed;
    			g = s.getGraphics();
    			update(timePassed, totTime);
    			draw(g);
    			g.dispose();
    			s.update();
     
    			st = String.valueOf(1000/timePassed);
    			System.out.println(st);
    			i++;
    			}
    		}
     
    	public void run() throws InterruptedException{
     
    		try{
    			DisplayMode dm = s.findFirstCompatibleMode(modes1);
    			s.setFullScreen(dm);
    			setPhysics();
    			movieLoop();
    		}finally{
    			s.restoreScreen();
    			}
     
    		}	
     
     
    	public void draw(Graphics2D g){
    		
    		g.drawImage(ph.getBg(), 0,0, null);
    		g.drawImage(ph.getBall(), Math.round(ph.getX()), Math.round(ph.getY()), null);
    		g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
    		g.setColor(Color.RED);
    		g.drawString(st,10,20);
    		}
     
    	public void update(long timePassed, long totTime){
    		if(ph.getX()<=0){
    			ph.setVelocityX(Math.abs(ph.getVelocityX()));
    		}else if(ph.getX() + ph.getWidth()>= s.getWidth()){
    			ph.setVelocityX(-Math.abs(ph.getVelocityX()));
    			}
     
    		if(ph.getY()<=0){
    			ph.setVelocityY(Math.abs(ph.getVelocityY()));
     
    		}else if(ph.getY() + ph.getHeight()>= s.getHeight()){
     
    			if(ph.getVelocityY()*1000< 5.75){
     
    				ph.setAccelerationY(0);
    				ph.setVelocityY(0);
    				ph.setVelocityX(0);
    				ph.setAccelerationX(0f);
     
    				}
    			else{
     
    			ph.setVelocityY(-Math.abs(ph.getVelocityY()));
    				}
    			}
     
    			ph.update(timePassed, totTime);
    		}
    }
    Screen class
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.DisplayMode;
    import java.awt.Graphics2D;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Window;
    import java.awt.image.BufferStrategy;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class Screen extends JFrame {
    
    	private JLabel fps;
    	private GraphicsDevice vc;
    	private Window w;
    
    	
    	public Screen() {
    	GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    	vc = env.getDefaultScreenDevice();
    	JLabel fps = new JLabel("aaaaaaaaa");
    	add(fps, BorderLayout.NORTH);
    	}
    	
    	public DisplayMode[] getCompatibleDisplayMode(){
    		return vc.getDisplayModes();
    		}
    	
    	public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
    		DisplayMode goodModes[] = vc.getDisplayModes();
    		for (int x =0; x<modes.length;x++){
    			for(int y = 0; y<goodModes.length;y++){
    				if(displayModesMatch(modes[x], goodModes[y])){
    					return modes[x];			
    						}
    				}	
    			}
    		return null;
    		}	
    
    	public DisplayMode getCurrentDisplayMode(){
    		return vc.getDisplayMode();
    		}
    
    	
    	public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
    		if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
    			return false;
    		}
    		
    		if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() !=  DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
    			return false;
    		}
    		
    		if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
    			return false;
    		}
    		return true;
    	}
    
    	public void setFullScreen(DisplayMode dm){
    		JFrame f = new JFrame();
    		f.setUndecorated(true);
    		f.setResizable(false);
    		f.setIgnoreRepaint(true);
    		f.setVisible(true);
    		
    		vc.setFullScreenWindow(f);
    		
    		if(dm != null && vc.isDisplayChangeSupported()){
    			try{
    				vc.setDisplayMode(dm);
    			}catch(Exception e){}
    		}
    		
    		f.createBufferStrategy(2);
    	}
    
    
    	public Graphics2D getGraphics(){
    		w = vc.getFullScreenWindow();
    		
    		if(w!=null){
    		
    				
    			BufferStrategy s = w.getBufferStrategy();
    			return (Graphics2D)s.getDrawGraphics();
    		}else{
    			return null;
    		}
    	}
    
    	public void update(){
    		w = vc.getFullScreenWindow();
    		if(w!=null){
    			BufferStrategy s = w.getBufferStrategy();
    			if(!s.contentsLost()){
    				s.show();
    			}
    		}
    	}
    
    
    	public Window getFullScreenWindow(){
    		return vc.getFullScreenWindow();
    	}
    
    	public int getWidth(){
    		 w = vc.getFullScreenWindow();
    		if(w != null){
    		return w.getWidth();
    		}else{
    			return 0;
    		}
    		
    	}
    	public int getHeight(){
    		 w = vc.getFullScreenWindow();
    		if(w!=null){
    			return w.getHeight();
    		}else{
    			return 0;
    		}
    		
    	}
    
    	public void restoreScreen(){
    		
    		if(w!=null){
    			w.dispose();
    		
    		}
    	vc.setFullScreenWindow(null);
    	}
    
    
    
    }
    Physics class
    Java Code:
    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    
    
    public class Physics {
    	private Image bg = new ImageIcon(getClass().getResource("background.png")).getImage();
    	private Image ball =  new ImageIcon(getClass().getResource("scena2.png")).getImage();
    	private float x, y, vx, vy, ax, ay;
    	
    
    	public void update(long timePassed, long totTime){
    		
    		if(ax!=0){
    			
    			vx+=ax*totTime;
    			x+=vx*totTime; 
    		}
    		else{
    		x+=vx*timePassed; 
    
    		
    		}
    		
    		if(ay!=0){
    			vy+=ay*totTime;
    			y+=vy*totTime; 
    
    		}
    		else{
    			y+=vy*timePassed; 
    
    		
    		}	
    
    	}
    	
    	public float getX(){
    		return x;
    	}
    	
    	public float getY(){
    		return y;
    	}
    	
    	public void setX(float x){
    		this.x=x;
    	}
    	
    	public void setY(float y){
    		this.y=y;
    	}
    	
    	public int getWidth(){
    		return ball.getWidth(null);
    	}
    	public int getHeight(){
    		return ball.getHeight(null);
    	}
    	
    	public float getVelocityX(){
    		return vx;
    	}
    	public float getVelocityY(){
    		return vy;
    	}
    	
    	public void setVelocityX(float vx){
    		this.vx = vx;	
    	}
    	
    	public void setVelocityY(float vy){
    		this.vy = vy;
    	}
    	
    	public float getAccelerationX(){
    		return ax;
    	}
    	
    	public float getAccelerationY(){
    		return ay;
    	}
    	
    	public void setAccelerationX(float ax){
    		this.ax=ax;
    	}
    	
    	public void setAccelerationY(float ay){
    		this.ay=ay;
    	}
    	public Image getBall(){
    		return ball;
    	}
    	public Image getBg(){
    		return bg;
    	}
    	
    	
    	
    	
    }
    Last edited by Chri; 12-28-2012 at 12:41 PM.

  5. #5
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: slow java on my computer

    have you tried a simple reboot on your iMac? my IDE runs slow sometimes (running Linux) and restarting pc helps out a bit. you may have several processes running from applications that were used and never quit properly, thus using up CPU resources and causing slow runtimes.
    i know its simple but sometimes overlooked, many people assume that their machine can run forever and thats generally not the case.

  6. #6
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: slow java on my computer

    Yes I tried...after it I also terminated all the "heavy" processes but nothing changed...

  7. #7
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: slow java on my computer

    The only time I had to work with Java animation, I had a similar problem getting very low FPS on one workstation and normal FPS on another.

    We came to the conclusion that hardware acceleration was not enabled, and enabling this solved our issue. Can you check whether or not it is activated on your OS?

  8. #8
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: slow java on my computer

    Quote Originally Posted by Zyril View Post
    The only time I had to work with Java animation, I had a similar problem getting very low FPS on one workstation and normal FPS on another.

    We came to the conclusion that hardware acceleration was not enabled, and enabling this solved our issue. Can you check whether or not it is activated on your OS?
    How can I enable it? I can't find it anywhere..

    --Update--
    I googled and I tried to put this
    System.setProperty("sun.java2d.opengl","True");
    pretty much everywhere in the code but it did not help...
    Last edited by Chri; 12-28-2012 at 06:20 PM.

  9. #9
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: slow java on my computer

    bump...

  10. #10
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: slow java on my computer

    Hey again Chri, since I'm not a Mac user, I cannot help you with this!

    I'd recommend to see if anyone in the IRC-channel ##Java can help you out! It's on the irc.freenode.net server!

  11. #11
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: slow java on my computer

    hey, thank you, that channel says that "I need to be identified with services"...
    Last edited by Chri; 12-31-2012 at 06:41 PM.

  12. #12
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: slow java on my computer

    Yes, read up on registering at the server!

  13. #13
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: slow java on my computer

    in refrence to the hardware acceleration, i dont know if you tried this but heres a step by step
    How to Change Hardware Acceleration in Mac OS | eHow.com

Similar Threads

  1. Creating UI became very slow in Java 1.7
    By pavon in forum AWT / Swing
    Replies: 6
    Last Post: 01-10-2013, 12:48 AM
  2. java 1.7.0 update 2 very slow?
    By Matthew Doucette in forum Advanced Java
    Replies: 0
    Last Post: 02-02-2012, 08:40 PM
  3. System Freeze/Slow-down using Java
    By Grunker in forum New To Java
    Replies: 12
    Last Post: 06-22-2011, 03:03 PM
  4. Why my java lan game programm slow!
    By arnelpogs in forum Networking
    Replies: 2
    Last Post: 12-07-2009, 04:00 PM
  5. Why JAVA is so slow?
    By aRTx in forum New To Java
    Replies: 2
    Last Post: 05-11-2009, 10:19 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
  •