[java] Right Mouse Clicks

Started by
6 comments, last by Whackjack 20 years, 8 months ago
Some of you may remember me working on the Java version of Minesweeper. I''ve worked out some of the bugs, but I''ve run into something totally unexpected. I''m trying to get the right-click working where it flags one of the buttons. My MouseListener methods are working correctly, but it doesn''t exactly do what I want it to do.. i.e. if you try to flag before starting and then click a button, the whole array somehow inverts itself, some random buttons clear and the game ends abruptly. Or if you try to flag after you already started, the buttons will all change size and makes the game unplayable. I don''t want the buttons to disappear, obviously. I just want the icon to load onto the button. I thought I was doing it right, but I''ve exhausted my resources. The code is pretty long, so I''ll wait to post it. Can anyone give me a clue on how to change the icon on the button? Oh, and in the process of my searching, I discovered that the right mouse button on a scroll mouse is considered button 3. I assumed it was button 2, and the scroll was 3. Wouldn''t that have an impact on some using a non-scroll mouse? How would I adjust for that?
Advertisement
I vaguely remember a method for get number of buttons. Or you could create an options section and let the user pick which button they want each to do.

In terms of disappearing buttons, or automatically resizing buttons, that sounds very strange. However, I''m going to guess you''re using AWT, and I''m not surprised in the least. Let me guess though... You gave each section its own panel? That generally causes problems. I''m not sure if they''ve fixed those problems in 1.4.2. You may want to try upgrading.
It might be a better approach to manage and draw the entire field at once not as buttons but as a single Panel/JPanel. Override the paintComponent(Graphics g) method and use it to draw the squares that need to be drawn at that point. That way, you're not changing an icon on a Button/JButton, you're overwriting a part of the panel with a graphic. The MouseEvent object has getX() and getY() methods that get the co-ordinates of the click in relation to the component that was clicked on. I'm working on a minesweeper game right now and this is the way I plan to approach drawing the field.

Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,
fasting, good works and so on. Up until Doom, no one seemed to have thought about the
double-barrel shotgun. Eat leaden death, demon...
-- Terry Pratchett

[edited by - Kentaro on July 29, 2003 2:37:45 AM]
Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,fasting, good works and so on. Up until Doom, no one seemed to have thought about thedouble-barrel shotgun. Eat leaden death, demon... -- Terry Pratchett
That''s a pretty good idea, Kentaro.. Unfortunately, it''ll require me to completely overhaul my code. I might do that as a revision later on. I just want to get it working with what I have currently. Thanks for the suggestion.
quote:Original post by Whackjack
Oh, and in the process of my searching, I discovered that the right mouse button on a scroll mouse is considered button 3. I assumed it was button 2, and the scroll was 3. Wouldn''t that have an impact on some using a non-scroll mouse? How would I adjust for that?


No. 3 button mice have been around a long time. the right mouse button has always been 3, even on a 2 button mouse.

Are you using Swing or AWT? Are you using real buttons, or making your own out of Panel/JPanel? Why don''t you post a download link so we can have a look at it.


First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Here's a link to a zip containing the source and an image for the flag. I commented out the problem causing code. It's in the mouseClicked() method.

JavaMine source

I know my style is sloppy. Please be gentle. I'll clean it up and do optimizations after I get it functioning.

[edited by - Whackjack on August 4, 2003 3:04:33 PM]
Your problem is that you are removing buttons from your MinePanel and not replacing them with another component. So when you set the icon for a button, it causes a call to repaint or updateUITree or validate and everything is re-layed out. Since the number of components is smaller, everything is packed together and drawn. If you count the number of buttons before you set a flag and then after, you will find that the count is the same, just the layout has been redrawn.

This problem stems from the way you are doing your custom drawing. It is alright to do custom drawing in Java, but you should try to do it within the bounds of how Java works. This way your program plays well with LayoutManagers.

Here is what I suggest(won''t cause a major rewrite):

1. Take your MineButton class and change it to extend JPanel instead of JButton.

2. Move the custom drawing for each square inside of MineButton.

3. You can then add a JButton within the MineButton to the panel and then when it is removed, your custom drawing takes over.

4. Make sure that anything that does custom drawing overrides setPreferredSize, setMinimumSize and setMaximumSize. This will let your custom component play well with the layou manager.

5. Instead of drawing the border yourself, use setBorder of JPanel to have Java draw it for you.


First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Thanks Captain, I''ll see what I can do with your suggestions. The only thing that puzzles me is that the buttons aren''t resized and laid out when I use the ActionListener, but it does with the MouseListener. I guess that''s just one of the nuances of Java. Thanks for your help.

This topic is closed to new replies.

Advertisement