Problem writing an applet

I can't figure out how to write the if-else code to determine the outcome of the game. This is because I can't figure out how to write the value of the variable for the picked card! I've commented on the part where I am having problems.
// User is asked to guess which of the two face down cards has
// the greatest value. The face value is the only thing used to
// determine the value of the cards. When the user selects a card,
// the fronts of the cards are shown, and the result of the game is
// displayed. The user is aksed if he or she wishes to play again.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Exercise4 extends Applet implements MouseListener
DeckOfCards deck;
Card card1, card2, drawnCard1, drawnCard2;
String result, message, programmerName = "2";
Font bigFont;
Image cardpics; by-60
public void init()
setBackground( Color.cyan );
setSize (320,250);
addMouseListener(this);
cardpics = getImage(getCodeBase(), "cards.gif");
bigFont = new Font("Serif", Font.BOLD, 14);
deck = new DeckOfCards();
deal();
// Start the game by dealing two cards and repainting the applet.
void deal()
card1 = card2 = drawnCard1 = drawnCard2 = null;
message = "Which Card is Higher?";
result = "";
deck.shuffle();
deck.deal();
repaint();
// Because the applet is registered to listen for mouse clicks on
// itself, the system will call this method when the user presses
// the mouse on the applet. In this case, the method checks whether
// the user clicked on one of the cards or on the message at the
// bottom of the screen. If the user clicked card N, this method
// calls clickedCard(N). If the user clicked on the message, this
// method calls clickedMessage().
public void mousePressed(MouseEvent evt)
int x = evt.getX(); // convenient name for x-coord of mouse
int y = evt.getY(); // convenient name for y-coord of mouse
if ( y > 20 && y < 80 )
if (x > 100 && x < 140)
clickedCard(1);
else if (x > 160 && x < 200)
clickedCard(2);
else
if (x > 33 && x > 25)
deal();
// This method is called (by the mousePressed() method given below)
// when the user clicks on one of the cards. The parameter, cardNum,
// tells which card was clicked. String result is set to the appropriate
// message telling the outcome of the game.
void clickedCard(int cardNum)
// I can't see to pick up the right card value and display it. I don't think I am using the method above right?
cardNum = card1.getValue();
if (card1.getValue() > card2.getValue())
message = "You win!";
else if (card1.getValue() < card2.getValue())
message = "Too Bad - You Lose";
else if (card1.getValue() == card2.getValue())
message = "A Draw";
drawnCard1 = card1;
drawnCard2 = card2;
message = "Play Again?";
repaint();
// The paint method is called by the system when the applet needs
// to be repainted. In this applet, it draws the two cards
// and the messages at the bottom of the applet.
public void paint(Graphics g)
int width = getSize().width;
int height = getSize().height;
FontMetrics fm = getFontMetrics(bigFont);
int w;
g.setColor(Color.blue);
g.drawRect(0,0,width-1,height-1);
g.setColor(Color.pink); background.
g.fillRect(33, 105, 250, 25);
g.setColor(Color.blue);
g.drawRect(33, 105, 250, 25);
g.setFont( bigFont );
w = fm.stringWidth(message);
g.drawString( message, 86, 122);
g.setColor(Color.pink);
g.fillRect(33, 155, 250, 25);
g.setColor(Color.blue);
g.drawRect(33, 155, 250, 25);
g.setFont( bigFont );
w = fm.stringWidth(result);
g.drawString( result, 20, 30);
g.setColor(Color.pink);
g.fillRect(33, 205, 250, 25);
g.setColor(Color.blue);
g.drawRect(33, 205, 250, 25);
g.setFont( bigFont );
w = fm.stringWidth(programmerName);
g.drawString( programmerName, 65, 222);
drawCard( g, drawnCard1, 100, 20 ); // Draw the two cards.
drawCard( g, drawnCard2, 160, 20 ); // If drawnCard1 or drawnCard2 are null,
// the cards will be drawn face down.
// Draws a card as a 40 by 60 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.
void drawCard(Graphics g, Card card, int x, int y)
if (card == null)
// Draw a face-down card
g.setColor(Color.blue);
g.fillRect(x,y,40,60);
g.setColor(Color.white);
g.drawRect(x+3,y+3,33,53);
g.drawRect(x+4,y+4,31,51);
else
int row = 0;
switch (card.getSuit())
case 0: row = 0; break;
case 1: row = 1; break;
case 2: row = 2; break;
case 3: row = 3; break;
int sx, sy; // coords of upper left corner in the source image.
sx = 40*(card.getValue() - 1);
sy = 60*row;
g.drawImage(cardpics, x, y, x+40, y+60,
sx, sy, sx+40, sy+60, this);
// Other mouse routines required by the MouseListener interface.
// They are not used in this applet
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
// DeckOfCards.java
import java.util.Random;
public class DeckOfCards
private int count;
private boolean[] deck = new boolean[53];
Random generator = new Random();
public DeckOfCards()
shuffle();
public void shuffle()
for (int i = 1; i <= 52; i++)
deck = false;
count = 0;
public Card deal()
int num, suit;
if (count > 51)
num = -1;
suit = -1;
else
do
num = generator.nextInt(13) + 1;
suit = generator.nextInt(4);
while (deck[num+suit*13] == true);
deck[num+suit*13] = true;
count++;
return new Card(num,suit);
public int cardsInDeck()
return (52 - count);
// Card.java
public class Card
private int num, suit;
public Card (int num, int suit)
this.num = num;
this.suit = suit;
public int getValue ()
return num;
public int getSuit ()
return suit;
public String toString()
String result = "message";
switch (num)
case -1: result = " No Card"; break;
case 1: result = " Ace "; break;
case 2: result = " 2 "; break;
case 3: result = " 3 "; break;
case 4: result = " 4 "; break;
case 5: result = " 5 "; break;
case 6: result = " 6 "; break;
case 7: result = " 7 "; break;
case 8: result = " 8 "; break;
case 9: result = " 9 "; break;
case 10: result = " 10 "; break;
case 11: result = " Jack "; break;
case 12: result = " Queen"; break;
case 13: result = " King "; break;
switch (suit)
case -1: break;
case 0: result = result + " of Clubs"; break;
case 1: result = result + " of Hearts"; break;
case 2: result = result + " of Spades"; break;
case 3: result = result + " of Diamonds"; break;
return result;
}

