I have just started making a new program. I barely have any code, and when I load it the frame is clear for like 20 seconds and then it loads what goes into the frame. why does it take so long to load? I have another code that I tested (just to make sure my comp wasn't lagging) and that one loads quickly. It has more code. Can anyone please explain this to me? Thanks.

Recommended Answers

All 17 Replies

can't explain any of that .. maybe when you show us your code, we'll be able to 'shine a light' on it, but just out of the blue ... nope, can't help you there.

when I load it the frame is clear for like 20 seconds

Probably the Thread.sleep(20000); statement.
Take that out it will speed up.

commented: good joke +1 +8

This is the code that is taking long to load.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class Fall{

	private FallMenu fm = new FallMenu();
	private FallGame fg = new FallGame();
	private JFrame f;
	
	public Fall (){
		f = new JFrame ("The Fall");
		f.add(fm);
		
		
		f.setResizable (false);
		f.setSize (300,300);
		f.setVisible(true);
		fm.menu(this);
	}
	
	
	public void startGame (boolean started){
		f.remove(fm);
		f.add (fg);
		f.setSize(300,600);
		f.validate();
		
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Fall f = new Fall ();
	}

}

build GUI, show JFrame, start javax.swing.Timer(timer.setRepeats(false);), then activate your hidden Woodoo in clasess FallMenu() and FallGame(). otherwise it would be always FallingGui :-)

private FallMenu fm = new FallMenu();
	private FallGame fg = new FallGame();

Comment out these two constructor calls and see how long it takes.

When I commented everything out (that use those 2 constructors) the program loaded quicker. When I left the constructors in, but did nothing with them the program loaded at the same rate (as everything commented). As soon as I added the menu the program returned to the original loading rate.

Here is my menu code. What could be causing the slow loading in this code?

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class FallMenu extends JPanel implements MouseListener, MouseMotionListener{

	boolean started;
	private Fall fall;
	
	public void menu (Fall f){

		addMouseListener (this);
		addMouseMotionListener (this);
		repaint();
		grabFocus();
		fall = f;
	}
	Color bC = Color.yellow;
	
	public void paintComponent (Graphics g){
		super.paintComponent(g);
		
		g.setFont (new Font ("Arail", 30,30));
		g.setColor (Color.red);
		g.drawString("The Fall!", 70, 50);
		g.setColor (bC);
		g.fillRect(130 - 40, 70, 80, 40);
		g.setColor (Color.black);
		g.drawString("Start", 140 - 40, 100);
		
	}
	
	
	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	int mx, my;
	
	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		mx = arg0.getX();
		my = arg0.getY();
		
		
		if (mx > 90 && mx < 170 && my < 110 && my > 70){
			bC = Color.red;
		}
		else {
			bC = Color.yellow;
		}
		repaint();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (mx > 90 && mx < 170 && my < 110 && my > 70){
			started = true;
			fall.startGame(started);
		}
		
		
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

Out of curiosity does this program load slow every time you execute the program? Or only the first time you opened it?

If it's only the first time then it's because the JVM is being loaded. The calls after the first call would be faster because the JVM would be cached although it isn't running.

Use netbean IDE 7.0/6.9.1/6.8...
and use the java swing window.
Then the program will be much easier and it will run quickly.

commented: Will you please stop posting nonsense you know nothing about. -3

Out of curiosity does this program load slow every time you execute the program? Or only the first time you opened it?

If it's only the first time then it's because the JVM is being loaded. The calls after the first call would be faster because the JVM would be cached although it isn't running.

It is every time I run the program.

You get the long load time without the FallGame constructor being called?
You do get it with only the FallMenu constructor.
Is your mouse moving over the FallMenu panel during the program starting? Add a println to the paintComponent method to see how many times it is called.

Use netbean IDE 7.0/6.9.1/6.8...
and use the java swing window.
Then the program will be much easier and it will run quickly.

what kind of nonsense is this??? "the program will be much easier"???
let me just assume you intended to say: "programming the code will be much easier, because NetBeans does everything concerning GUI's for you, and comes with a handy auto-complete function".

your code, whether you load it in NetBeans, Eclipse, TextPad, JCreator, Springsource Tool Suit, or just plain old notepad, will not be "easier", it will be identical.

automatically giving the advice to trust the Allmighty NetBeans is saying: "please, don't learn anything about how to write code .. Just be another "programmer" who'll need help on every single method you write, whenever NetBeans is not around to do the work for you."

I sincerely doubt that running your app in NetBeans will make it run "quickly" if running it in another way goes reeeaaally slow, so, IMHO, this post is absolutely NOT what the OP needed to read, especially not if he takes it seriously.

It is only with the menu. I have put in print methods and it is only being called once.

Have you tried taking out the repaint() call? I can't see why you would need that in your constructor.

You may also want to use requestFocusInWindow() instead of grabFocus().

commented: Just stop it .If I do wrong , please then correct me, but dont insult. +0
commented: Equalizer +16

I just tried it and it still takes a long time to load. As soon as I put in this line of code:

f.add(fm);

the program gives me the long loading time.

I have decided to take a picture of the loading frame and upload it. Here it is:

[IMG]http://img692.imageshack.us/img692/3027/blankframe.jpg[/IMG]

For debugging, take a few lines of code (eg repaint() or paintComponent() method) out of the FallMenu class and test and remove more and test until you find a normal load time.
Then put everything back except what you last removed and test.

Also try using the java command -Xprof option. See where the program is spending time.

@NormR1

hehehe are you meant JProfiler :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.