我正在尝试制作一个将根据键输入演奏的鼓组,但是键输入未到达阵列列表。
我尝试将数组列表放在不同的程序中,我尝试过字符串和字符。
package instruments;
鼓组
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.KeyEventDispatcher;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.*;
public class Drumset extends Canvas implements Runnable{
static KeyEvent keyevent;
static MyKeyListener keys = new MyKeyListener();
static ArrayList<String> instrument = new ArrayList<String>();//collects key inputs
public static final int width = 800, height = width;
public Thread thread;
public boolean running = false;
public Drumset(){
this.addKeyListener(keys);
new Window(width,height,"Drumset", this);
}
public synchronized void start(){
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop(){
try{
thread.join();
running = false;
}catch(Exception e){
e.printStackTrace();
}
}
public void findInstrument(){
}
public void run() {
long lastTime = System.nanoTime();
double amountofticks = 60.0;
double ns = 1000000000 / amountofticks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta>= 1){
tick();
delta--;
}
findInstrument();
if(running){
render();
frames ++;
}
if (System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
public void tick(){
}
public void render(){
BufferStrategy buffer = this.getBufferStrategy();
if (buffer == null){
this.createBufferStrategy(3);
return;
}
Graphics pen = buffer.getDrawGraphics();
pen.setColor(Color.black);
pen.fillRect(0,0,width,height);
buffer.show();
}
public static void main(String args[]){
new Drumset();
}
}
MyKeyListener
package instruments;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
public class MyKeyListener implements KeyListener {
private ArrayList<Character> keyInstruments = new ArrayList<Character>();
public MyKeyListener() {
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
System.out.println(key);
// bass
if (key == (KeyEvent.VK_B)){
keyInstruments.add('b');
}
// hi-hat
else if (key == (KeyEvent.VK_H)){
keyInstruments.add('h');
}
// crash cymbal
else if (key == (KeyEvent.VK_C)){
keyInstruments.add('c');
}
// floor tom
else if (key == (KeyEvent.VK_F)){
keyInstruments.add('f');
}
// high tom
else if (key == (KeyEvent.VK_T)){
keyInstruments.add('t');
}
// low tom
else if (key == (KeyEvent.VK_L)){
keyInstruments.add('l');
}
// ride cymbal
else if (key == (KeyEvent.VK_R)){
keyInstruments.add('r');
}
}
public void keyReleased(KeyEvent e) {
for(int c = 0; c < keyInstruments.size(); c++) {
if(keyInstruments.get(c) == e.getKeyChar()) {
keyInstruments.remove(c);
return;
}
}
}
public int getSize(){
return keyInstruments.size();
}
public void keyTyped(KeyEvent e) {
}
public ArrayList<Character> getKey() {
return keyInstruments;
}
}
我希望输出是被按下的任何按键,这些按键也包含在KeyPressed方法中。
答案 0 :(得分:0)
因此,要解决此问题,需要在窗口类而不是Drumset类中添加键侦听器。
JFrame frame = new JFrame(title);
frame.addKeyListener(listen);
答案 1 :(得分:-1)
在您的drumset类中,您将初始化一个称为键的静态MyKeyListener(),但您似乎在以后的代码中不再使用它。
我建议更换
public Drumset(){
this.addKeyListener(new MyKeyListener());
new Window(width,height,"Drumset", this);
}
使用
public Drumset(){
this.addKeyListener(keys);
new Window(width,height,"Drumset", this);
}
这可能不是正确答案,但我会尽力提供帮助。