java - PaintComponent 的事件监听器

标签 java javax.sound.midi

我正在开发音乐应用程序,它会在每个事件中创建一个正方形并重新绘制屏幕。事件就是声音,我使用 sound.midi API 创建它们。但是,当我运行该应用程序时,方 block 不会出现在窗口上!有人可以告诉我我做错了什么吗?

这是我的主要类(class):

import javax.sound.midi.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class BeatBoxProject {

static JFrame f = new JFrame ("My first mucic video");
static DrawPanel m1;

public static void main(String[] args) {
BeatBoxProject mini = new BeatBoxProject();
mini.go();
}

public void setUpGui(){
m1 = new DrawPanel();
f.setContentPane(m1);
f.setBounds(30, 30, 300, 300);
f.setVisible(true);
}

public void go(){
setUpGui();

try{  Sequencer player = MidiSystem.getSequencer();  
player.open(); // we need the open the sequencer 

player.addControllerEventListener(m1, new int [] {127});

Sequence seq = new Sequence( Sequence.PPQ, 4); 
Track track = seq.createTrack();  

int r = 0;                        
for (int i = 0; i < 60; i+=4) {

r = (int) ((Math.random()*50)+ 1);
track.add(makeEvent(144,1,r,100,i));
track.add(makeEvent(176,1,127,0,i));
track.add(makeEvent(128,1,r,100,i+2));

}
player.setSequence(seq); // lets put the disk into the player
player.setTempoInBPM(120); // lets set the beat(beat per minutes)
player.start();


}catch (Exception ex){

        ex.printStackTrace();
}
}



public static MidiEvent makeEvent (int comd, int chan, int one, int two, int tick){


MidiEvent event  = null;

try{
ShortMessage a = new ShortMessage();
a.setMessage(comd, chan, two, two);
event = new MidiEvent(a,tick);
}catch(Exception e) {}
return event;
}
}

这是 DrawPanel 类:

import javax.sound.midi.ControllerEventListener;
import java.awt.*;
import javax.sound.midi.ShortMessage;
import javax.swing.*;

public class DrawPanel extends JPanel implements ControllerEventListener { 

boolean msg = false; // its false unles we gonna have an event 

public void controlChange(ShortMessage event) {
    msg = true;
    repaint();
}   


public void paintComponent (Graphics g){
if (msg){  // wee need the msg because other thing can repaint the panel but we only want when event occurs 

Graphics2D g2 = (Graphics2D) g;    

int r = (int) (Math.random()* 250); 
int gr = (int) (Math.random()* 250); 
int b = (int) (Math.random()* 250); 


g.setColor(new Color(r,gr,b));

int ht = (int) ((Math.random()*120) +10);
int width = (int) ((Math.random()*120) +10);

int x = (int) ((Math.random()*40) +10);
int y = (int) ((Math.random()*40) +10);

g.fillRect(x, y, width, HEIGHT);
msg=false;

}
}
}

最佳答案

改变

player.addControllerEventListener(m1, new int [] {127});

player.addControllerEventListener(m1, new int [] {0});

关于java - PaintComponent 的事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41458778/

相关文章:

java - GetObjectField() 是否返回本地引用?

java - 在 Java 中解析许多复杂命令行参数的最佳方法是什么?

java - MidiSystem.getMidiDevice(...) 返回意外的类

Java :Painting Random Shapes on JFrame against Music Played

java - 如何在Java MIDI程序中更改乐器

java - 'withIngestionTimestamps()' 在 Hazelcast Jet Pipeline 中到底有什么用?

java - 试图将 Tomcat servlet 的 http 请求发送到另一台服务器,卡在 HttpClientBuilder.create().build();

java - Maven Javadoc 插件 - 覆盖默认 CSS 的特定部分

java - try-catch 无法正常工作

java - 在Java中同时播放创建的Midi声音