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

Thread: Blackjack program runs excrutiatingly slow.

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default [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



     
    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.
    Last edited by sina12345; January 19th, 2010 at 06:11 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,371
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default 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

    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.

  3. The Following User Says Thank You to copeg For This Useful Post:

    sina12345 (January 18th, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Blackjack program runs excrutiatingly slow.

    Quote Originally Posted by copeg View Post
    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

    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.

  5. #4
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default 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.

  6. The Following User Says Thank You to JavaDaveUK For This Useful Post:

    sina12345 (January 19th, 2010)

  7. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Blackjack program runs excrutiatingly slow.

    Quote Originally Posted by JavaDaveUK View Post
    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.

  8. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    very good work by the way!
    Last edited by copeg; December 10th, 2010 at 10:06 AM. Reason: Removed link to gambling site

Similar Threads

  1. runs once only
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2009, 03:31 AM