7 Replies - 454 Views - Last Post: 11 May 2012 - 06:16 PM Rate Topic: -----

#1 PleaseHelpne  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 11-May 12

How can I make this applet into a standalone application?

Posted 11 May 2012 - 04:18 PM


import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class BlackjackGUI extends JApplet {
    
       public void init() {
       
             // The init() method creates components and lays out the applet.
             // A BlackjackCanvas occupies the CENTER position of the layout.
             // On the bottom is a panel that holds three buttons.  The
             // BlackjackCanvas object listens for events from the buttons
             // and does all the real work of the program.
             
          setBackground( new Color(130,50,40) );
          
          BlackjackCanvas board = new BlackjackCanvas();
          getContentPane().add(board, BorderLayout.CENTER);
          
          JPanel buttonPanel = new JPanel();
          buttonPanel.setBackground( new Color(220,200,180) );
          getContentPane().add(buttonPanel, BorderLayout.SOUTH);
          
          JButton hit = new JButton( "Hit!" );
          hit.addActionListener(board);
          buttonPanel.add(hit);
          
          JButton stand = new JButton( "Stand!" );
          stand.addActionListener(board);
          buttonPanel.add(stand);
          
          JButton newGame = new JButton( "New Game" );
          newGame.addActionListener(board);
          buttonPanel.add(newGame);
          
       }  // end init()
       
       public Insets getInsets() {
             // Specify how much space to leave between the edges of
             // the applet and the components it contains.  The background
             // color shows through in this border.
          return new Insets(3,3,3,3);
       }
       

       // --- The remainder of this class consists of a nested class ---

    
       class BlackjackCanvas extends JPanel implements ActionListener {

             // A nested class that displays the card game and does all the work
             // of keeping track of the state and responding to user events.

          Deck deck;         // A deck of cards to be used in the game.

          BlackjackHand dealerHand;   // Hand containing the dealer's cards.
          BlackjackHand playerHand;   // Hand containing the user's cards.

          String message;  // A message drawn on the canvas, which changes
                           //    to reflect the state of the game.

          boolean gameInProgress;  // Set to true when a game begins and to false
                                   //   when the game ends.

          Font bigFont;      // Font that will be used to display the message.
          Font smallFont;    // Font that will be used to draw the cards.


          BlackjackCanvas() {
                // Constructor.  Creates fonts and starts the first game.
             setBackground( new Color(0,120,0) );
             smallFont = new Font("SansSerif", Font.PLAIN, 12);
             bigFont = new Font("Serif", Font.BOLD, 14);
             doNewGame();
          }


          public void actionPerformed(ActionEvent evt) {
                 // Respond when the user clicks on a button by calling
                 // the appropriate procedure.  Note that the canvas is
                 // registered as a listener in the BlackjackGUI class.
             String command = evt.getActionCommand();
             if (command.equals("Hit!"))
                doHit();
             else if (command.equals("Stand!"))
                doStand();
             else if (command.equals("New Game"))
                doNewGame();
          }


          void doHit() {
                 // This method is called when the user clicks the "Hit!" button.
                 // First check that a game is actually in progress.  If not, give
                 // an error message and exit.  Otherwise, give the user a card.
                 // The game can end at this point if the user goes over 21 or
                 // if the user has taken 5 cards without going over 21.
             if (gameInProgress == false) {
                message = "Click \"New Game\" to start a new game.";
                repaint();
                return;
             }
             playerHand.addCard( deck.dealCard() );
             if ( playerHand.getBlackjackValue() > 21 ) {
                message = "You've busted!  Sorry, you lose.";
                gameInProgress = false;
             }
             else if (playerHand.getCardCount() == 5) {
                message = "You win by taking 5 cards without going over 21.";
                gameInProgress = false;
             }
             else {
                message = "You have " + playerHand.getBlackjackValue() + ".  Hit or Stand?";
             }
             repaint();
          }


          void doStand() {
                  // This method is called when the user clicks the "Stand!" button.
                  // Check whether a game is actually in progress.  If it is,
                  // the game ends.  The dealer takes cards until either the
                  // dealer has 5 cards or more than 16 points.  Then the 
                  // winner of the game is determined.
             if (gameInProgress == false) {
                message = "Click \"New Game\" to start a new game.";
                repaint();
                return;
             }
             gameInProgress = false;
             while (dealerHand.getBlackjackValue() <= 16 && dealerHand.getCardCount() < 5)
                dealerHand.addCard( deck.dealCard() );
             if (dealerHand.getBlackjackValue() > 21)
                 message = "You win!  Dealer has busted with " + dealerHand.getBlackjackValue() + ".";
             else if (dealerHand.getCardCount() == 5)
                 message = "Sorry, you lose.  Dealer took 5 cards without going over 21.";
             else if (dealerHand.getBlackjackValue() > playerHand.getBlackjackValue())
                 message = "Sorry, you lose, " + dealerHand.getBlackjackValue()
                                                   + " to " + playerHand.getBlackjackValue() + ".";
             else if (dealerHand.getBlackjackValue() == playerHand.getBlackjackValue())
                 message = "Sorry, you lose.  Dealer wins on a tie.";
             else
                 message = "You win, " + playerHand.getBlackjackValue()
                                                   + " to " + dealerHand.getBlackjackValue() + "!";
             repaint();
          }


          void doNewGame() {
                 // Called by the constructor, and called by actionPerformed() if
                 // the use clicks the "New Game" button.  Start a new game.
                 // Deal two cards to each player.  The game might end right then
                 // if one of the players had blackjack.  Otherwise, gameInProgress
                 // is set to true and the game begins.
             if (gameInProgress) {
                     // If the current game is not over, it is an error to try
                     // to start a new game.
                message = "You still have to finish this game!";
                repaint();
                return;
             }
             deck = new Deck();   // Create the deck and hands to use for this game.
             dealerHand = new BlackjackHand();
             playerHand = new BlackjackHand();
             deck.shuffle();
             dealerHand.addCard( deck.dealCard() );  // Deal two cards to each player.
             dealerHand.addCard( deck.dealCard() );
             playerHand.addCard( deck.dealCard() );
             playerHand.addCard( deck.dealCard() );
             if (dealerHand.getBlackjackValue() == 21) {
                 message = "Sorry, you lose.  Dealer has Blackjack.";
                 gameInProgress = false;
             }
             else if (playerHand.getBlackjackValue() == 21) {
                 message = "You win!  You have Blackjack.";
                 gameInProgress = false;
             }
             else {
                 message = "You have " + playerHand.getBlackjackValue() + ".  Hit or stand?";
                 gameInProgress = true;
             }
             repaint();
          }  // end newGame();


          public void paintComponent(Graphics g) {
                // The paint method shows the message at the bottom of the
                // canvas, and it draws all of the dealt cards spread out
                // across the canvas.

             super.paintComponent(g); // fill with background color.

             g.setFont(bigFont);
             g.setColor(Color.green);
             g.drawString(message, 10, getSize().height - 10);

             // Draw labels for the two sets of cards.

             g.drawString("Dealer's Cards:", 10, 23);
             g.drawString("Your Cards:", 10, 153);

             // Draw dealer's cards.  Draw first card face down if
             // the game is still in progress,  It will be revealed
             // when the game ends.

             g.setFont(smallFont);
             if (gameInProgress)
                drawCard(g, null, 10, 30);
             else
                drawCard(g, dealerHand.getCard(0), 10, 30);
             for (int i = 1; i < dealerHand.getCardCount(); i++)
                drawCard(g, dealerHand.getCard(i), 10 + i * 90, 30);

             // Draw the user's cards.

             for (int i = 0; i < playerHand.getCardCount(); i++)
                drawCard(g, playerHand.getCard(i), 10 + i * 90, 160);

          }  // end paint();


          void drawCard(Graphics g, Card card, int x, int y) {
                  // Draws a card as a 80 by 100 rectangle with
                  // upper left corner at (x,y).  The card is drawn
                  // in the graphics context g.  If card is null, then
                  // a face-down card is drawn.  (The cards are 
                  // rather primitive.)
             if (card == null) {  
                    // Draw a face-down card
                g.setColor(Color.blue);
                g.fillRect(x,y,80,100);
                g.setColor(Color.white);
                g.drawRect(x+3,y+3,73,93);
                g.drawRect(x+4,y+4,71,91);
             }
             else {
                g.setColor(Color.white);
                g.fillRect(x,y,80,100);
                g.setColor(Color.gray);
                g.drawRect(x,y,79,99);
                g.drawRect(x+1,y+1,77,97);
                if (card.getSuit() == Card.DIAMONDS || card.getSuit() == Card.HEARTS)
                   g.setColor(Color.red);
                else
                   g.setColor(Color.black);
                g.drawString(card.getValueAsString(), x + 10, y + 30);
                g.drawString("of", x+ 10, y + 50);
                g.drawString(card.getSuitAsString(), x + 10, y + 70);
             }
          }  // end drawCard()


       } // end nested class BlackjackCanvas


    } // end class BlackjackGUI
    
   



Is This A Good Question/Topic? 0
  • +

Replies To: How can I make this applet into a standalone application?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: How can I make this applet into a standalone application?

Posted 11 May 2012 - 04:45 PM

You could add your Applet to a JFrame, and use the JFrame for your standalone application.
Was This Post Helpful? 0
  • +
  • -

#3 PleaseHelpne  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Re: How can I make this applet into a standalone application?

Posted 11 May 2012 - 04:55 PM

View Postmacosxnerd101, on 11 May 2012 - 04:45 PM, said:

You could add your Applet to a JFrame, and use the JFrame for your standalone application.


Thank you! I replaced the code as
public class BlackjackGUI extends JFrame.

However, I'm not sure how i'm supposed to use Jframe as my standalone application.
Was This Post Helpful? 0
  • +
  • -

#4 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: How can I make this applet into a standalone application?

Posted 11 May 2012 - 05:23 PM

after extending JFrame, the second thing you need to do is create a main method so that the program executes. Think of the main method as the equivalent of the JApplets init method.

public static void main(String[] args) {
new BlackjackGUI();
}



Also, put the init method in a constructor for the BlackjackGUI class, so that it is called upon startup. Since you aren't dealing with an applet anymore, the init method isn't called automatically.

public BlackjackGUI() {
init();
}


