Jump to content
  • Advertisement
Sign in to follow this  
gandolf1212

Need to call a function when the user clicks the form in Java

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Need to make a simple card game in java. There is going to be a deck of cards drawn in the paint method. What I need to do is make a function that will be called when the user clicks on the form. I will also need to be able to get thier mouse position. Thanks.
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Cards extends JFrame{


	public void paint(Graphics g){
		super.paint(g);


	}

	public Cards(){

		super("Cards!");

		Container container = getContentPane();
		container.setLayout(null);

	};



	public static void main(String args[]){

		Cards deck = new Cards();

		deck.addWindowListener( new WindowAdapter(){

			public void windowClosing( WindowEvent event ){

				System.exit(0);

			}

		});

		deck.setSize(500, 360);

		deck.setVisible(true);

	};

};

Share this post


Link to post
Share on other sites
Advertisement
Thats basically where I'm at now.

Do I have to create a new, full class for it or can I create a new, nameless class?

Not sure if the example I have is unnessacarily complex or not, but might just copy it.

Share this post


Link to post
Share on other sites
You can either have you Cards class 'implements MouseListener', or use a MouseAdapter as a local nameless class. I suggest you use to mouse-listener.

Toolmaker

Share this post


Link to post
Share on other sites
Well, heres the problem now.

The example says I need to make a new public class to extend MouseAdapter.

Java doesn't let you make 2 public classes in one file, so I go and make 2 files.

Then the second file doesn't find the first one... or somthing. I forget how packages work in this language, and kind doubt I need to even make a new class anyway.

Share this post


Link to post
Share on other sites
Toolmaker, that sounds like a good idea.

This looking good? Am I missing something?


import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Cards extends JFrame implements MouseListener{


public void paint(Graphics g){
super.paint(g);


}

public Cards(){

super("Cards!");

Container container = getContentPane();
container.setLayout(null);

};

public void mousePressed(MouseEvent e){



};

public void mouseReleased(MouseEvent e){



};

public void mouseExited(MouseEvent e){



};

public void mouseEntered(MouseEvent e){



};

public void mouseClicked(MouseEvent e){



};


public static void main(String args[]){

Cards deck = new Cards();

deck.addWindowListener( new WindowAdapter(){

public void windowClosing( WindowEvent event ){

System.exit(0);

}

});

deck.setSize(500, 360);

deck.setVisible(true);

};

};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!