Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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 404 times)
0 Members and 1 Guest are viewing this topic.
Offline semir91

Junior Newbie





« Posted 2013-10-24 17: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;
   }
}
Online Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #1 - Posted 2013-10-24 18: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 23: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 23: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-25 00:16:48 »

Nothing changed.
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (36 views)
2014-04-15 18:08:23

BurntPizza (32 views)
2014-04-15 03:46:01

UprightPath (47 views)
2014-04-14 17:39:50

UprightPath (30 views)
2014-04-14 17:35:47

Porlus (47 views)
2014-04-14 15:48:38

tom_mai78101 (68 views)
2014-04-10 04:04:31

BurntPizza (128 views)
2014-04-08 23:06:04

tom_mai78101 (228 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!