Assuming it's this one than this problem is solved:
http://forum.java.sun.com/thread.jspa?threadID=671142

Similar Messages

  • Writing an Applet Viewer?

    I'm not sure why I'm interested in writing an applet viewer, I just thought it would be a nice project. Anyways, I'm wondering if anyone knows on how to tackle this. I've tried a little experimenting but everytime I call the applet init() method from my c++ program, it always crashes. I've tried to debug the problem in a external debugger but I can't seem to know why it's happening. If anyone can help direct me such as what I'll need to learn, what steps I need to take, etc, that'll be excellent. Thanks for your time.
    Edited by: _ImJoe on May 30, 2009 11:36 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    Crashes mean pointer problems.
    Are you verifying every single JNI call with error checking as appropriate?

  • Problem with URL - Applet

    hi, i have a little problem running my applet. it's supposed to get a file from the WWW and display its contents as a string inside a JTextArea. the file on the web is a simple .txt containing little text, say one sentence, its irrelevant here. the file is read in inside a different class that the applet calls. however, there seems to be a problem with that - maybe it has something to do with Applet restrictions. anyway. here goes:
    the applet (i removed all irrelevant applet construction objects like buttons etc) -
    public class Applet1 extends JApplet {
    private JTextArea sisestaJutt;
    private String juttVeebist;
    public void init() {
    URLReader veebist = new URLReader();
    try {
    juttVeebist.equals( veebist.reader());
    catch(Exception e) {
    Container sisu = new Container();
    sisestaJutt = new JTextArea(juttVeebist);
    sisu = getContentPane();
    sisu.setLayout( new BorderLayout() );
    sisestaJutt.setBorder(brdr);
    sisu.add(sisestaJutt, BorderLayout.CENTER);
    the called class (& method) -
    public class URLReader {
    public static String reader()
    throws Exception {
    URL jjstreet = new URL("http://www.anydomain.com/file.txt");
    BufferedReader input = new BufferedReader(
    new InputStreamReader(
    jjstreet.openStream()));
    String inputLine = input.readLine();
    input.close();
    return inputLine;

    thank you everyone for information, it's getting clearer to me already. sounds like signing would do the trick.
    however, i first tried this thing on a localhost - thats the server-program and applet at the same place, even writing in the same dir... but the text ment to be inside the textarea is still from a URL, so i guess applet has trouble getting input from the www then...
    anyway, JFrame is working very good + it has the comfortable command line feel in it =)
    ty all.

  • Problems writing text on Photoshop CC

    I am having problems writing text on Photoshop CC - the image goes black.

    Windows 8?  Update your video card driver from the GPU maker's website. If you cannot update your driver (like on a locked laptop machine), then set the GPU drawing mode in Photoshop to "Basic" and relaunch the app.

  • Lost all photos on my iPhoto and get the ERROR WRITING TO DISK when trying to reload from Time Machine. iPhoto cannot import your photos because there was a problem writing to the volume containing your iPHOTO LIBRARY  . Looking for solution.

    Lost all photos on my iPhoto and get the ERROR WRITING TO DISK when trying to reload from Time Machine. iPhoto cannot import your photos because there was a problem writing to the volume containing your iPHOTO LIBRARY  . Looking for solution.

    Problems such as yours are sometimes caused by files that should belong to you but are locked or have wrong permissions. This procedure will check for such files. It makes no changes and therefore will not, in itself, solve your problem.
    First, empty the Trash, if possible.
    Triple-click anywhere in the line below on this page to select it, then copy the selected text to the Clipboard by pressing the key combination command-C:
    find ~ $TMPDIR.. \( -flags +sappnd,schg,uappnd,uchg -o ! -user $UID -o ! -perm -600 \) 2>&- | wc -l | pbcopy
    Launch the built-in Terminal application in any of the following ways:
    ☞ Enter the first few letters of its name into a Spotlight search. Select it in the results (it should be at the top.)
    ☞ In the Finder, select Go ▹ Utilities from the menu bar, or press the key combination shift-command-U. The application is in the folder that opens.
    ☞ Open LaunchPad. Click Utilities, then Terminal in the icon grid.
    Paste into the Terminal window (command-V). The command may take a noticeable amount of time to run. Wait for a new line ending in a dollar sign ($) to appear.
    The output of the command will be a number. It's automatically copied to the Clipboard. Please paste it into a reply.
    The Terminal window doesn't show the output. Please don't copy anything from there.

  • Problem on loading applet netscape4.73 in macos9 very urgent pls help me

    Hello,
    Problems on loading applet in Netscape4.73(jre1.1.5) AppleMac OS-9.
    when i try to load applet of size ~ 200kb in Netscape 4.73 AppleMac os9, then browser abruptly closing with the AppleMac os error message :- the Applicarion " Netscape Communicator " has unexpectedly quit because an error of type 2 occured. And then asking for system restart.
    Since i am new to the AppleMac environment i cant adjust the settings please help me to overcome the problem. Its Very urgent and i am in great hurry pls.
    thanks in advance.
    Rao. V.N.

    Hi Rao,
    I'm not a mac-os specialist (even I had some business with mac !!), and it is very difficult speaking in blind, but I have severals questions that may help you.
    - Did you try you applet in other AppleMac plateform ?
    - Does your applet work fine in other browser/plateform ?
    - Does a small applet work fine on this plateform with this browser ?
    - Does you applet contains instructions like System.exit, System.* or try to access (read or write) to local files ?
    Otherelse it is possible you have problem with your jre config, try to configure your plugin with command like : -Xmx64m -Xms16m
    (These commands tell the plugin to use at least 16 Mo memory and 64 Mo for the maximum see : http://java.sun.com/docs/hotspot/gc/)
    Hope that will help you

  • Problem while loading applet in opera- very urgent please

    hi,
    I am having problem while loading applet in opera browser. Showing "Applet loading " continuously. I am trying to use URLConnection to read information from applet's own host. I cant find the exact problem what is happening, Since there is no error message in browser Javaconsole.
    If anybody have idea on this please help me. I am in great hurry.
    Thanks in advance.
    Rao. V

    hi this is rao,
    I inserted a lot of System.out statements throughout applet life cycle. But they are not displaying in java console. I think the problem is in Opera-java settings. Since i am new to the opera environment i cant get right solution.
    Please help me if anybody having clear idea on this aspect.
    thanks in advance

  • Director 12- can't create a windows projector - problem writing file error

    I am trying to create a windows projector of a project. when I get the following error - problem writing file - file name- Can't compress file that has been modified and not saved. The file has not been modified and has been saved. Any help would be great.

    Hi.
    You say you have tried publishing to a new empty folder.  From the video I can see that the folder has table1.app in there (25 secs in) which is a Mac projector that has been published.
    Yet you have Windows Projector checkbox ticked at the start of the video.
    I publish Mac and Windows projectors to entirely seperate folders as a matter of practice.
    I call the folders "Published" and "PublishedMac" and that is where the respective projectors for each piece of software lives.
    Perhaps there is some mix up between Mac and Windows publishing that is going on because
    you are publishing both to the same folder?
    Hope this helps.
    Richie

  • Problem on debug Applet

    hi all,
    i am developing an user debugger which is used to debug Applet.
    problem is, the Applet debugged must be in same directory as the debugger, simply says, the command must be:
    appletviewer MyApplet
    or
    java sun.applet.AppletViewer MyApplet
    i need following command work:
    appletviewer file:/c:\folder\MyApplet
    because normally debugger is not in the same directory as Applet debugged.
    how to solve the problem?
    i guess i have to develop my own AppletViewer, but how to do?
    thx

    i fix it.

  • Hi,  Trying to log in with my user id and password at iocbc but was not able to access. Problem message shown : Applet not initialised or may not be supported. Please refresh the page or check the browser setting  Anyone can advise? or i need to download?

    Hi,
    i have the same problem?
    Trying to log in with my user id and password at iocbc but was not able to access.
    Problem message shown : Applet not initialised or may not be supported. Please refresh the page or check the browser setting
    Anyone can advise?

    You need to install Java for your Mac OS version, and/or make sure it's enabled in the Java Preferences application and your browser's preferences.

  • Cannot import photos from camera, "problem writing to the volume"

    I recently upgraded to iPhoto 09 from iPhoto 5. While everything seemed ok in terms of Faces and Places and Events etc, today I was trying to import some photos from my camera and suddenly iPhoto 09 gave an error *"iPhoto cannot import your photos because there was a problem writing to the volume containing your iPhoto library"* somewhere mid-way through the import. I don't understand why my own internal HD would have a problem writing to.
    Luckily, I had made a bootable backup before upgrading to iLife 09, so I just booted from that external FireWire disk, opened iPhoto 5 and as usual, it imported all the photos without any issues. But I do not want to go back and forth just for importing the photos. So, Apple, please update the iPhoto 09 with necessary bug fixes.
    Has anyone else experienced this issue ? any solutions ?
    Thanks.

    Your stated problem
    I was trying to import some photos from my camera and suddenly iPhoto 09 gave an error "iPhoto cannot import your photos because there was a problem writing to the volume containing your iPhoto library" somewhere mid-way through the import.
    As I stated I have NOT seen this problem before and it is clearly a specific problem with your system not an iPhoto bug
    Then you are not watching enough. There are several reports here itself :
    http://discussions.apple.com/thread.jspa?messageID=8971388
    Issue was user had their iPhoto library on a FAT 32 formated disk - nothing at all like your personal problem
    http://discussions.apple.com/thread.jspa?threadID=1907134&tstart=0
    User was short of disk space - one of the questions I ask you since it can cause similar problems and can corrupt your library
    http://discussions.apple.com/thread.jspa?threadID=1885964&tstart=0
    Even with all of the thread jacks nothing in this long thread is like yours - these are mostly corrupted libraries and are solved by rebuilding
    http://forums.appleinsider.com/showthread.php?p=1372496
    Not on a forum that I follow - but appears to again be a FAT32 formated disk - nothing like your issue
    SInce you are mostly interested in arguing and insulting people and not in solving your problem - if you even have one e - I choose not to deal with peole with your attitude
    LN

  • Problem writing xmltab to application server

    Hi experts ,
    I have problem writing a xmltab to appliction , the file is written to application serever but with some junk at very last record and when I open it in browser I have an error
    "An invalid character was found in text content. Error processing resource 'file:///" in the end of document.
    is that due to the line break ,
    any suggestion on resolution will be great .
    below is the code .
    Thanks
    vinay

    Use the function module
    ARCHIVFILE_SERVER_TO_CLIENT
    Pass the values: as download file and destination in the respective fields.
    File to be downloaded in :(Pass the exact file name)
    PATH :                          
    SDEC01\SAPMNT\INT\HR\OUT\FMLA-20080205-0728
    and
    Destination to download the file in:
    TARGETPATH                      C:\DOCUMENTS AND SETTINGS\JILFEM\MY DOCUMENTS\FMLA-20080205-0728
    Regards,
    Jilly

  • HP Pavillion dv6-1245dx problems writing to MicroSDHC card - errors

    I've purcahsed two different brands of MicroSDHC 32GB cards. They will allow me to write 1-2GB of info before giving me an error code: 0x80070052. I was using Windows 7 and read that this was an issue that was going to be resolved in Windows 8, so I installed Windows 8 on my laptop (It's legitimate through Microsoft's MSDN program).
    I went looking for product specifications for my laptop on the HP site and it just gives this vague answer: 5-in-1 integrated Digital Media Reader for Secure Digital cards, MultiMedia cards, Memory Stick, Memory Stick Pro, or xD Picture cards.
    It doesn't specify SD, MicroSD, MicroSDHC or any variation. I am inclined to believe that the laptop doesn't support cards as large as 32GB. The largest card I've ever tried before this was an standard size 8GB SDHC, so at least I know it will handle the SDHC part. However, I have had issues with 32GB USB drives as well, where the drive gets half full and then starts having problems writing (no error codes though).
    I'd like to know if MicroSDHC cards as large as 32GB are or aren't supported before I commit to purchasing a third card. I've tried multiple adapters as well, so I know it's not 1) Me trying to stick a tiny card in a large hole, 2) poor contact between the adapter and the laptop, or 3) poor contact between the card and the adapter.
    Thank you.

    card readers are prone to problems with the internal contacts, particularly multi-card readers. If you get a card inserted a little  bit wrong it can cause it to go out. HP should warrant the problem but if you can't get relief, the expresscard multi-readers are cheap and work well. My laptop has no built in card reader so that is what I use.

  • Security problem when signed applet dynamically load plugins

    Hi!
    I have one problem : "security problem when signed applet dynamically load plugins"
    This is the scenario:
    the main program [app.jar]
    . contain applet and shared library (interface & implement of common class)
    . it is signed and run normally on browser
    . it can draw image loaded from other URL [ex] http://bp1.blogger.com/image.jpg
    . the image loader is in the shared library
    . dynamically load amazon.jar through URLClassLoader and reflection
    the plugin [amazon.jar]
    . search amazon product [ex] Harry Potter book
    . draw image on applet
    . use image loader from shared library, BUT CANNOT LOAD IMAGE
    The question: "Why it cannot load image, because the image loader is in the shared library which has been signed and working?" I tried to sign the amazon.jar too, but it did not work.
    Your reply would be very helpful. Thank you.
    Sovann

    hello. i have create a signed applet for A.jar. A.jar include two package B and C. the main applet class is within B.
    B need some classes in C to run the applet. but i got the error that class in package c are not found.
    what shall i do?

  • Problem with signed applet

    HI All,
    I am writing a small applet to draw a line graph by setting the parameters from the html pages .It came up beutifully .There are two problems here .
    1)When i try to print the applet it prints only black coloroued rectangle in place of applet(graph) .I read lot of articles only to find out that the applet need to be signed for printed .I tried all ways and means but could not succedd in printing an applet .
    2)When i try to access the applet from other computer with out copying it says that applet can not be acceessed due to security failure .It throws exception.
    I need your help on how to sign applets to access from other computer and also how to print applet content .
    Can any one help me on this
    Thanks in advance .

    i am not sure of that , but having experience with drawing images ,graphs and the like and with no experience in printing, i can share u my experience.
    using setBackground(Color.white) of the applet/panel wouldnot work since i believe it is something that is got to do with the graphics class .U have to draw a full white rectangle as a background before u start drawing lines.
    like
    g.setColor(color.white);
    g.drawfullrect(0,0,width,height); /// draw a white background
    g.setColot(Color.black);
    g.drawline(0,0);
    etc.....
    Have fun dude.

Maybe you are looking for

  • Time Capsule or other storage?

    I'm new to Mac and new to backing up data with anything other than a CD or flash drive. What is the best storage to use if I am wanting to backup the HD and Windows (using VMWare fusion)? Does Time Machine only back up the Mac HD and not the windows

  • Can someone help a non-tech type with connecting AppleTV?

    I want to connect AppleTV3.  My current setup was done 6 yrs ago when I bought TV, Receiver and Speakers.  Sony TV has 3 HDMI inputs, Integra receiver has 2 HDMI inputs.  On receiver HDMI1 is from cable box, HDMI2 is Bluray, HDMI Out goes to TV HDMI1

  • Locale values for the arabic db in oracle 10g

    Please suggest me the locale values for the Oracle 10g client/server that can store arabic data. Please also suggest me the best value for the "Database character set". Note: Both my client and server will be installed on the Arabic version of Window

  • JSP Issue in 9.1 but not 8.1

    I am building a Web application and packaging it into an EAR file then deploying it into 9.1 WebLogic server. I have an issue that is happening when I activate the changes at deployment time but this one is when it's trying to compile the JSP code, a

  • GraphicAntialiasing="true" gives REP-0606 error

    Hi Friends, When i am using graphicAntialiasing="true" to make lines in my graphs smooth it gives me Rep-0606 error. I am using reports 9.2.0.4.0. Is enviorment variable REPORTS_GRAPH_IMAGE_DPI available with this version of reports. Thanks in advanc