hey people,
I've been experimenting with some new (for me) java techniques, like keybindings. i've got a small app, where the bindings work fine, untill i press a JButton with actionListener.
the actionListener doesn't do anything at all, but keybindings fail after i press one of them.
here's my code (the keybindings are in a seperate file, but i know they work anyway)
package hokjesmain;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HokjesMain extends JFrame{
private static Color background = Constants.applicationBackground;
private static int width = Constants.applicationWidth, height = Constants.applicationHeight;
public static void main(String[] args) {
JFrame frame = new HokjesMain();
frame.setBackground(background);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setContentPane(new Pane());
frame.setVisible(true);
}
}
class Pane extends JPanel{
private Player player, player2;
private Grid grid;
private JButton newGame,help, exit;
public Pane(){
try{
this.setLayout(null);
this.player2 = new Player(Constants.defaultPlayerOneStartCell, Constants.defaultPlayerOneStartRow, Constants.defaultPlayerStartingCredits, Constants.defaultPlayer1Color, Constants.defaultPlayerOneImageLocation);
this.player = new Player(Constants.defaultPlayerTwoStartCell, Constants.defaultPlayerTwoStartRow, Constants.defaultPlayerStartingCredits, Constants.defaultPlayer2Color, Constants.defaultPlayerTwoImageLocation);
this.grid = new Grid(Constants.defaultGridColls, Constants.defaultGridRows);
this.newGame = new JButton("restart");
this.help = new JButton("help");
this.exit = new JButton("exit");
MovementHandler movement = new MovementHandler(player, player2, grid, this);
PlayerActionHandler pa = new PlayerActionHandler(player, player2, grid, this);
this.newGame.setBounds(Constants.buttonLeftX, Constants.buttonTopY, Constants.buttonWidth, Constants.buttonHeight);
this.help.setBounds(Constants.buttonRightX, Constants.buttonTopY, Constants.buttonWidth, Constants.buttonHeight);
this.exit.setBounds(Constants.buttonLeftX, Constants.buttonRow2Y, Constants.buttonWidth, Constants.buttonHeight);
this.newGame.addActionListener(new startNewGame());
this.help.addActionListener(new help());
this.exit.addActionListener(new exit());
add(this.newGame);
add(this.help);
add(this.exit);
validate();
}
catch(Error e){
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(Constants.IOError + e.getCause());
}
}
class startNewGame implements ActionListener{
@Override public void actionPerformed(ActionEvent e){
}
}
class help implements ActionListener{
@Override public void actionPerformed(ActionEvent e){
System.out.println("not yet implemented");
repaint();
}
}
class exit implements ActionListener{
@Override public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
@Override public void paintComponent(Graphics g){
try{
grid.drawGrid(g);
g.setFont(new Font(Constants.fontForPlayerText, Constants.fontForPlayerTextStyle, Constants.fontForPlayerTextSize));
g.setColor(Constants.defaultTextDrawingColor);
g.drawImage(player.getPlayer(), player.getPositionCellForDrawing(), player.getPositionRowForDrawing(), null);
g.drawImage(player2.getPlayer(), player2.getPositionCellForDrawing(), player2.getPositionRowForDrawing(), null);
g.drawString(Constants.playerDataText1, Constants.drawDistanceXForPlayer1Data, (grid.getAmountOfRows() * Constants.defaultCellHeightPx) + Constants.distanceBetweenGridAndData);
g.setColor(player.getPlayerColor());
g.fillRect(Constants.drawDistanceXForPlayer1Data + Constants.distanceBetweenTextAndData, ((grid.getAmountOfRows() * Constants.defaultCellHeightPx) + Constants.distanceBetweenGridAndData - Constants.defaultExampleCellHeight), Constants.defaultExampleCellWidth, Constants.defaultExampleCellHeight);
g.setColor(player2.getPlayerColor());
g.fillRect(Constants.drawDistanceXForPlayer2Data + Constants.distanceBetweenTextAndData, ((grid.getAmountOfRows() * Constants.defaultCellHeightPx) + Constants.distanceBetweenGridAndData - Constants.defaultExampleCellHeight), Constants.defaultExampleCellWidth, Constants.defaultExampleCellHeight);
g.setColor(Constants.defaultTextDrawingColor);
g.drawString(Constants.playerDataText2, Constants.drawDistanceXForPlayer1Data, (grid.getAmountOfRows() * Constants.defaultCellHeightPx) + Constants.distanceBetweenGridAndData + Constants.fontForPlayerTextSize + Constants.DistanceBetweenDataRules);
g.setColor(Constants.applicationBackground);
g.fillRect(Constants.drawDistanceXForPlayer1Data + Constants.distanceBetweenTextAndData, ((grid.getAmountOfRows() * Constants.defaultCellHeightPx) + Constants.distanceBetweenGridAndData - Constants.defaultExampleCellHeight) + Constants.defaultExampleCellHeight + Constants.DistanceBetweenDataRules, (Constants.fontForPlayerTextSize * 2), Constants.defaultExampleCellHeight);
g.fillRect(Constants.drawDistanceXForPlayer2Data + Constants.distanceBetweenTextAndData, ((grid.getAmountOfRows() * Constants.defaultCellHeightPx) + Constants.distanceBetweenGridAndData - Constants.defaultExampleCellHeight) + Constants.defaultExampleCellHeight + Constants.DistanceBetweenDataRules, (Constants.fontForPlayerTextSize * 2), Constants.defaultExampleCellHeight);
g.setColor(Constants.defaultTextDrawingColor);
g.drawString(""+player.getCredits(), Constants.drawDistanceXForPlayer1Data + Constants.distanceBetweenTextAndData, ((grid.getAmountOfRows() * Constants.defaultCellHeightPx) + Constants.distanceBetweenGridAndData) + Constants.defaultExampleCellHeight + Constants.DistanceBetweenDataRules);
g.drawString(""+player2.getCredits(), Constants.drawDistanceXForPlayer2Data + Constants.distanceBetweenTextAndData, ((grid.getAmountOfRows() * Constants.defaultCellHeightPx) + Constants.distanceBetweenGridAndData) + Constants.defaultExampleCellHeight + Constants.DistanceBetweenDataRules);
}
catch(Error e){
System.out.println(e.getMessage());
}
}
}