나는이 타이머를 꽤 끊임없이 업데이트하도록 설정했지만 스페이스 바를 사용하여 타이머를 일시 중지 할 수 있기를 원합니다. actionListener를 구현하려고 시도했지만 적용 할 항목이 확실하지 않습니다. 내가 찾을 수있는 대부분의 예제는 jpanel 내부의 키보드 프레스가 아니라 버튼이나 텍스트 상자와 관련이 있습니다. 콘솔에 src를 인쇄했는데 이벤트로 내 스페이스 바를 등록하지 않는 것 같습니다 ... 나는 actionListener를 추가하려고 시도했지만 구문에 대해 뭔가를 얻지는 않습니다. 어떤 도움을 주시면 감사하겠습니다.스페이스 바를위한 actionListener를 액션 이벤트로 구현하는 방법
public class Arena extends JFrame {
private PaintPanel paintPanel;
public Arena() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(1000, 720));
paintPanel = new PaintPanel();
getContentPane().add(paintPanel, BorderLayout.CENTER);
paintPanel.setBackground(Color.black);
paintPanel.setFocusable(true);
//paintPanel.addActionListener(this);
pack();
paintPanel.initGame();
}
class PaintPanel extends JPanel implements ActionListener {
private List<Gladiator> gladiators;
private Timer timer;
private Ai AI;
private Load loadObject;
public void initGame() {
timer = new Timer(500, this);
timer.start();
AI = new Ai(gladiators);
loadObject = new Load();
}
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
System.out.println("************* "+src);
// if (src == spacebar) {
// } else if (src = timer) {
AI.runAI();
try {
Thread.sleep(100);
System.out.println("sleeping");
} catch (InterruptedException d) {
System.err.println("Caught : InterruptedException" + d.getMessage());
}
repaint();
// }
}
public PaintPanel(){
super();
gladiators = new ArrayList<Gladiator>();
String[][] gladiatorInfo = new String[100][25];
String[] gladiatorRaw = new String[100];
String[][] parsedInfo = new String[250][100];
Gladiator[] gladiator = new Gladiator[20];
int[] matchInfo = new int[3];
int numberofcontestants = 0;
gladiatorRaw = loadObject.getGladiators(gladiatorRaw);
parsedInfo = loadObject.parseStats(gladiatorRaw);
Venue venue = new Venue();
matchInfo = loadObject.getMatchSettings(matchInfo);
venue.populateVenue(matchInfo);
gladiator = createGladiators(venue);
for (int a = 0; a < venue.contestants; a++) {
System.out.println("Populating Gladiator "+a);
gladiator[a].populategladiators(parsedInfo,a);
gladiator[a].getEquipment();
gladiator[a].contestantNumber = a;
gladiators.add(gladiator[a]);
}
}
public Gladiator[] createGladiators(Venue venue) {
int[][] initialPlacement = new int[20][2];
Gladiator[] gladiator = new Gladiator[(venue.contestants)];
initialPlacement = loadObject.loadInitialPlacement(venue.contestants);
for (int a = 0; a < venue.contestants; a++) {
System.out.println("Add gladiator "+a);
gladiator[a] = new Gladiator(initialPlacement[a][0],initialPlacement[a][1]);
System.out.println(initialPlacement[a][0]+","+initialPlacement[a][1]);
}
return gladiator;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
try {
BufferedImage background = ImageIO.read(new File("background.png"));
g.drawImage(background,0,0,this);
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
for (Gladiator s : gladiators){
s.draw(g2);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Arena gamePanel = new Arena();
gamePanel.setVisible(true);
}
});
}
}
또한 스페이스 바에 getEvent() 키 코드가 있습니까? 하나를 찾을 수없는 것 같습니다. 감사합니다
는
에 부착 좋을 것를 사용해야합니다 당신은 어딘가에 addactionlistener (this)를함으로써 paintpanel이 actionevents를 듣게 할 필요가있다. – arynaq