Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6907995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:30:17+00:00 2026-05-27T08:30:17+00:00

I have a card game that I am developing and need to load card

  • 0

I have a card game that I am developing and need to load card images to the screen. I am using ImageIcon and PaintIcon to Print to image. I want the images to be clickable but I dont know how to do this.

The reason I am using PaintIcon is because I want the images to move with with a click of a button.(stack cards to save space, spread to view them all)

I dont know what to search for or were to get started.

If any one can show me sample code or the right direction that would help.

This is the code I used:

public class CustomGraphicsDemo2 extends JFrame {

  // Define constances
  private static final int CANVAS_WIDTH = 640;
  private static final int CANVAS_HEIGHT = 480;

  //Array of image cards


  //Handle for the custom drawing panel
  private DrawCanvas canvas;

  private ImageIcon card1, card2, card3,card4;

  //Attributes of Drawing object
  private int x = 100;      // x and y position
  private int y = 100;
  private int size = 50;    
  private int xSpeed = 3;   // moving speed in x and y directions
  private int ySpeed = 5;

  //Constructor to create the UI components
 public CustomGraphicsDemo2() {
  canvas = new DrawCanvas();
  canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
  this.setContentPane(canvas);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.pack();
  this.setTitle("Custom Graphics Demo");
  this.setVisible(true);

  // Refresh the display at regular interval.
  // Run the display refresh code in its own thread.
  Thread updateThread = new Thread() {
     public void run() {
        while (true) {
           update();   // update the (x, y) position
           repaint();  // Refresh the JFrame, callback paintComponent()
           try {
              // Delay and give other thread a chance to run
              Thread.sleep(50);  // milliseconds
           } catch (InterruptedException ex) {}
        }
     }
  };
   updateThread.start();   // callback run()
 }

 // Update the (x, y) position of the graphical object
   public void update() {
   x += xSpeed;
   y += ySpeed;
   if (x > CANVAS_WIDTH - size || x < 0) {
     xSpeed = -xSpeed;
   }
   if (y > CANVAS_HEIGHT - size || y < 0) {
      ySpeed = -ySpeed;
   }
}


   //Custom drawing canvas (designed as inner class).
   class DrawCanvas extends JPanel {
   // Custom drawing codes
   @Override
   public void paintComponent(Graphics g) {

      super.paintComponent(g);
      setBackground(Color.BLACK);
      g.setColor(Color.BLUE);
      g.fillOval(x, y, size, size);  // draw a circle


      //cards being drawn
      card1 = new ImageIcon("Uno Cards/Blue/ EIGHT.png");
      card1.paintIcon(this, g, 50, 100);

      card2 = new ImageIcon("Uno Cards/Blue/FIVE.png");
      card2.paintIcon(this, g, 100, 100);

      card3 = new ImageIcon("Uno Cards/Blue/NINE.png");
      card3.paintIcon(this, g, 150, 100);

      card4 = new ImageIcon("Uno Cards/Blue/EIGHT.png");
      card4.paintIcon(this, g, x, y);

      //Graphics2D g2 = (Graphics2D) g; //we use this for drawing later. 
      //g2.fill(new drawImage());
      //g2.fill(new Rectangle2D.Double(10,y,size,size));

    }
 }

  // main program
  public static void main(String[] args) {

   SwingUtilities.invokeLater(new Runnable() {
      public void run() {
         new CustomGraphicsDemo2();
       }
    });
  }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:30:18+00:00Added an answer on May 27, 2026 at 8:30 am

    You can add your ImageIcon to a JLabel and then add that label to a panel.

    JLabel label = new JLabel(new ImageIcon(path));
    panel.add(label);
    

    A better way is to create separate components for each card. Have CardUI extend JComponent

    class CardUI extends JComponent {
        //... class members ...
    
        public CardUI(BufferedImage cardPhoto){
            this.cardPhoto= cardPhoto;
        }
    
        void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(cardPhoto, x, y, this);
        }
    }
    

    Edit:
    Forgot to mention, that now that you have a CardUI class, you can add listeners to it to see if the mouse is on this CardUI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple card game (using 52 cards - no jokers) that I
I have a script that successfully encrypts a credit card. I need it to
im developing a multi-player card game that needs the clients to communicate with each
I'm developing a card game that uses rummy-style sets of three cards. From a
I'm implementing a card game. In this game I have a Board that is
I'm developing a card playing game and would like to print out the symbol
For my assignment I have to put make a card memory game that allows
I have to develop an in-browser card game using silverlight. The game requires exactly
So I am writing a class that plays the card game war. I have
I have three canvases I'm using in a card game app I'm building. One

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.