Display images of cards from a file










1















I need to code my program to randomly select three cards from a deck of cards. The images are in a folder on my desktop. I then display the images of the cards. My program runs fine as far as reading from the file and then randomly picking 3 cards out of the 52. I have the program printing the 3 random cards in the output. But I can not see the images of the cards when the java window opens up on my computer. It displays a small line that just continuously repeats. So I think it is trying to display the cards but I have something wrong somewhere that is keeping the whole card(s) from displaying.
Thanks in advance for your help!



import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class DisplayThreeCards

public static void main(String args)

// File location
File folder = new File("//Users//macuser//Desktop//DeckOfCards");
File fileList;
fileList = folder.listFiles();

// Generate random card. Uses the Math.rand() function.
// The '%' operator is used to make sure the random is between 0-52.
int randomCard = (int) (Math.random() * 1000);
System.out.println(fileList[randomCard % 52].getAbsolutePath());

// Print the filename.
// Card 1
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);
// Card 2
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);
// Card 3
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);

try
ImageFrame frame = new ImageFrame(fileList[randomCard % 52].getAbsolutePath());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
catch (Exception E)



// Create a window for the card images to be displayed
class ImageFrame extends JFrame

public ImageFrame(String name)
setTitle("ImageTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

ImageComponent component = new ImageComponent(name);
add(component);


public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;


class ImageComponent extends JComponent

private static final long serialVersionUID = 1L;
private Image image;

public ImageComponent(String name)
try
File image2 = new File(name);
image = ImageIO.read(image2);
catch (IOException e)



@Override
public void paintComponent(Graphics g)
if (image == null)

int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);

g.drawImage(image, 50, 50, this);

for (int i = 0; i * imageWidth <= getWidth(); i++)
for (int j = 0; j * imageHeight <= getHeight(); j++)
if (i + j > 0)
g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);

















share|improve this question



















  • 1





    there can be an error during the loading of an image, add an output to all catch (Exception E) blocks to see it. E.g. E.printStackTrace()

    – Sergey Grinev
    Nov 15 '18 at 18:11












  • In addition to the advice of @SergeyGrinev .. 1) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL. 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and ..

    – Andrew Thompson
    Nov 15 '18 at 23:40











  • .. use it consistently. 3) The paintComponent method of the ImageComponent class should immediately call the super method.

    – Andrew Thompson
    Nov 15 '18 at 23:42















1















I need to code my program to randomly select three cards from a deck of cards. The images are in a folder on my desktop. I then display the images of the cards. My program runs fine as far as reading from the file and then randomly picking 3 cards out of the 52. I have the program printing the 3 random cards in the output. But I can not see the images of the cards when the java window opens up on my computer. It displays a small line that just continuously repeats. So I think it is trying to display the cards but I have something wrong somewhere that is keeping the whole card(s) from displaying.
Thanks in advance for your help!



import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class DisplayThreeCards

public static void main(String args)

// File location
File folder = new File("//Users//macuser//Desktop//DeckOfCards");
File fileList;
fileList = folder.listFiles();

// Generate random card. Uses the Math.rand() function.
// The '%' operator is used to make sure the random is between 0-52.
int randomCard = (int) (Math.random() * 1000);
System.out.println(fileList[randomCard % 52].getAbsolutePath());

// Print the filename.
// Card 1
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);
// Card 2
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);
// Card 3
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);

try
ImageFrame frame = new ImageFrame(fileList[randomCard % 52].getAbsolutePath());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
catch (Exception E)



// Create a window for the card images to be displayed
class ImageFrame extends JFrame

public ImageFrame(String name)
setTitle("ImageTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

ImageComponent component = new ImageComponent(name);
add(component);


public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;


class ImageComponent extends JComponent

private static final long serialVersionUID = 1L;
private Image image;

public ImageComponent(String name)
try
File image2 = new File(name);
image = ImageIO.read(image2);
catch (IOException e)



@Override
public void paintComponent(Graphics g)
if (image == null)

int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);

g.drawImage(image, 50, 50, this);

for (int i = 0; i * imageWidth <= getWidth(); i++)
for (int j = 0; j * imageHeight <= getHeight(); j++)
if (i + j > 0)
g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);

















share|improve this question



















  • 1





    there can be an error during the loading of an image, add an output to all catch (Exception E) blocks to see it. E.g. E.printStackTrace()

    – Sergey Grinev
    Nov 15 '18 at 18:11












  • In addition to the advice of @SergeyGrinev .. 1) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL. 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and ..

    – Andrew Thompson
    Nov 15 '18 at 23:40











  • .. use it consistently. 3) The paintComponent method of the ImageComponent class should immediately call the super method.

    – Andrew Thompson
    Nov 15 '18 at 23:42













1












1








1








I need to code my program to randomly select three cards from a deck of cards. The images are in a folder on my desktop. I then display the images of the cards. My program runs fine as far as reading from the file and then randomly picking 3 cards out of the 52. I have the program printing the 3 random cards in the output. But I can not see the images of the cards when the java window opens up on my computer. It displays a small line that just continuously repeats. So I think it is trying to display the cards but I have something wrong somewhere that is keeping the whole card(s) from displaying.
Thanks in advance for your help!



import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class DisplayThreeCards

public static void main(String args)

// File location
File folder = new File("//Users//macuser//Desktop//DeckOfCards");
File fileList;
fileList = folder.listFiles();

