Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  mouseListener problems  (Read 577 times)
0 Members and 1 Guest are viewing this topic.
Offline semir91

Junior Newbie





« Posted 2013-10-24 15:51:40 »

Hi everyone, I have some problems with my mouseListener. I'm developing a card game and I wanted to draw cards when the deck is clicked. Is it possible? Or do I need to make a mouseListener in my Board class, take the coordinates when the mouse was clicked and compare them to the coordinates of the deck?

Here's the code of my Board class.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
public class Board extends JPanel{
   
   public OrderCardsDeck deck;
   public Image img;
   private JFrame frame;

   public Board(JFrame frame){
      this.frame = frame;
     
      deck = new OrderCardsDeck();
     
      setFocusable(true);
      ImageIcon i = new ImageIcon("images/GtR.jpg");
      img = i.getImage();
      //help();
   }
   
   public void paint(Graphics g){
      int width = frame.getWidth();
      int height = frame.getHeight();
      super.paint(g);
      Graphics2D g2d = (Graphics2D) g;
     
      g2d.drawImage(img, 0, 0, width, height, null);
      g2d.drawImage((Image) deck.getImage(), 307, 231, null);
      g.drawString(""+deck.getCardsAmmount(), 343, 370);
   }
   
   
   //only for help in the construction of the board
   public void help(){
      this.addMouseListener(new MouseListener(){
         @Override
         public void mouseClicked(MouseEvent e) {
            System.out.println(e.getX()+", " + e.getY());  
         }
         @Override
         public void mousePressed(MouseEvent e) {}
         @Override
         public void mouseReleased(MouseEvent e) {}
         @Override
         public void mouseEntered(MouseEvent e) {}
         @Override
         public void mouseExited(MouseEvent e) {}  
      });
   }
}



And my OrderCardsDeck code, the class Deck is extending Components

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
public class OrderCardsDeck extends Decks{
   
   private int cards_amount = 144;
   private ArrayList<Cards> cards = new ArrayList<Cards>();
   
   public OrderCardsDeck(){
      setImage("images/back_m.jpg");
      this.addMouseListener(new MouseListener(){
         
         @Override
         public void mouseClicked(MouseEvent e) {
            cards_amount--;
            System.out.println(cards_amount);
            repaint();
         }
         public void mousePressed(MouseEvent e) {}
         public void mouseReleased(MouseEvent e) {}
         public void mouseEntered(MouseEvent e) {}
         public void mouseExited(MouseEvent e) {}
      });
   }

   public int getCardsAmmount() {
      return cards_amount;
   }
}
Offline Abuse

JGO Knight


Medals: 15


falling into the abyss of reality


« Reply #1 - Posted 2013-10-24 16:32:25 »

Two problems I can see:

- Deck isn't being added to the Component tree (it should be a child of Board), thus it isn't receiving events.
- Board is handling the redrawing of Deck; Deck should be doing this in its own paint method.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline semir91

Junior Newbie





« Reply #2 - Posted 2013-10-24 21:50:56 »

Ok, thanks. The problem with the mouseListener is solved. But now the problem is the image of the orderCardsDeck it's not drawn or it's just invisible.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
public class Frame {
   
   static Board board;
   static OrderCardsDeck orderCardsDeck;
   
   public static void main(String[] args){
      JFrame frame = new JFrame("Glory to Rome");
     
      board = new Board();
      board.setFocusTraversalKeysEnabled(true);
      board.img = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Semir\\Pictures\\Glory to Rome\\Na-chwale-Rzymu-_bw42706.jpg");
      frame.getContentPane().add(board, BorderLayout.CENTER);
      board.setLayout(null);
     
      orderCardsDeck = new OrderCardsDeck();
      orderCardsDeck.setBounds(new Rectangle(343, 370, 93, 130));
      orderCardsDeck.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
      orderCardsDeck.image = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Semir\\workspace\\GloryToRome\\images\\back_m.jpg");
      orderCardsDeck.setBounds(343, 370, 93, 130);
      board.add(orderCardsDeck);
     
      frame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Semir\\Pictures\\Glory to Rome\\Glory-to-Rome-Na-chwale-Rzymu-_bc23417.jpg"));
     
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
     
      frame.setMinimumSize(new Dimension(400, 400));
      frame.setLocationRelativeTo(null);
     
      frame.validate();

      frame.setVisible(true);

   }
   
   public void paint(Graphics g){
      orderCardsDeck.paint(g);
      board.paint(g);
   }
   
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dxu1994
« Reply #3 - Posted 2013-10-24 21:56:03 »

Change:

1  
2  
3  
4  
public void paint(Graphics g){
      orderCardsDeck.paint(g);
      board.paint(g);
   }


To:

1  
2  
3  
4  
5  
public void paint(Graphics g){
      board.paint(g);
      orderCardsDeck.paint(g);
     
   }

Offline semir91

Junior Newbie





« Reply #4 - Posted 2013-10-24 22:16:48 »

Nothing changed.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (23 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (96 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (152 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!