[SOLVED] Blackjack program runs excrutiatingly slow.
Hi, I'm taking a computer science course in school, and for a project I need to write a game, and in my case blackjack.
I've only just started learning how to write in GUI, and I'm really just a beginner. Anyways my problem is that whenever I want to run my program as an application (be it in Eclipse or Dr. Java), it takes anywhere from 2-5 minutes to load, and during this time my computer lags to hell.
Also I noted that in task manager I have around 10 javaw.exe tasks running, although I have 8GBs of Ram.
I'm using a visual editor at the moment to write my GUI code, since my teacher didn't exactly 'teach' how to do this kind of code, and I'm learning it through trial and error. Anyways, here's the code, and if anyone can help me and tell me where the problem lies, I'd really appreciate it.
Thanks
Code :
import javax.swing.JFrame;
import javax.swing.JPanel;
//import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.JLabel;
import java.util.Random;
public class Blackjack {
int card = 0;
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JLabel jLabel = null;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new Dimension(593, 429));
jFrame.setTitle("Blackjack");
jFrame.setVisible(true);
jFrame.setResizable(false);
jFrame.setFont(new Font("Calibri", Font.PLAIN, 12));
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(150, 75, 181, 31));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(jLabel, null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(45, 345, 75, 28));
jButton.setFont(new Font("Calibri", Font.BOLD, 12));
jButton.setText("Hit Me");
jButton.addActionListener(new java.awt.event.ActionListener() {
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(java.awt.event.ActionEvent e) {
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(135, 345, 71, 28));
jButton1.setFont(new Font("Calibri", Font.BOLD, 12));
jButton1.setText("Deal");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
deal();
}
});
}
return jButton1;
}
public int deal() {
Random dealCard = new Random();
card = (dealCard.nextInt(10)+ 1);
jLabel.setText("Your card total: " + card);
return card;
}
private static void runGUI() {
//JFrame.setDefaultLookAndFeelDecorated(true);
Blackjack greeting = new Blackjack();
greeting.getJFrame();
}
/**
* @param args
*/
public static void main(String[] args) {
runGUI();
}
}
PS at the moment this program really does nothing, as I'm only trying to figure out how to get the buttons to work, and make my blackjack game.
Any tips on how to get this done would also be appreciated!
Thanks in advance.
Re: Blackjack program runs excrutiatingly slow.
Your code doesn't seem to pose a problem on my system, took the normal time to load (couple of seconds). I'd suggest testing it on another computer. You may also with to try running your GUI on the Swing EDT rather than the main thread, although I doubt this could be the problem
Code :
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Blackjack greeting = new Blackjack();
greeting.getJFrame();
}
});
}
With regards to design, I'd encourage you to use a LayoutManager as opposed to a null layout.
Re: Blackjack program runs excrutiatingly slow.
Quote:
Originally Posted by
copeg
Your code doesn't seem to pose a problem on my system, took the normal time to load (couple of seconds). I'd suggest testing it on another computer. You may also with to try running your GUI on the Swing EDT rather than the main thread, although I doubt this could be the problem
Code :
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Blackjack greeting = new Blackjack();
greeting.getJFrame();
}
});
}
With regards to design, I'd encourage you to use a LayoutManager as opposed to a null layout.
Thanks for the reply, I tested my code again on the computers at my school, and there it didn't load at all.
A friend of mine showed me a code similar to what you posted, and I'm trying that right now. It seems to have sped up the loading time considerably, because a few days ago, pressing RUN would lag the computer for a good minute or two, and I could barley use the mouse, even though I built my rig only a few months ago.
For now I think I've got my program on it's way, so thanks again for the help!
Btw, every so often I get Null exeption errors, and I've searched countless sites, but most explanations don't really make sense to me, is there a way you could dumb it down for me?
I've seem to gone around them for now, but I'd like to know what to do if it happens again.
Thanks in advance.
Re: Blackjack program runs excrutiatingly slow.
NULL Exceptions are caused by attempting to use an Object that has a value of NULL, i.e. NOT instantiated.
Attempting to call a method on such an object would cause a NullPointerException to be thrown.
for example
JLabel label = null;
label.setText("test");
would cause a NullPointerException.
JLabel label = new JLabel();
label.setText("test");
would NOT cause a NullPointerException;
I would recommend you put in some exception handling in the main method to catch the exception and get a stacktrace this should tell you which line of code the exception is being thrown.
eg.
public static void main(String[] args) {
try {
runGUI();
} catch (Exception e) {
e.printStackTrace();
}
}
}
It is seen as good practice to always check for null values before using an Object, where a null value may be possible, as these Exceptions will cause the VM to exit if the exception is not caught properly and are very hard to find in complex code.
Re: Blackjack program runs excrutiatingly slow.
Quote:
Originally Posted by
JavaDaveUK
NULL Exceptions are caused by attempting to use an Object that has a value of NULL, i.e. NOT instantiated.
Attempting to call a method on such an object would cause a NullPointerException to be thrown.
for example
JLabel label = null;
label.setText("test");
would cause a NullPointerException.
JLabel label = new JLabel();
label.setText("test");
would NOT cause a NullPointerException;
I would recommend you put in some exception handling in the main method to catch the exception and get a stacktrace this should tell you which line of code the exception is being thrown.
eg.
public static void main(String[] args) {
try {
runGUI();
} catch (Exception e) {
e.printStackTrace();
}
}
}
It is seen as good practice to always check for null values before using an Object, where a null value may be possible, as these Exceptions will cause the VM to exit if the exception is not caught properly and are very hard to find in complex code.
Thanks for the prompt reply! Best explanation I've read so far.
My blackjack game is nearly done now, and I just need to work out some of the little glitches in it, but overall its looking good.
Thanks for your help.
Re: Blackjack program runs excrutiatingly slow.
I wrote a little card game program myself recently.. It was an simple form of blackjack where you can play against yourself.. I wrote a lot about card games for my project.. helped me a lot since i wasn't much of a card player but I had to finish the project for school :D
very good work by the way!