Results 1 to 7 of 7
  1. #1
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default mouseListener trouble please help!

    Hi everybody!

    I am working on a Battle Ship game, and i am having some issues with Mouse listener. The problem is i can not add a mouselistener to JFrame, or JPane. Why? here's my code below (all the other stuff works, just the line with the comment gives an error when i add it.

    Java Code:
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import javazoom.jl.player.Player;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    
    public abstract class BattleShip extends JFrame implements MouseListener
    {
        public static void showSplash()
    	{
    		MP3 pirates = new MP3("pirates.mp3");
    		pirates.play();
    		JFrame frame = new JFrame("Battle Ship");
            frame.setSize(490, 320);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            Image backgroundImage = null;
            String pathToTheImage = "splash.jpg";
            try 
    		{
                backgroundImage = ImageIO.read(new File(pathToTheImage));
            } 
    		catch (IOException e) 
    		{
                e.printStackTrace();
            }
            ImagePanel panel = new ImagePanel(backgroundImage);
    		
    		panel.addMouseListener(mouseClicked); // ******* ERROR ****/
            frame.getContentPane().add(panel);
    		frame.setResizable(false);
            frame.show();
    	}
    	
    	public static void showgameScreen()
    	{
    		JFrame frame = new JFrame("Battle Ship");
            frame.setSize(800, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            Image backgroundImage = null;
            String pathToTheImage = "grid.jpg";
            try 
    		{
                backgroundImage = ImageIO.read(new File(pathToTheImage));
            } 
    		catch (IOException e) 
    		{
                e.printStackTrace();
            }
            ImagePanel panel = new ImagePanel(backgroundImage);
    
            frame.getContentPane().add(panel);
    		frame.setResizable(false);
            frame.show();
    	}
    	
    	public void mouseClicked(MouseEvent e) 
    	{
            int x = e.getX();
    		int y = e.getY();
    		if ((x >= 370 && x <= 470) && (y >= 275 && y <= 230))
    		{
    			System.exit(1);
    		}
        }
    
    	
    	public static void main(String[] args) 
    	{
    		showSplash(); 
    	}
    }
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Let me give you a suggestion, and then I'll explain why.

    Put a single JPanel in the JFrame, and put everything else in the JPanel. You can set the JFrame layout manager to GridLayout with no parameters, and the JPanel will fill the JFrame. This is one of my "always do" practices.

    The reason is this: JFrame cannot hold components directly. It actually has a JRootPane, which in turn has several other panes. JFrame has "convenience methods" to add components and what not, which means that you can use the JFrame methods to work on the pane that actually holds the components, but that can lead to confusion.

    Adding a JPanel to the JFrame means that you have a component you defined that contains everything else, so you can see exactly what is going on.

    You can add a mouse listener to the JPanel. Keep in mind that if you add components to the JPanel, they may intercept the mouse events. I haven't experimented with that, so this is just something to keep in mind if things don't happen the way you expect.

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

    Default

    The error is because you're trying to pass a method to your addMosueListener method when instead you must pass a MouseListener object which in this situation would be "this".

    I agree with Steve that you should do your work in a JPanel. Also, try to avoid having your GUI classes implement Listener interfaces. You're much better off adding an anonymous MouseAdapter object to your addMouseListener method.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Oops... That's why Fubarable is the moderator, he actually reads the code!

  5. #5
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    Thanks for the help!
    I forgot about JPane (just started doing GUI's last week)
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    Also, show() is deprecated. Use setVisible(true) instead.

    db

  7. #7
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    ok, thanks
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

Similar Threads

  1. Mouselistener questions
    By jigglywiggly in forum New To Java
    Replies: 0
    Last Post: 05-07-2009, 09:59 PM
  2. Basic MouseListener Question
    By jshailes in forum AWT / Swing
    Replies: 9
    Last Post: 01-15-2009, 08:58 AM
  3. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 03:30 AM
  4. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 08:10 AM
  5. I need help with my MouseMotionAdapter and MouseListener.
    By MurderfaceX4 in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 03:13 AM

Posting Permissions

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