Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2007, 06:01 PM
Member
 
Join Date: Jul 2007
Posts: 35
carl is on a distinguished road
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.,
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-03-2007, 10:48 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
random numbers without random class` carlos123 New To Java 4 01-18-2008 12:44 AM
random numbers carlos123 New To Java 1 12-22-2007 04:56 AM
Random Integers www.kwalski.com Java Applets 8 12-09-2007 07:49 PM
Math.Random Java Tip Java Tips 0 11-23-2007 04:09 PM
Display random cards carl Advanced Java 1 08-07-2007 09:01 AM


All times are GMT +3. The time now is 06:51 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org