This post has been edited by oha055: 11 May 2012 - 05:28 PM

Was This Post Helpful? 1
  • +
  • -

#5 PleaseHelpne  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Re: How can I make this applet into a standalone application?

Posted 11 May 2012 - 05:43 PM

View Postoha055, on 11 May 2012 - 05:23 PM, said:

after extending JFrame, the second thing you need to do is create a main method so that the program executes. Think of the main method as the equivalent of the JApplets init method.

public static void main(String[] args) {
new BlackjackGUI();
}



Also, put the init method in a constructor for the BlackjackGUI class, so that it is called upon startup. Since you aren't dealing with an applet anymore, the init method isn't called automatically.

public BlackjackGUI() {
init();
}




Thank you so much. I added this code (the new constructor is at the top and the main method is at the bottom)
But when I run the main method, nothing happens.

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

public class BlackjackGUI extends JFrame {

    public BlackjackGUI(){
        init();
    }

    public void init() {
        setBackground( new Color(130,50,40) );

        BlackjackCanvas board = new BlackjackCanvas();
        getContentPane().add(board, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground( new Color(220,200,180) );
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        JButton hit = new JButton( "Hit!" );
        hit.addActionListener(board);
        buttonPanel.add(hit);

        JButton stand = new JButton( "Stand!" );
        stand.addActionListener(board);
        buttonPanel.add(stand);

        JButton newGame = new JButton( "New Game" );
        newGame.addActionListener(board);
        buttonPanel.add(newGame);

    } 

    public Insets getInsets() {
        return new Insets(3,3,3,3);
    }

    class BlackjackCanvas extends JPanel implements ActionListener {

        Deck deck;         
        BlackjackHand dealerHand;  
        BlackjackHand playerHand;   
        String message; 

        boolean gameInProgress;  

        Font bigFont;     
        Font smallFont;    

        BlackjackCanvas() {

            setBackground( new Color(0,120,0) );
            smallFont = new Font("SansSerif", Font.PLAIN, 12);
            bigFont = new Font("Serif", Font.BOLD, 14);
            doNewGame();
        }

        public void actionPerformed(ActionEvent evt) {

            String command = evt.getActionCommand();
            if (command.equals("Hit!"))
                doHit();
            else if (command.equals("Stand!"))
                doStand();
            else if (command.equals("New Game"))
                doNewGame();
        }

        void doHit() {

            if (gameInProgress == false) {
                message = "Click \"New Game\" to start a new game.";
                repaint();
                return;
            }
            playerHand.addCard( deck.dealCard() );
            if ( playerHand.getBlackjackValue() > 21 ) {
                message = "You've busted!  Sorry, you lose.";
                gameInProgress = false;
            }
            else if (playerHand.getCardCount() == 5) {
                message = "You win by taking 5 cards without going over 21.";
                gameInProgress = false;
            }
            else {
                message = "You have " + playerHand.getBlackjackValue() + ".  Hit or Stand?";
            }
            repaint();
        }

        void doStand() {

            if (gameInProgress == false) {
                message = "Click \"New Game\" to start a new game.";
                repaint();
                return;
            }
            gameInProgress = false;
            while (dealerHand.getBlackjackValue() <= 16 && dealerHand.getCardCount() < 5)
                dealerHand.addCard( deck.dealCard() );
            if (dealerHand.getBlackjackValue() > 21)
                message = "You win!  Dealer has busted with " + dealerHand.getBlackjackValue() + ".";
            else if (dealerHand.getCardCount() == 5)
                message = "Sorry, you lose.  Dealer took 5 cards without going over 21.";
            else if (dealerHand.getBlackjackValue() > playerHand.getBlackjackValue())
                message = "Sorry, you lose, " + dealerHand.getBlackjackValue()
                + " to " + playerHand.getBlackjackValue() + ".";
            else if (dealerHand.getBlackjackValue() == playerHand.getBlackjackValue())
                message = "Sorry, you lose.  Dealer wins on a tie.";
            else
                message = "You win, " + playerHand.getBlackjackValue()
                + " to " + dealerHand.getBlackjackValue() + "!";
            repaint();
        }

        void doNewGame() {

            if (gameInProgress) {

                message = "You still have to finish this game!";
                repaint();
                return;
            }
            deck = new Deck();   
            dealerHand = new BlackjackHand();
            playerHand = new BlackjackHand();
            deck.shuffle();
            dealerHand.addCard( deck.dealCard() ); 
            dealerHand.addCard( deck.dealCard() );
            playerHand.addCard( deck.dealCard() );
            playerHand.addCard( deck.dealCard() );
            if (dealerHand.getBlackjackValue() == 21) {
                message = "Sorry, you lose.  Dealer has Blackjack.";
                gameInProgress = false;
            }
            else if (playerHand.getBlackjackValue() == 21) {
                message = "You win!  You have Blackjack.";
                gameInProgress = false;
            }
            else {
                message = "You have " + playerHand.getBlackjackValue() + ".  Hit or stand?";
                gameInProgress = true;
            }
            repaint();
        }  // end newGame();

        public void paintComponent(Graphics g) {

            super.paintComponent(g); 
            g.setFont(bigFont);
            g.setColor(Color.green);
            g.drawString(message, 10, getSize().height - 10);

            g.drawString("Dealer's Cards:", 10, 23);
            g.drawString("Your Cards:", 10, 153);


            g.setFont(smallFont);
            if (gameInProgress)
                drawCard(g, null, 10, 30);
            else
                drawCard(g, dealerHand.getCard(0), 10, 30);
            for (int i = 1; i < dealerHand.getCardCount(); i++)
                drawCard(g, dealerHand.getCard(i), 10 + i * 90, 30);


            for (int i = 0; i < playerHand.getCardCount(); i++)
                drawCard(g, playerHand.getCard(i), 10 + i * 90, 160);

        }  

        void drawCard(Graphics g, Card card, int x, int y) {

            if (card == null) {  

                g.setColor(Color.blue);
                g.fillRect(x,y,80,100);
                g.setColor(Color.white);
                g.drawRect(x+3,y+3,73,93);
                g.drawRect(x+4,y+4,71,91);
            }
            else {
                g.setColor(Color.white);
                g.fillRect(x,y,80,100);
                g.setColor(Color.gray);
                g.drawRect(x,y,79,99);
                g.drawRect(x+1,y+1,77,97);
                if (card.getSuit() == Card.DIAMONDS || card.getSuit() == Card.HEARTS)
                    g.setColor(Color.red);
                else
                    g.setColor(Color.black);
                g.drawString(card.getValueAsString(), x + 10, y + 30);
                g.drawString("of", x+ 10, y + 50);
                g.drawString(card.getSuitAsString(), x + 10, y + 70);
            }
        } 

    } 
    public static void main(String[] args) {
        new BlackjackGUI();
    }

} 


Was This Post Helpful? 0
  • +
  • -

#6 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: How can I make this applet into a standalone application?

Posted 11 May 2012 - 05:48 PM

at the top of your init method put

setVisible(true);


Was This Post Helpful? 1
  • +
  • -

#7 PleaseHelpne  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 11-May 12

Re: How can I make this applet into a standalone application?

Posted 11 May 2012 - 06:07 PM

View Postoha055, on 11 May 2012 - 05:48 PM, said:

at the top of your init method put

setVisible(true);




Thank you! Main is working perfectly now!
Any tips on how to make the window bigger? When I run main, the program starts, but the window is minimized. I want to make it at least 500x500, but i'm not sure how
Was This Post Helpful? 0
  • +
  • -

#8 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: How can I make this applet into a standalone application?

Posted 11 May 2012 - 06:16 PM

Sure thing!

// Set the size of your JFrame
setPreferredSize(new Dimension(500, 500));
// Make setPreferredSize() work
pack();
// Center application on the screen
setLocationRelativeTo(null); /* needs to be called AFTER pack() to work properly */
// Make the exit button actually terminate the code instead of just closing the JFrame
setDefaultCloseOperation(EXIT_ON_CLOSE);

EDIT:
// Always good to have a title
setTitle("name of application");
// May sometimes be preferable
setResizable(false);



Put all of this in the constructor/method that gets called upon when creating the JFrame

This post has been edited by oha055: 11 May 2012 - 06:21 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1