PictureBox in Java

For a class project, I'm creating a card game (BlackJack in particular).  I beleive (and correct me if I'm wrong in this) that the best way to tackle this is using a frame and event driven type programming.  But I am having great difficulty finding something along the lines of a picture box.  Something where I can drop the image of my card into some random control and be able to drag and drop within my program.  While the dragging and dropping is not particularly critical for THIS program, if I wanted to make, say, a solitare program, I would need to be able to use the same basic layout.  I'm willing to abandon whatever and go to something else BUT since this IS a java class I'm taking, I will need to continue to use this language.  Thanks for your help.

-Chris

ps - as a note, I would like to use png graphics whithin
lillockeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NaeemgCommented:
Use JLable or JEditorpane to dispaly pictures.
Dejan PažinHead of SW DevelopmentCommented:

If you are creating a game, such as a card game for example, you best use JPanel in which you override the paint() method. In that method you simplt paint the image at coordinates where the mouse cursor is. Here is an example that will show you how to do it. Drag the image with mouse and drop it.

The only thing you have to change is the path to you image.


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseAdapter;

public class PictureMotion extends JFrame
{
   private int x = 0;
   private int y = 0;
   private int w = 0;
   private int h = 0;
   private int dx = 0;
   private int dy = 0;
   private boolean isMouseOver = false;

   /// Put the path to your image here:
   private Image myImage = getToolkit().getImage("path_to_your_image.png");

   public PictureMotion()
   {
      MediaTracker mt = new MediaTracker(this);
      mt.addImage(myImage, 0);
      try
      {
         mt.waitForID(0);
      }
      catch (InterruptedException e)
      {
         e.printStackTrace();
      }
      w = myImage.getWidth(null);
      h = myImage.getHeight(null);
      setSize(400, 400);
      MyPanel myPanel = new MyPanel();
      myPanel.addMouseListener(new MouseAdapter()
      {
         public void mousePressed(MouseEvent e)
         {
           if (isMouseOver)
           {
              dx = e.getX() - x;
              dy = e.getY() - y;
           }
         }
         public void mouseReleased(MouseEvent e)
         {
            if (isMouseOver)
            {
               x = e.getX() - dx;
               y = e.getY() - dy;
               repaint();
            }
         }
      });

      myPanel.addMouseMotionListener(new MouseMotionListener()
      {
         public void mouseDragged(MouseEvent e)
         {
            if (isMouseOver)
            {
               x = e.getX() - dx;
               y = e.getY() - dy;
               repaint();
            }
         }

         public void mouseMoved(MouseEvent e)
         {
            isMouseOver = ((e.getX() > x) && (e.getX() < (x + w)) && (e.getY() > y) && (e.getY() < (y + h)));
            repaint();
         }
      });

      getContentPane().add(myPanel);
      setVisible(true);

   }

   public static void main(String[] args)
   {
      new PictureMotion();
   }

   private class MyPanel extends JPanel
   {
      public void paint(Graphics g)
      {
         super.paint(g);
         if (isMouseOver)
         {
            g.setColor(Color.black);
            g.fillRect(x - 2, y - 2, w + 4, h + 4);
         }
         g.drawImage(myImage, x, y, null);
       }
   }
}

lillockeyAuthor Commented:
Perhaps I am too n00b for this but I'm confused on adding a MouseMotionListener.  I've done similar for a button using something along these lines:

            JButton c1 = new JButton("click me");
            getContentPane().add(c1);
            c1.addActionListener(new ActionListener);

I'm not sure how to do this with a JLabel.  I noticed the addMouseMotionListener method but I'm not sure how to actually add it given that I got an error any time I tried using the MouseMotionListener type.  Thank you all for your help.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Dejan PažinHead of SW DevelopmentCommented:

Did you try the demo I posted? There is no JLabel there. You can just compile and run the class and it will work, just change the path_to_your_image.png with the path to your image.

And here is how you add a MouseMotionListener to a label: (You add a MouseListener in a similar way, its all in the code I posted, please take a look there also)

JLabel label = new JLabel("my label");

      label.addMouseMotionListener(new MouseMotionListener()
      {
         public void mouseDragged(MouseEvent e)
         {
             /// put whatever you need to do when the mouse is dragged here
         }

         public void mouseMoved(MouseEvent e)
         {
              //// put whatever you need to do when the mouse is moved here
         }
      });
lillockeyAuthor Commented:
I tried your example using the following:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseAdapter;

public class PictureMotion extends JFrame
{
   private int x = 0;
   private int y = 0;
   private int w = 0;
   private int h = 0;
   private int dx = 0;
   private int dy = 0;
   private boolean isMouseOver = false;

   /// Put the path to your image here:
   private Image myImage = getToolkit().getImage("file:///e:/004%20Programming/Java%20Workspace/BlackJack/Cards/Standard/back.png");

   public PictureMotion()
   {
      MediaTracker mt = new MediaTracker(this);
      mt.addImage(myImage, 0);
      try
      {
         mt.waitForID(0);
      }
      catch (InterruptedException e)
      {
         e.printStackTrace();
      }
      w = myImage.getWidth(null);
      h = myImage.getHeight(null);
      setSize(400, 400);
      MyPanel myPanel = new MyPanel();
      myPanel.addMouseListener(new MouseAdapter()
      {
         public void mousePressed(MouseEvent e)
         {
           if (isMouseOver)
           {
              dx = e.getX() - x;
              dy = e.getY() - y;
           }
         }
         public void mouseReleased(MouseEvent e)
         {
            if (isMouseOver)
            {
               x = e.getX() - dx;
               y = e.getY() - dy;
               repaint();
            }
         }
      });

      myPanel.addMouseMotionListener(new MouseMotionListener()
      {
         public void mouseDragged(MouseEvent e)
         {
            if (isMouseOver)
            {
               x = e.getX() - dx;
               y = e.getY() - dy;
               repaint();
            }
         }

         public void mouseMoved(MouseEvent e)
         {
            isMouseOver = ((e.getX() > x) && (e.getX() < (x + w)) && (e.getY() > y) && (e.getY() < (y + h)));
            repaint();
         }
      });

      getContentPane().add(myPanel);
      setVisible(true);

   }

   public static void main(String[] args)
   {
      new PictureMotion();
   }

   private class MyPanel extends JPanel
   {
      public void paint(Graphics g)
      {
         super.paint(g);
         if (isMouseOver)
         {
            g.setColor(Color.black);
            g.fillRect(x - 2, y - 2, w + 4, h + 4);
         }
         g.drawImage(myImage, x, y, null);
       }
   }
}

//All I get is a blank frame
Dejan PažinHead of SW DevelopmentCommented:


Its because the path to your file. Try to use the following line:

private Image myImage = getToolkit().getImage("e:\\004 Programming\\Java Workspace\\BlackJack\\Cards\\Standard\\back.png");


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lillockeyAuthor Commented:
I'll play with this a bit but it looks like it'll be good.  I'll post here again after some basic testing.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.