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.

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.

Comments
good joke +1

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.

Edited 5 Years Ago by NormR1: n/a

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.

Comments
Will you please stop posting nonsense you know nothing about.

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().

Edited 5 Years Ago by Ezzaral: n/a

Comments
Equalizer
Just stop it .If I do wrong , please then correct me, but dont insult.

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.

This article has been dead for over six months. Start a new discussion instead.