Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Abstract Method

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Abstract Method

    I would like to create a class file in which I can change how a method in it works by writing it in another class file. For example,

     button.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    System.out.println("You clicked the button");
                }
            });

    So I want to be able to create several of the same objects like you can in JButton, but then I want to be able to write a method type such as actionPerformed, so that each object has the same method name, but they do not do the same actions as for the other objects. Anybody know how I can do this?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Abstract Method

    Well, I'm not sure, I don't think so. This is my code, I'm programming with LWJGL.

    My Button Class. Where it says System.out.println("Action Button"); under the input method. I want to change that to a method called actionEvent(). But I don't want to write out the method in the Button Class. I want to be able to write it in another class file I call Main Menu, where I have several Button Objects. So each Button object must have a different actionEvent().

    Example in Main Menu Class:
    Button singlePlayer = new Button(10, 10, 100, 100, "texture.png"){
    public void actionEvent(){
    //load singlePlayer Menu
    };
    Button mutliPlayer = new Button(10, 150, 100, 100, "texture.png"){
    public void actionEvent(){
    //load multiPlayer Menu
    }
    };

    Now the problem is that both buttons will only load the mutliplay Menu.

    package gui;
     
    import java.awt.Rectangle;
     
    import org.lwjgl.input.Mouse;
    import org.lwjgl.opengl.Display;
    import org.lwjgl.opengl.GL11;
    import org.newdawn.slick.Color;
    import org.newdawn.slick.opengl.Texture;
    import org.newdawn.slick.opengl.TextureLoader;
    import org.newdawn.slick.util.ResourceLoader;
     
    public class Button {
    	private int width;
    	private int height;
    	private int x;
    	private int y;
    	private Rectangle bounds;
    	private float widthRatio;
    	private float heightRatio;
    	private Texture texture;
     
    	public Button(int x, int y, int width, int height, String res){
    		widthRatio = (float)((float)Display.getWidth())/1920.0f;
    		heightRatio = (float)((float)Display.getHeight())/1080.0f;
    		this.x = (int) (x * widthRatio);
    		this.y = (int) (y * heightRatio);
    		this.width = (int)((int)(width * widthRatio));
    		this.height = (int)((int)(height * heightRatio));
    		bounds = new Rectangle();
    		bounds.x = this.x;
    		bounds.width = this.x + this.width;
    		bounds.y = Display.getHeight() - this.y;
    		bounds.height = bounds.y - this.height;
    		try{
    			texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(res));
    		}catch(Exception e){}
    	}
     
    	public void input(){
    		if((Mouse.getX() >= bounds.x && Mouse.getX() <= bounds.width) && (Mouse.getY() <= bounds.y && Mouse.getY() >= bounds.height) && Mouse.getEventButton() == 0 && !Mouse.getEventButtonState()){
    			System.out.println("Action Button");
    		}
    	}
     
    	private void update(){}
     
    	public void render(){
    		update();
     
    		Color.white.bind();
    		texture.bind();
     
    		GL11.glBegin(GL11.GL_QUADS);
    		GL11.glTexCoord2f(0.0f, 0.0f);
    		GL11.glVertex2i(x, y);
    		GL11.glTexCoord2f(1.0f, 0.0f);
    		GL11.glVertex2i(x + width, y);
    		GL11.glTexCoord2f(1.0f, 1.0f);
    		GL11.glVertex2i(x + width, y + height);
    		GL11.glTexCoord2f(0.0f, 1.0f);
    		GL11.glVertex2f(x, y + height);
    		GL11.glEnd();
    	}
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Abstract Method

    Sounds like an interface would do the trick. You can follow the example that Swing provides for the JButton class:
    1) create an interface that defines the method(s) required (you could also re-use another standard interface, such as the ActionListener interface)
    2) Create an 'add' (or 'set') method to your Button class, in which an implementation of the interface from (1) is added to a collection or a field variable set
    3) When required in the Button class, call the appropriate method of the interface from (1) and (2).

    In your 'Main Menu Class', you can then create the Button, create an implementation of the interface (as you would for an ActionListener), and add it to the button.

  5. The Following User Says Thank You to copeg For This Useful Post:

    Damian3395 (May 28th, 2014)

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Abstract Method

    thanks for the help

Similar Threads

  1. Abstract Method
    By snowie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2014, 02:35 AM
  2. Replies: 1
    Last Post: May 20th, 2013, 01:19 AM
  3. Abstract method vs overloading
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: January 10th, 2013, 01:58 AM
  4. [SOLVED] Abstract method in a non-abstract class
    By bassie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 2nd, 2012, 10:27 AM
  5. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM