Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-28-2009, 05:48 AM
linux1man's Avatar
Member
 
Join Date: Nov 2008
Posts: 39
Rep Power: 0
linux1man is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-28-2009, 06:17 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-28-2009, 06:38 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,463
Rep Power: 8
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-28-2009, 06:58 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
Default
Oops... That's why Fubarable is the moderator, he actually reads the code!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-30-2009, 04:57 AM
linux1man's Avatar
Member
 
Join Date: Nov 2008
Posts: 39
Rep Power: 0
linux1man is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-30-2009, 09:33 AM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 733
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
Also, show() is deprecated. Use setVisible(true) instead.

db
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-03-2009, 02:57 AM
linux1man's Avatar
Member
 
Join Date: Nov 2008
Posts: 39
Rep Power: 0
linux1man is on a distinguished road
Default
ok, thanks
__________________
-- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
-- 2.66 Ghz Pentium Geforce NVidia 440 64mb
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouselistener questions jigglywiggly New To Java 0 05-07-2009 10:59 PM
Basic MouseListener Question jshailes AWT / Swing 9 01-15-2009 09:58 AM
i need help for MouseListener sfaxianovic New To Java 2 08-21-2008 04:30 AM
MouseListener Aswq New To Java 12 07-18-2008 09:10 AM
I need help with my MouseMotionAdapter and MouseListener. MurderfaceX4 New To Java 1 12-07-2007 04:13 AM


All times are GMT +2. The time now is 07:22 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org