Hallo Leute, ich bin zwar nicht gerade ein Anfänger, was Java angeht, aber im Moment komme ich echt nicht mehr weiter! :-( Ich versuche ganz einfach einer JComponent einen KeyListener anzuhängen, aber es werden einfach keine Events geworfen? Gibt es vielleicht bestimmte Einschränkungen, die ich beachten muss?
Die Eventmethode "keyPressed" wird einfach niemals abgefeuert! :bahnhof:
Code:
// ######################################## class Board #######################################
public class Board extends JComponent
implements Runnable,KeyListener,MouseListener,MouseMotionListener,GameListener{
// ---------------------------------------- constants ----------------------------------------
public static final byte GRP_SRC = 0x01; // source group
public static final byte GRP_DST = 0x01; // destination group
public static final byte GRP_MIL = 0x02; // mill group
// -------------------------------------------------------------------------------------------
// ------------------------------------- dynamic members -------------------------------------
private Game gam; // the game instance to render
private int[] arrCoo; // coords representing the position of each field
private BufferedImage imgBgi; // background - image
private BufferedImage imgWht; // white stone - image
private BufferedImage imgBlk; // black stone - image
private BufferedImage imgWin; // winner image
private BufferedImage imgLos; // looser image
private Point pntMov; // position of the currently moving stone if any
private Cursor crsHnd; // hand cursor
private Player plyCmp; // the computer player
private Move mov; // the move to execute
private AnimationController aniCtr; // an animation controller
private Protocol proto; // a protocol
private boolean bShwPrt; // flag if the protocol shall be rendered
private boolean bShwRul; // flag if the rules shall be rendered
// -------------------------------------------------------------------------------------------
// ----------------------------------- default constructor -----------------------------------
public Board(Game gam,String strBgi){/*
// copy the game and register this instance as listener
this.gam = gam;
this.gam.addGameListener(this);
// create an array containing the 2D - coordinates of all fields
this.arrCoo = new int[]{
166,44,377,44,590,44,590,256,590,468,377,468,166,468,166,256,
236,114,377,114,520,114,520,256,520,398,377,398,236,398,236,256,
306,184,377,184,450,184,450,256,450,328,377,328,306,328,306,256
};
// open a try statements as there may occure an exception
try{
// load all needed images
this.imgBgi = ImageIO.read(this.getClass().getResource(strBgi));
this.imgWht = ImageIO.read(this.getClass().getResource("/rsc/wht.png"));
this.imgBlk = ImageIO.read(this.getClass().getResource("/rsc/blk.png"));
this.imgWin = ImageIO.read(this.getClass().getResource("/rsc/win.png"));
this.imgLos = ImageIO.read(this.getClass().getResource("/rsc/los.png"));
// setup the moving point and load the hand cursor
this.pntMov = null;
BufferedImage imgHnd = ImageIO.read(this.getClass().getResource("/rsc/hnd.png"));
this.crsHnd = this.getToolkit().createCustomCursor(imgHnd,new Point(5,7),"Hand");
}
// an exception is impossible as the required resources are inside the package
catch(IOException exp){}
// init the computer player by null and the source and destination by nil
this.plyCmp = null;
this.mov = new Move(Game.SRC_NIL,Game.SRC_NIL);
// create, setup and activate the animation controller
this.aniCtr = new AnimationController(this,10);
this.aniCtr.addAnimation(new Trail(81,367,Board.GRP_SRC,Trail.CLR_SRC));
this.aniCtr.startAnimations();
// create a new protocol instance and register it to the game
this.gam.addGameListener(this.proto = new Protocol());
this.bShwPrt = this.bShwRul = false;
*/
// register this component as MouseListener and MouseMotionListener
this.addKeyListener(this);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
// -------------------------------------------------------------------------------------------
// ------------------------------ interface method "keyPressed" ------------------------------
/** This interface method will be called from awt whenever a key was typed
@param evt The KeyEvent related to this event
@author Samuel Lörtscher */
// -------------------------------------------------------------------------------------------
public void keyPressed(KeyEvent evt){
System.err.println("lkj");
/*
// check if the F1 - key was typed
if(evt.getKeyCode() == KeyEvent.VK_F1){
// switch the show rules flag and repaint this component
this.bShwRul = !this.bShwRul;
this.repaint();
}*/
}
// -------------------------------------------------------------------------------------------
Die Eventmethode "keyPressed" wird einfach niemals abgefeuert! :bahnhof: