Results 1 to 5 of 5
  1. #1
    tigersarehot is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default 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 04:14 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    tigersarehot is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    For some reason my indents dont show up in my post...do you know how to fix this??

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Use CODE tags, not QUOTE tags, to post your code. Also make sure it's well-formatted.

    Java Code:
    package HotCold;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class game extends JFrame implements MouseMotionListener, 
    MouseListener{
    Choose a better class name -- "game" is a little vague, and in any case it should be capitalized, like Game.
    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()));
                }
    You should close your ActionListener and addActionListener here, and move your HotorCold() method outside the game constructor.
    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... =(";
                    }
                }
    It would be much cleaner and easier to read if you structured it this way:
    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 =)";
            }
    You have already guaranteed that value is a positive int so there are no other possibilities.
    Java Code:
            });
    Finally closed ActionListener and addActionListener -- should have done this much earlier.

    I'll leave the rest to someone else for now.

    -Gary-

Similar Threads

  1. Mouse events, are they best or only way to go?
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-10-2009, 05:34 PM
  2. Need help with looping mouse Events.
    By busdude in forum New To Java
    Replies: 1
    Last Post: 04-08-2009, 09:25 PM
  3. Replies: 1
    Last Post: 10-24-2008, 05:00 AM
  4. Menu item not working properly for mouse events
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 09:56 AM
  5. Demonstration of mouse events
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •