Results 1 to 5 of 5
- 04-28-2010, 01:27 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Help with some basics please (mouse events) -- FIXED
Hey
I'm trying to make a number guessing game in Java. The game part of it works perfectly, but I also have a jpg with the instructions for the game. Im trying to make the mouse clicked event hide the instructions and the mouse dragged event show the instructions. I don't see anything wrong with my code..but it doesnt seem to work. Can someone figure it out?
EDIT - My indents are working now. I've bolded everything relevant to make it easier.
Java Code:public class game extends JFrame implements[B] MouseMotionListener, MouseListener{[/B] private JTextField data, output; private JButton go; private Image inst; [B]private boolean showInst;[/B] private int randomNumber; //Constructor Method public game() { super("Hot and Cold"); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); data = new JTextField("00"); output = new JTextField(30); randomNumber= (int)(Math.random()*30); [B]showInst = true;[/B] start(); go = new JButton("Hot or Cold?"); go.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { output.setText("You are: " + HotorCold(data.getText())); } // Method - Check to see if Number is Hot or Cold private String HotorCold(String text) { //Convert string to integer int input = Integer.parseInt((data.getText()).trim()); int value = Math.abs(randomNumber-input); if (value >= 5 && value < 10){ return "Cold"; } if (value >= 10){ return "Very Cold"; } if (value > 2 && value < 5){ return "Hot"; } if (value <= 2){ return "Very Hot"; } if (value == 0){ return "You Got it =)"; } else { return "no idea what you are doing... =("; } } }); Container myPane = getContentPane(); myPane.setLayout(new FlowLayout()); myPane.add(go); myPane.add(data); myPane.add(output); validate(); this.addMouseMotionListener(this); this.addMouseListener(this); } //Main Method public static void main(String[] args) { new game(); } /* * Load Image * To display the instructions Image */ private Image loadImage(String inst) { Image img = null; try { Toolkit tk = Toolkit.getDefaultToolkit(); img = tk.getImage(inst); } catch (Exception e) { System.err.println("Image not found: "+ inst); } return img; } [B]public void start() { inst = loadImage("inst.jpg"); } public void paint(Graphics g) { super.repaint(); if (showInst == true) { g.drawImage(inst,50,50,this); }[/B] } [B]/* * Method - Mouse Motion Listener * Use Mouse dragged event to show instructions image */ //Use Mouse Dragged event to set image to visible. public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub showInst = true; repaint(); } /* * Method - Mouse Listener * Use Mouse clicked event to hide instructions image */ //Use Mouse clicked event to set image to not visible. public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub showInst = false; repaint(); } [/B]Last edited by tigersarehot; 04-28-2010 at 03:14 AM.
-
You may improve your chances of getting help if you post well-formatted code since the code you've posted is quite hard to read. Best of luck!
- 04-28-2010, 02:11 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
For some reason my indents dont show up in my post...do you know how to fix this??
- 04-28-2010, 02:21 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
hard to say, your code looks right. try to put a System.out.println("in method X") kind of thing in each of your mouse handling methods, and see in what order they execute. maybe somehow when you drag, mouseClicked() gets triggered as well.
- 04-28-2010, 02:22 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Use CODE tags, not QUOTE tags, to post your code. Also make sure it's well-formatted.
Choose a better class name -- "game" is a little vague, and in any case it should be capitalized, like Game.Java Code:package HotCold; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class game extends JFrame implements MouseMotionListener, MouseListener{
You should close your ActionListener and addActionListener here, and move your HotorCold() method outside the game constructor.Java Code:private JTextField data, output; private JButton go; private Image inst; private boolean showInst; private int randomNumber; //Constructor Method public game() { super("Hot and Cold"); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); data = new JTextField("00"); output = new JTextField(30); //variable - Have computer pick random number from 1 - 30 randomNumber= (int)(Math.random()*30); showInst = true; start(); go = new JButton("Hot or Cold?"); go.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { output.setText("You are: " + HotorCold(data.getText())); }
It would be much cleaner and easier to read if you structured it this way:Java Code:// Method - Check to see if Number is Hot or Cold private String HotorCold(String text) { //Convert string to integer int input = Integer.parseInt((data.getText()).trim()); int value = Math.abs(randomNumber-input); if (value >= 5 && value < 10){ return "Cold"; } if (value >= 10) { return "Very Cold"; } if (value > 2 && value < 5) { return "Hot"; } if (value <= 2) { return "Very Hot"; } if (value == 0) { return "You Got it =)"; } else { return "no idea what you are doing... =("; } }
You have already guaranteed that value is a positive int so there are no other possibilities.Java Code:if (value > 9) { return "Very Cold"; } else if (value > 4) { return "Cold"; } else if (value > 2) { return "Hot"; } else if (value > 0) { return "Very Hot"; } else { return "You Got it =)"; }
Finally closed ActionListener and addActionListener -- should have done this much earlier.Java Code:});
I'll leave the rest to someone else for now.
-Gary-
Similar Threads
-
Mouse events, are they best or only way to go?
By dbashby in forum New To JavaReplies: 2Last Post: 04-10-2009, 04:34 PM -
Need help with looping mouse Events.
By busdude in forum New To JavaReplies: 1Last Post: 04-08-2009, 08:25 PM -
How to get Mouse and keboard events via HTTP for remote access
By shahzadcreative in forum NetworkingReplies: 1Last Post: 10-24-2008, 04:00 AM -
Menu item not working properly for mouse events
By Preethi in forum New To JavaReplies: 1Last Post: 09-23-2008, 08:56 AM -
Demonstration of mouse events
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks