我一直在使用java中的ArrayList创建矩形等形状。我试图让矩形一次出现在面板中,然后慢慢构建一个实际图像。我已经没有问题让他们出现并保持随机的位置(位置,大小,颜色)......但是当我试图使这些rects静态...它们都崩溃了。欢迎提出建议。
Full code so far below:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.List;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.sound.midi.ControllerEventListener;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraphicMusicPlayer
implements ControllerEventListener {
static JFrame f = new JFrame("Graphic Music");
static MyDrawPanel myPanel;
@Override
public void controlChange(ShortMessage event) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
GraphicMusicPlayer mini = new GraphicMusicPlayer();
mini.go();
// TODO Auto-generated method stub
}
public void setUpGui() {
// Create the panel within the frame
myPanel = new MyDrawPanel();
f.setContentPane(myPanel);
f.setBounds(700, 700, 800, 800);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void go() {
setUpGui();
try {
// Make and open a sequencer.
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
// Register for events. 127 is the event.
sequencer.addControllerEventListener(myPanel, new int[] {93});
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
int r = 0;
int[] tonesList = new int[] {
65, 64, 65, 64, 65, 60, 63, 61,
58,65, 64, 65, 64, 65, 60, 63, 61,
58};
for (int i = 0; i < 18; i +=1) {
r = tonesList[i];
if (r>0){
track.add(makeEvent(144,5,r,93,i));//144 = NOTE_ON
track.add(makeEvent(176,5,93,125,i));//176 = get event
track.add(makeEvent(128,5,r,46,i+1));//128 = NOTE_OFF
}//end loop
sequencer.setSequence(seq);
sequencer.setTempoInBPM(30);
sequencer.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, one, two);
event = new MidiEvent(a, tick);
} catch (Exception e) { }
return event;
}
@SuppressWarnings("serial")
class MyDrawPanel extends JPanel implements ControllerEventListener {
boolean msg = false;
ArrayList<Rectangle> rectangleList = new ArrayList<Rectangle>();
Map<Rectangle, Color> rectangleColors = new HashMap<Rectangle, Color>();
public void addRectangle(Rectangle rect){
rectangleList.add(rect);
}
public void controlChange(ShortMessage event) {
msg = true;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.BLACK);
while (msg) {
Graphics2D g2 = (Graphics2D) g;
int red = (int) (Math.random() * 250);
int green = (int) (Math.random() * 250);
int blue = (int) (Math.random() * 250);
Color rcolor = new Color(red, green, blue);
int x = (int) (Math.random() * 750);
int y = (int) (Math.random() * 750);
int width = (int) (Math.random() * 100);
int height = (int) (Math.random() * 100);
Rectangle nextRect = new Rectangle(x,y,width,height);
rectangleList.add(nextRect);
g.setColor(rcolor);
rectangleColors.put(nextRect, rcolor);
//paint all of the rectangles
for (Rectangle rect : rectangleList){
Color rectColor = rectangleColors.get(rect);
g.setColor(rectColor);
// Fill the rectangle with the rectangle color
g2.fill(rect);
}
msg = false;
}
}
}
}