9 Replies - 4340 Views - Last Post: 28 December 2012 - 02:46 AM Rate Topic: -----

#1 NOddi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 12

Images and MouseListener

Posted 23 December 2012 - 03:51 PM

Hey! Im trying to make a little game in java. Rock Paper Scissors, u know ;)/>
At the bottom of my screen ( Running in fullscreen mode) I have a image with the different moves a player can choose.
Is there a way to devide the image into three different parts and add listeners to all of them?

Or do i have to separate them in Photoshop and add them one by one ?

Or maby another way im not familiar with?

Is This A Good Question/Topic? 0
  • +

Replies To: Images and MouseListener

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: Images and MouseListener

Posted 23 December 2012 - 04:28 PM

You can use:

Graphics

public abstract boolean drawImage(Image img,
                int dx1,
                int dy1,
                int dx2,
                int dy2,
                int sx1,
                int sy1,
                int sx2,
                int sy2,
                ImageObserver observer);



the sx and sy parameters specify the source x and y locations of the image. You could split an image into 100 parts, if you like. As for MouseListeners, sure. You can monitor the x and y of the mouse via the listener, then do whatever you like.

This post has been edited by farrell2k: 23 December 2012 - 04:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Images and MouseListener

Posted 23 December 2012 - 04:30 PM

You can split the image yes :)
The BufferedImage class even has a method to do just that
example
BufferedImage image = ImageIO.read(new File("C:\\image.png"));
BufferdImage rock = image.getSubimage(0, 0, 40, 40); //x: 0, y: 0, width: 40, height: 40
...



Are you using swing? if you are, then just add the images as icons on three JLabel's :)
Was This Post Helpful? 2
  • +
  • -

#4 NOddi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 12

Re: Images and MouseListener

Posted 23 December 2012 - 04:51 PM

Tnx for fast reply!
I will try the JLabel method when im done eating christmas dinner ;)
Was This Post Helpful? 0
  • +
  • -

#5 NOddi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 12

Re: Images and MouseListener

Posted 26 December 2012 - 03:15 PM

I got the JLable method to work btw. I found out that im not a master to write gui and have a program that respond the way i want. Never done this before. Do anyone have a great way to start? The first code i wrote didnt work, so started over..
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: Images and MouseListener

Posted 26 December 2012 - 03:20 PM

What specific topics are you interested in? The Oracle tutorials are a great place to start, as well as the DIC tutorials.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Images and MouseListener

Posted 26 December 2012 - 07:44 PM

You can addMouseListener() to your JLabel if you make them setFocusable(true);
Was This Post Helpful? 0
  • +
  • -

#8 NOddi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 12

Re: Images and MouseListener

Posted 27 December 2012 - 03:57 PM

View Postmacosxnerd101, on 26 December 2012 - 03:20 PM, said:

What specific topics are you interested in? The Oracle tutorials are a great place to start, as well as the DIC tutorials.


Making the Gui is not the hardest. The hardest is to make it work with the user inputs. I got one problem now before the game is ready. That is a status text i want to draw to the screen. Im using drawString(), and that works fine for the first text i want to show, the text is make your choise. The next one is my problem. it works with system.out.println(). the next text will be usersChoise VS compureChoise (example. ROCK VS PAPER)
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: Images and MouseListener

Posted 27 December 2012 - 08:46 PM

Rather than painting and using drawString(), use a JLabel and setText() instead. If you have a submit JButton, you can add an ActionListener that updates the JLabels. The Oracle tutorial has good tutorial on ActionListeners.
Was This Post Helpful? 1
  • +
  • -

#10 NOddi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 12

Re: Images and MouseListener

Posted 28 December 2012 - 02:46 AM

Okey tnx! I will give it a try!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1