// Generate random card. Uses the Math.rand() function.
// The '%' operator is used to make sure the random is between 0-52.
int randomCard = (int) (Math.random() * 1000);
System.out.println(fileList[randomCard % 52].getAbsolutePath());

// Print the filename.
// Card 1
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);
// Card 2
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);
// Card 3
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);

try
ImageFrame frame = new ImageFrame(fileList[randomCard % 52].getAbsolutePath());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
catch (Exception E)



// Create a window for the card images to be displayed
class ImageFrame extends JFrame

public ImageFrame(String name)
setTitle("ImageTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

ImageComponent component = new ImageComponent(name);
add(component);


public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;


class ImageComponent extends JComponent

private static final long serialVersionUID = 1L;
private Image image;

public ImageComponent(String name)
try
File image2 = new File(name);
image = ImageIO.read(image2);
catch (IOException e)



@Override
public void paintComponent(Graphics g)
if (image == null)

int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);

g.drawImage(image, 50, 50, this);

for (int i = 0; i * imageWidth <= getWidth(); i++)
for (int j = 0; j * imageHeight <= getHeight(); j++)
if (i + j > 0)
g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);

















share|improve this question
















I need to code my program to randomly select three cards from a deck of cards. The images are in a folder on my desktop. I then display the images of the cards. My program runs fine as far as reading from the file and then randomly picking 3 cards out of the 52. I have the program printing the 3 random cards in the output. But I can not see the images of the cards when the java window opens up on my computer. It displays a small line that just continuously repeats. So I think it is trying to display the cards but I have something wrong somewhere that is keeping the whole card(s) from displaying.
Thanks in advance for your help!



import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class DisplayThreeCards

public static void main(String args)

// File location
File folder = new File("//Users//macuser//Desktop//DeckOfCards");
File fileList;
fileList = folder.listFiles();

// Generate random card. Uses the Math.rand() function.
// The '%' operator is used to make sure the random is between 0-52.
int randomCard = (int) (Math.random() * 1000);
System.out.println(fileList[randomCard % 52].getAbsolutePath());

// Print the filename.
// Card 1
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);
// Card 2
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);
// Card 3
System.out.println(fileList[randomCard % 52].getName());
randomCard = (int) (Math.random() * 1000);

try
ImageFrame frame = new ImageFrame(fileList[randomCard % 52].getAbsolutePath());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
catch (Exception E)



// Create a window for the card images to be displayed
class ImageFrame extends JFrame

public ImageFrame(String name)
setTitle("ImageTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

ImageComponent component = new ImageComponent(name);
add(component);


public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;


class ImageComponent extends JComponent

private static final long serialVersionUID = 1L;
private Image image;

public ImageComponent(String name)
try
File image2 = new File(name);
image = ImageIO.read(image2);
catch (IOException e)



@Override
public void paintComponent(Graphics g)
if (image == null)

int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);

g.drawImage(image, 50, 50, this);

for (int i = 0; i * imageWidth <= getWidth(); i++)
for (int j = 0; j * imageHeight <= getHeight(); j++)
if (i + j > 0)
g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);














java image swing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 23:38









Andrew Thompson

154k28164349




154k28164349










asked Nov 15 '18 at 17:38









S. CruzS. Cruz

62




62







  • 1





    there can be an error during the loading of an image, add an output to all catch (Exception E) blocks to see it. E.g. E.printStackTrace()

    – Sergey Grinev
    Nov 15 '18 at 18:11












  • In addition to the advice of @SergeyGrinev .. 1) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL. 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and ..

    – Andrew Thompson
    Nov 15 '18 at 23:40











  • .. use it consistently. 3) The paintComponent method of the ImageComponent class should immediately call the super method.

    – Andrew Thompson
    Nov 15 '18 at 23:42












  • 1





    there can be an error during the loading of an image, add an output to all catch (Exception E) blocks to see it. E.g. E.printStackTrace()

    – Sergey Grinev
    Nov 15 '18 at 18:11












  • In addition to the advice of @SergeyGrinev .. 1) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL. 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and ..

    – Andrew Thompson
    Nov 15 '18 at 23:40











  • .. use it consistently. 3) The paintComponent method of the ImageComponent class should immediately call the super method.

    – Andrew Thompson
    Nov 15 '18 at 23:42







1




1





there can be an error during the loading of an image, add an output to all catch (Exception E) blocks to see it. E.g. E.printStackTrace()

– Sergey Grinev
Nov 15 '18 at 18:11






there can be an error during the loading of an image, add an output to all catch (Exception E) blocks to see it. E.g. E.printStackTrace()

– Sergey Grinev
Nov 15 '18 at 18:11














In addition to the advice of @SergeyGrinev .. 1) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL. 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and ..

– Andrew Thompson
Nov 15 '18 at 23:40





In addition to the advice of @SergeyGrinev .. 1) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL. 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and ..

– Andrew Thompson
Nov 15 '18 at 23:40













.. use it consistently. 3) The paintComponent method of the ImageComponent class should immediately call the super method.

– Andrew Thompson
Nov 15 '18 at 23:42





.. use it consistently. 3) The paintComponent method of the ImageComponent class should immediately call the super method.

– Andrew Thompson
Nov 15 '18 at 23:42












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325089%2fdisplay-images-of-cards-from-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325089%2fdisplay-images-of-cards-from-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3