Results 1 to 2 of 2
  1. #1
    carl is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Help with Random cards

    Hi, I would like to display random images in a java applet i am doing. I have to create a card game called 'snap'. I would like 2 random cards to appear on the applet every time the 'Next' button is pressed which i will be creating.
    This is the skeleton program i was given to start this program.,
    Java Code:
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class CardGame extends Applet implements ActionListener /*NB other interfaces may be added*/
    {
    int MAX= some_value;
    MyObj myObjects[];
    
    public void start()
    {
    myObjects=new MyObj[MAX];
    
    //instantiate objects here
    myObjects[0]=new MyObj(/*some_arguments*/);
    }
    
    public void actionPerformed(ActionEvent e)
    {
    }
    
    public void paint(Graphics g)
    {
    }
    }
    
    class MyObj {
    MyObj(/*some_arguments*/) {
    }
    }
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    //  <applet code="SnapApplet" width="240" height="240"></applet>
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Random;
    import javax.imageio.ImageIO;
    
    public class SnapApplet extends Applet implements ActionListener
    {
        int MAX = 5;
        CardStore[] cardStores;
        Random random = new Random();
        CardCanvas cardCanvas = new CardCanvas();
    
        public void init()
        {
            BufferedImage[] images = loadImages();
            cardStores = new CardStore[MAX];
    
            //instantiate objects here
            for(int j = 0; j < MAX; j++)
            {
                cardStores[j] = new CardStore(images[j]);
            }
            cardCanvas.setImages(getImages());
            Button button = new Button("next");
            button.addActionListener(this);
            Panel panel = new Panel();
            panel.add(button);
            setLayout(new BorderLayout());
            add(cardCanvas);
            add(panel, BorderLayout.PAGE_END);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            BufferedImage[] images = getImages();
            cardCanvas.setImages(images);
        }
    
        private BufferedImage[] getImages()
        {
            BufferedImage[] images = new BufferedImage[2];
            for(int j = 0; j < images.length; j++)
            {
                int index = random.nextInt(cardStores.length);
                images[j] = cardStores[index].image;
            }
            return images;
        }
    
        public BufferedImage[] loadImages()
        {
            String prefix = "playingCards/";
            BufferedImage[] images = new BufferedImage[MAX];
            for(int j = 0; j < images.length; j++)
            {
                String path = prefix + (j+1) + ".jpg";
                try
                {
                    URL url = getClass().getResource(path);
                    images[j] = ImageIO.read(url);
                }
                catch(IOException e)
                {
                    System.err.println("Read error: " + e.getMessage());
                }
            }
            return images;
        }
    
        private class CardCanvas extends Canvas
        {
            BufferedImage[] images;
    
            public void paint(Graphics g)
            {
                int w = getWidth();
                int h = getHeight();
                int x = w - images[1].getWidth();
                int y = h - images[1].getHeight();
                g.drawImage(images[0], 0, 0, this);
                g.drawImage(images[1], x, y, this);
            }
    
            public void update(Graphics g)
            {
                paint(g);
            }
    
            public void setImages(BufferedImage[] images)
            {
                this.images = images;
                repaint();
            }
        }
    }
    
    class CardStore
    {
        BufferedImage image;
    
        CardStore(BufferedImage image)
        {
            this.image = image;
        }
    }

Similar Threads

  1. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  2. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:56 AM
  3. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 06:49 PM
  4. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM
  5. Display random cards
    By carl in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •