Sign in to follow this  

KeyListener use in app trouble.(Solved)

This topic is 3574 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

This is probably a simple concept that im missing. I basically want this java applet to utilize WASD keys for moving the oval around. Here's my source code. PathTest.java:
import java.awt.*;
import java.applet.*;

public class PathTest extends Applet
{
	private Ball b;
    public void init()
    {
    	this.setSize(300,300);
    	b = new Ball(0,0,10,10);
    }

    //This method gets called when the applet is terminated
    public void stop()
    {
    
    }


    public void paint(Graphics g)
    {
    	b.drawBall(g);
    	g.drawString("x: "+ b.getX() , 200, 50);
    	g.drawString("y: "+ b.getX() , 250, 50);
    	this.repaint();
    } 
}


Ball.java:
import java.awt.*;

public class Ball 
{
	private int x, y, sizex, sizey;
	private InputListener il;
	public Ball(int x, int y, int sizex, int sizey)
	{
		this.x = x;
		this.y = y;
		this.sizex = sizex;
		this.sizey = sizey;
		il = new InputListener(this);
	}
	
	public void drawBall(Graphics g)
	{
		g.fillOval(x, y, sizex, sizey);
	}
	
	public void setX(int x)
	{
		this.x = x;
	}
	public void setY(int y)
	{
		this.y = y;
	}
	public int getX()
	{
		return x;
	}
	public int getY()
	{
		return y;
	}
}


InputListener.java: The mouselistener is there for future use, as Im just using WASD for a base of input right now(been a while since ive done this).
import java.awt.event.*;
import java.awt.*;

public class InputListener implements KeyListener, MouseListener
{
	private int x, y, sizex, sizey;
	private Ball b;
	public InputListener(Ball b)
	{
		 this.b = b;
	}
	public void keyPressed( KeyEvent e ) 
	{ 
		if(e.getKeyCode() == e.VK_D)
		{
			if(x <= 298)
			{
				x += 2;
				b.setX(x);
			}
			else
			{
				x += 0;
				b.setX(x);
			}
		}
		if(e.getKeyCode() == e.VK_A)
		{
			if(x >= 2)
			{
				x -= 2;
				b.setX(x);
			}
			else
			{
				x -= 0;
				b.setX(x);
			}
		}
		if(e.getKeyCode() == e.VK_S)
		{
			if(y >= 2)
			{
				y += 2;
				b.setY(y);
			}
			else
			{
				y += 0;
				b.setY(y);
			}
		}
		if(e.getKeyCode() == e.VK_W)
		{
			if(y <= 298)
			{
				y -= 2;
				b.setY(y);
			}
			else
			{
				y -= 0;
				b.setY(y);
			}
		}
	}
	public void keyReleased( KeyEvent e ) 
	{ 
		
	}
	public void keyTyped( KeyEvent e ) 
	{
	  
	}
	
	
	public void mouseEntered( MouseEvent e ) 
	{ 
		
	}
	public void mouseExited( MouseEvent e ) 
	{ 
		
	}
	public void mousePressed( MouseEvent e ) 
	{ 
		
	}
	public void mouseReleased( MouseEvent e ) 
	{ 
		
	}
	public void mouseClicked( MouseEvent e ) 
	{
	     
	}
}



[Edited by - agm_ultimatex on March 3, 2008 9:42:33 PM]

Share this post


Link to post
Share on other sites
Not sure what problem you are having, probably want to specify that. But if you want your applet to respond to key pressed you should add a listener.


public void init()
{
this.setSize(300,300);
b = new Ball(0, 0, 10, 10);
this.addKeyListener(new InputListener(b));
}


Share this post


Link to post
Share on other sites
Quote:
Original post by HappyCoder
Not sure what problem you are having, probably want to specify that. But if you want your applet to respond to key pressed you should add a listener.

*** Source Snippet Removed ***


My problem is that essentially nothing happens. I have the two draw strings to essentially show the x and y position of the ball. I want to get those numbers changing at least, so i know keyboard input is working. After that, I will apply it to the ball it self.

Share this post


Link to post
Share on other sites
Your InputListener class already seems to modify the ball's coordinates, and HappyCoder is right. If you want to be notified of key events, you will need to add your inputlistener somewhere. This is likely best done in the PathTest.init function and looks like this:
this.addKeyListener( new InputListener( b ) );


Regards,

Rogier

Share this post


Link to post
Share on other sites
Yeah i added that in, the x and y values still do not change. Here is my current version:


//PathTest.java

import java.awt.*;
import java.applet.*;

public class PathTest extends Applet
{
private Ball b;
private Graphics g;
public PathTest()
{

}
public void init()
{
this.setSize(300,300);
b = new Ball();
this.addKeyListener(new InputListener(this));
}

//This method gets called when the applet is terminated
public void stop()
{

}


public void paint(Graphics g)
{
this.g = g;
b.drawBall(g);
}
public void wKey()
{
int y = b.getY();
if(y <= 298)
{
y -= 2;
b.setY(y);
b.drawBall(g);
}
else
{
y -= 0;
b.setY(y);
}
}
public void aKey()
{
int x = b.getX();
if(x >= 2)
{
x -= 2;
b.setX(x);
b.drawBall(g);
}
else
{
x -= 0;
b.setX(x);
}
}
public void sKey()
{
int y = b.getY();
if(y >= 2)
{
y += 2;
b.setY(y);
b.drawBall(g);
}
else
{
y += 0;
b.setY(y);
}
}
public void dKey()
{
int x = b.getX();
if(x <= 298)
{
x += 2;
b.setX(x);
b.drawBall(g);
}
else
{
x += 0;
b.setX(x);
}
}
}

//Ball.java

import java.awt.*;

public class Ball
{
private int x, y, sizeX, sizeY;
public Ball()
{
x = 0;
y = 0;
sizeX = 10;
sizeY = 10;
}

public void drawBall(Graphics g)
{
g.fillOval(getX(), getY(), getSizeX(), getSizeY());
g.drawString("x: "+ getX() , 200, 50);
g.drawString("y: "+ getX() , 250, 50);
}

public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setSizeX(int x)
{
this.x = x;
}
public void setSizeY(int y)
{
this.y = y;
}
public int getSizeX()
{
return sizeX;
}
public int getSizeY()
{
return sizeY;
}

}

//InputListener.java

import java.awt.event.*;

public class InputListener implements KeyListener, MouseListener
{
private PathTest pt;
public InputListener(PathTest pt)
{
this.pt = pt;
}
public void keyPressed( KeyEvent e )
{
if(e.getKeyCode() == e.VK_D)
{
pt.wKey();
}
if(e.getKeyCode() == e.VK_A)
{
pt.aKey();
}
if(e.getKeyCode() == e.VK_S)
{
pt.sKey();
}
if(e.getKeyCode() == e.VK_W)
{
pt.wKey();
}
}
public void keyReleased(KeyEvent e)
{

}
public void keyTyped(KeyEvent e)
{

}


public void mouseEntered(MouseEvent e)
{

}
public void mouseExited(MouseEvent e)
{

}
public void mousePressed(MouseEvent e)
{

}
public void mouseReleased(MouseEvent e)
{

}
public void mouseClicked(MouseEvent e)
{

}
}





Note, in InputListener, eclipse gives me a warning for each of the e.VK_<key>
Saying: "The static field KeyEvent.VK_W should be accessed in a static way."

Share this post


Link to post
Share on other sites

This topic is 3574 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this