Легкая программа показывает в менюшке непонятный артефакт - Java
Формулировка задачи:
Здравствуйте!
Ниже работающий код. Однако при исполнении программы почему то отображается "будто бы вторая строка меню".
Фото приложено
import javax.sound.midi.*; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import static javax.sound.midi.Sequence.PPQ; public class JHF_TESTS_MMP3_412 { static JFrame jFrame; static int eventsCounter=0; static MyDrawingPanel myDrawingPanel; static ContinuePlaying continuePlaying; static StopPlaying stopPlaying; Sequencer sequencer; public static void main(String[] args) throws Exception { JHF_TESTS_MMP3_412 jhf_tests_mmp3_412 = new JHF_TESTS_MMP3_412(); jhf_tests_mmp3_412.GStart(); } public void setGUI() { myDrawingPanel = new MyDrawingPanel(); continuePlaying = new ContinuePlaying(); stopPlaying = new StopPlaying(); jFrame = new JFrame("MMP3"); JMenuBar MP3menuBar = new JMenuBar(); JMenu MP3menu = new JMenu("Player"); JMenuItem Play = new JMenuItem("Play"); JMenuItem Stop = new JMenuItem("Pause"); Play.addActionListener(continuePlaying); Stop.addActionListener(stopPlaying); MP3menu.add(Play); MP3menu.add(Stop); MP3menuBar.add(MP3menu); jFrame.setJMenuBar(MP3menuBar); jFrame.getContentPane().add(BorderLayout.CENTER,myDrawingPanel); jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); jFrame.setSize(1920, 1080); jFrame.setVisible(true); } public void GStart() throws Exception{ setGUI(); sequencer = MidiSystem.getSequencer(); sequencer.open(); int[] arraytt = {127}; sequencer.addControllerEventListener(myDrawingPanel,arraytt); Sequence sequence = new Sequence(PPQ,4); Track track = sequence.createTrack(); //Track track = new Sequence(PPQ,4).createTrack(); for (long i = 1; i < 6500; i+=4) { track.add(newSingleMidiEvent(192,1,18,0,1)); track.add(newSingleMidiEvent(144, 1, NotesrandomNumber(), 100, i)); track.add(newSingleMidiEvent(176, 1, 127, 0, i)); track.add(newSingleMidiEvent(128, 1, NotesrandomNumber(), 100, i + 2)); } sequencer.setSequence(sequence); sequencer.setTempoInBPM(333); sequencer.start(); Thread.sleep(100); System.out.println("eventsCounter: " + eventsCounter + "; arraytt: " + arraytt[0]); } public MidiEvent newSingleMidiEvent(int mv1, int mv2, int mv3, int mv4, long mv5) throws Exception { MidiEvent midiEvent = new MidiEvent(new ShortMessage(mv1, mv2, mv3, mv4), mv5); return midiEvent; } public int NotesrandomNumber() { int intrandom = (int) (Math.random()*65); return intrandom; } class MyDrawingPanel extends JPanel implements ControllerEventListener { boolean EventTestMark; public void controlChange(ShortMessage event){ EventTestMark = true; myDrawingPanel.repaint(); } public int dotsRandomNumberX() { int intrandomXandX = (int) (Math.random()*1820); return intrandomXandX; } public int dotsRandomNumberY() { int intrandomXandY = (int) (Math.random()*980); return intrandomXandY; } public Color colorsRandom() { int red = (int) (Math.random()*255); int green = (int) (Math.random()*255); int blue = (int) (Math.random()*255); Color randomColor = new Color(red,green,blue); return randomColor; } public void paint(Graphics g){ if(EventTestMark==true) { eventsCounter++; g.setColor(colorsRandom()); g.fillRect(dotsRandomNumberX(), dotsRandomNumberY(), 23+dotsRandomNumberX()/14, 23+ dotsRandomNumberY()/14); EventTestMark = false; } } } public class StopPlaying implements ActionListener{ public void actionPerformed(ActionEvent actionEvent){ sequencer.stop(); } } public class ContinuePlaying implements ActionListener{ public void actionPerformed(ActionEvent actionEvent){ sequencer.start(); sequencer.setTempoInBPM(333); } } }
Решение задачи: «Легкая программа показывает в менюшке непонятный артефакт»
textual
Листинг программы
import javax.sound.midi.*; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import java.util.List; import static javax.sound.midi.Sequence.PPQ; public class untitled { static JFrame jFrame; static int eventsCounter = 0; static MyDrawingPanel myDrawingPanel; static ContinuePlaying continuePlaying; static StopPlaying stopPlaying; Sequencer sequencer; public static void main(String[] args) throws Exception { untitled untitled = new untitled(); untitled.GStart(); } public void setGUI() { myDrawingPanel = new MyDrawingPanel(); continuePlaying = new ContinuePlaying(); stopPlaying = new StopPlaying(); jFrame = new JFrame("MMP3"); JMenuBar MP3menuBar = new JMenuBar(); JMenu MP3menu = new JMenu("Player"); JMenuItem Play = new JMenuItem("Play"); JMenuItem Stop = new JMenuItem("Pause"); Play.addActionListener(continuePlaying); Stop.addActionListener(stopPlaying); MP3menu.add(Play); MP3menu.add(Stop); MP3menuBar.add(MP3menu); jFrame.setJMenuBar(MP3menuBar); jFrame.getContentPane().add(BorderLayout.CENTER, myDrawingPanel); jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); jFrame.setSize(1920, 1080); jFrame.setVisible(true); } public void GStart() throws Exception { setGUI(); sequencer = MidiSystem.getSequencer(); sequencer.open(); int[] arraytt = {127}; sequencer.addControllerEventListener(myDrawingPanel, arraytt); Sequence sequence = new Sequence(PPQ, 4); Track track = sequence.createTrack(); //Track track = new Sequence(PPQ,4).createTrack(); for (long i = 1; i < 6500; i += 4) { track.add(newSingleMidiEvent(192, 1, 18, 0, 1)); track.add(newSingleMidiEvent(144, 1, NotesrandomNumber(), 100, i)); track.add(newSingleMidiEvent(176, 1, 127, 0, i)); track.add(newSingleMidiEvent(128, 1, NotesrandomNumber(), 100, i + 2)); } sequencer.setSequence(sequence); sequencer.setTempoInBPM(333); sequencer.start(); Thread.sleep(100); System.out.println("eventsCounter: " + eventsCounter + "; arraytt: " + arraytt[0]); } public MidiEvent newSingleMidiEvent(int mv1, int mv2, int mv3, int mv4, long mv5) throws Exception { MidiEvent midiEvent = new MidiEvent(new ShortMessage(mv1, mv2, mv3, mv4), mv5); return midiEvent; } public int NotesrandomNumber() { int intrandom = (int) (Math.random() * 65); return intrandom; } class MyRect { Rectangle rect; Color color; } class MyDrawingPanel extends JPanel implements ControllerEventListener { boolean EventTestMark; Random rnd = new Random(); List arrayList = new ArrayList(); int index; public void controlChange(ShortMessage event) { EventTestMark = true; myDrawingPanel.repaint(); } public int dotsRandomNumberX() { int intrandomXandX = (int) (Math.random() * 1820); return intrandomXandX; } public int dotsRandomNumberY() { int intrandomXandY = (int) (Math.random() * 980); return intrandomXandY; } public Color colorsRandom() { int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); Color randomColor = new Color(red, green, blue); return randomColor; } public void paint(Graphics g) { super.paint(g); if (EventTestMark == true) { System.out.println(eventsCounter++); List arrayListInternal = new ArrayList(); arrayListInternal.add(dotsRandomNumberX()); arrayListInternal.add(dotsRandomNumberY()); arrayListInternal.add(colorsRandom()); arrayListInternal.add((int)(Math.random() * 1820/14)+23); // width arrayListInternal.add((int)(Math.random() * 980/14)+23); // height arrayList.add(arrayListInternal); ListIterator iterator = arrayList.listIterator(); while (iterator.hasNext()) { ArrayList arrayListTemp = (ArrayList) iterator.next(); g.setColor((Color) arrayListTemp.get(2)); g.fillRect((int) arrayListTemp.get(0), (int) arrayListTemp.get(1), (int) arrayListTemp.get(3), (int) arrayListTemp.get(4)); System.out.println(arrayListTemp); } } EventTestMark = false; } } public class StopPlaying implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { sequencer.stop(); } } public class ContinuePlaying implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { sequencer.start(); sequencer.setTempoInBPM(333); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д