Reputation: 229
This is my class that everything is done in:
import java.awt.Color;
import java.awt.Graphics;
//This class will not compile until all
//abstract Locatable methods have been implemented
public class Block implements Locatable
{
//instance variables
private int xPos;
private int yPos;
private int width;
private int height;
private Color color;
//constructors
public Block () {}
public Block(int x,int y,int w,int h)
{
xPos = x;
yPos = y;
width = w;
height = h;
}
public Block(int x,int y,int w,int h, Color c)
{
xPos = x;
yPos = y;
width = w;
height = h;
color = c;
}
//set methods
public void setBlock(int x, int y, int w, int h)
{
xPos = x;
yPos = y;
width = w;
height = h;
}
public void setBlock(int x, int y, int w, int h, Color c)
{
xPos = x;
yPos = y;
width = w;
height = h;
color = c;
}
public void draw(Graphics window)
{
window.setColor(color);
window.fillRect(getX(), getY(), getWidth(), getHeight());
}
//get methods
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
//toString
public String toString()
{
String complete = getX() + " " + getY() + " " + getWidth() + " " + getHeight() + " java.awt.Color[r=" + color.getRed() + ", g=" + color.getGreen() + ", b=" + color.getBlue() + "]";
return complete;
}
}
and here is my interface class that has to be implemented:
public interface Locatable
{
public void setPos( int x, int y);
public void setX( int x );
public void setY( int y );
public int getX();
public int getY();
}
I haven't had formal instruction yet on interfaces/implementations and thus, am not sure what needs to be done to get the first class to run right
Upvotes: 0
Views: 633
Reputation: 46428
When you implementing an interface, you have to implement all the methods declared in that interface. Interface is a contract that your implementing class must full fill.in your case your implementing class Block should implement the following methods to full fill the contract.
public void setPos( int x, int y);
public void setX( int x );
public void setY( int y );
public int getX();
public int getY();
public class Block implements Locatable {
public void setPos( int x, int y){
// your implementatioon code
}
public void setX( int x ) {
// your implementatioon code
}
public void setY( int y ){
// your implementatioon code
}
public int getX(){
// your implementatioon code
return (an int value);
}
public int getY(){
// your implementatioon code
return (an int value);
}
}
EDIT: for your NPE from the comments.
you never initialized your Color object.and trying to call a method on its refrence in your toString method.
private Color color;
initialize it like this
private Color color = new Color(any of the Color constructors);
check here for Color API
Upvotes: 2