اذهب إلى المحتوى
  • 0

Exception in thread "AWT-EventQueue-0" java

Asma'a

السؤال

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.MalformedURLException;
import java.util.logging.*;
import javax.sound.sampled.*;
import javax.swing.*;



public class BounceyDot {
  public static float SAMPLE_RATE = 8000f;
 public static void tone(int hz, int msecs) 
            throws LineUnavailableException 
                    {
                       tone(hz, msecs, 1.0);
                    }
 
  public static void tone(int hz, int msecs, double vol)
      throws LineUnavailableException 
  {
                byte[] buf = new byte[1];
                AudioFormat af = 
                    new AudioFormat(
                        SAMPLE_RATE, // sampleRate
                        8,           // sampleSizeInBits
                        1,           // channels
                        true,        // signed
                        false);      // bigEndian
                SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
                sdl.open(af);
                sdl.start();
                for (int i=0; i < msecs*8; i++) {
                  double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
                  buf[0] = (byte)(Math.sin(angle) * 127.0 * vol);
                  sdl.write(buf,0,1);
                }
                sdl.drain();
                sdl.stop();
                sdl.close();
  }
    public static void main(String[] args)
           {
              new BounceyDot();
            }

    public BounceyDot() {
        EventQueue.invokeLater(new Runnable() {
         @Override
          public void run() {
            try {
                  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } 
            catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                 SoundUtils.tone(1000,100);
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException ex) {
                 Logger.getLogger(BounceyDot.class.getName()).log(Level.SEVERE, null, ex);
             }
    SoundUtils.tone(100,1000);
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException ex) {
                 Logger.getLogger(BounceyDot.class.getName()).log(Level.SEVERE, null, ex);
             }
    SoundUtils.tone(5000,100);
             try {
                 Thread.sleep(1000);
             } catch (Exception ex) {
                 System.out.println(ex.getMessage());
             }
    SoundUtils.tone(400,500);
             try {
                 Thread.sleep(1000);
             } catch (Exception ex) {
                 System.out.println(ex.getMessage());
             }
    SoundUtils.tone(400,500, 0.2);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int x = 0;
        private int y = 100;
        private int radius = 20;
        private int xDelta = 2;

        public TestPane() {
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    x += xDelta;
                          if (x + (radius * 2) > getWidth()) {
                        x = getWidth() - (radius * 2);
                        xDelta *= -1;
                    } else if (x < 0) {
                        x = 0;
                        xDelta *= -1;
                    }
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillOval(x, y - radius, radius * 2, radius * 2);
        }
    }

}

السلام عليكم هنا عملت كود الصوت مع الأشكال ..لمن كل كود منفصل لحالة يعمل بشكل صحيح لكن لمن أدمج الكودين ينتج خطأ لم أستطيع تصحيحة..فممكن مساعدة؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

لا توجد أي إجابات على هذا السؤال بعد

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...