0

So, I have this blackjack game which I have been working on. I have successfully done it, but the output is normally printed in the form ''3 of Diamonds'' on the Java terminal window.

My instructor has requested that I modify it so it will be able to show the card as an image. What I have done so far is develop an equation (int i = n*type+rdigit) (used from the OneCard class) so that the computer will know to which card I am referring to, from the cards in the picture of allcards. For example, if the Ace of Spades is to be used, n = 13, type = 1 and rdigit = 1. When put into the equation the answer is correct.

My problem is that I have a very small amount of knowledge of how to draw graphics, thus I have no idea how to actually show the image.

I would really appreciate if someone was to help me on this presumably easy task.

This is where everything I have is located. https://www.dropbox.com/home/Blackjack%20Card%20Game

Thank you.

user2035045
  • 27
  • 1
  • 5
  • 2
    I can't access the dropbox link that you posted. Make sure that is accessible from everyone. – Francesco Montesano Feb 02 '13 at 12:10
  • 3
    Better: edit your question to include an [sscce](http://sscce.org/) that exhibits a specific problem; see also these [examples](http://stackoverflow.com/q/10693762/230513) suggesting `Icon`. – trashgod Feb 02 '13 at 14:24
  • You simply can use a `JLabel` to put your image to, for displaying purpose, as `JLabel imageLabel = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/imageFolder/imagePath.extension"))));` and putting this inside a try/catch block. – nIcE cOw Feb 02 '13 at 14:49
  • @GagandeepBali Is the try/catch necessary? One of my pet hates about the `ImageIcon` class is that it seems to consume errors silently. – Andrew Thompson Feb 03 '13 at 03:27
  • @AndrewThompson : If I am not mistaken, I should have explicitly stated the reason for using `try/catch` block, being, the use of [ImageIO.read(...)](http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#read(java.net.URL)) part, this seems to throw `IOException/IllegalArgumentException`. My bad for missing this point. – nIcE cOw Feb 03 '13 at 03:51
  • @GagandeepBali I'm not sure what that means. Note that AFAIU, `ImageIO` is not used to load images for an `ImageIcon`. An `ImageIcon` can load an animated GIF, whereas `ImageIO` only returns/displays the first frame. If `ImageIO` *is* used internally to load images for the icon, it 1) Has tricks to make them animated. 2) Swallows any exception output. – Andrew Thompson Feb 03 '13 at 03:56
  • @AndrewThompson : Actually I tried to test this [example](http://stackoverflow.com/a/11428289/1057230) without try/catch , and it gave me errors : `src\ButtonImageExample.java:26: error: unreported exception IOException; must be caught or declared to be thrown image = new ImageIcon(ImageIO.read(` – nIcE cOw Feb 03 '13 at 04:04
  • 1
    @GagandeepBali *Oh **Right!*** I missed the part of your 1st comment that explicitly calls `ImageIO`! `` Try taking out any reference to `ImageIO` and pass the `URL` directly to the `ImageIcon` constructor. **That** should not require a `try/catch`. – Andrew Thompson Feb 03 '13 at 04:14
  • [Slick2D](http://www.slick2d.org/) will have a game up in no time quick and easy. – David Kroukamp Feb 03 '13 at 13:47

1 Answers1

2

You need a Graphical UI framework for Java.

Previously, Swing was the popular framework for Java GUI applications. In later years, JavaFX is intended as a replacement, and is now included in JDK7.

Google:

javafx tutorial

javafx example

or similar to get started.

Rop
  • 3,359
  • 3
  • 38
  • 59
  • JavaFX isn't meant as replacement for Swing - Swing is a core API and is unlikely to be deprecated anytime soon. JavaFX is intended as a competitor to Flash and to provide an alternative solution. While a agree, in this case, JavaFX is probably worth a look, I wouldn't discount Swing so quickly - IMHO – MadProgrammer Feb 02 '13 at 19:57
  • @MadProgrammer -- Well, Oracle's FAQ says: "Is JavaFX replacing Swing as the new client UI library for Java SE? Yes. However, Swing will remain part of the Java SE specification for the foreseeable future, and therefore included in the JRE. While we recommend developers to leverage JavaFX APIs as much as possible when building new applications, it is possible to extend a Swing application with JavaFX, allowing for a smoother transition." ( http://www.oracle.com/technetwork/java/javafx/overview/faq-1446554.html ) – Rop Feb 02 '13 at 20:28
  • Well, that's the first time I've seen them say that. There you go :D – MadProgrammer Feb 02 '13 at 20:41