java only Write a Java application or applet program that (1) plays a sound file
ID: 3719247 • Letter: J
Question
java only
Write a Java application or applet program that (1) plays a sound file; (2) displays an image moving from the top to the bottom of the panel, and if the image reaches the bottom of the panel it will be randomly and vertically bounced back. This process will run forever unless you terminate the program; and (3) displays a randomly colored circle moving from the top to the bottom of the panel, and if the circle reaches the bottom of the panel it will be randomly and vertically bounced back. This process will run forever unless you terminate the program. A sample run of the program is shown in Appendix 1.
Explanation / Answer
here is your program : ------------>>>>>>>>>
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.*;
import java.util.Random;
import javax.sound.sampled.*;
import javax.sound.*;
import javax.imageio.ImageIO;
import java.io.*;
public class AppExample extends JFrame{
private ImagePanel p1;
private CirclePanel p2;
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
public void run() {
try {
Clip clip = AudioSystem.getClip();
//here you have to put the path of audio file
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("C:\Users\dhananjay\Desktop\" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
public AppExample(){
super("Animation Example");
setSize(700,800);
setLayout(new GridLayout(1,2));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new ImagePanel();
p2 = new CirclePanel();
add(p1);
add(p2);
setVisible(true);
}
private void paintComponent(Graphics g){
p1.paintComponent(g);
p2.paintComponent(g);
}
public static void main(String[] args) throws Exception{
//put here the wav file name
AppExample.playSound("click.wav");
AppExample app = new AppExample();
while(true){
app.repaint();
Thread.sleep(10);
}
}
}
class ImagePanel extends JPanel{
private BufferedImage image;
private int x,y;
public ImagePanel() {
setSize(300,800);
try {
//here you have to put the full path + name of the image file
image = ImageIO.read(new File("C:\Users\dhananjay\Desktop\dkedu\New folder\bootstrap\boot project\c project\punjit\espresso1.png"));
x = 20;
y = 0;
} catch (IOException ex) {
// handle exception...
}
}
;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
y = y + 4;
if(y+image.getHeight() >= 800){
Random rand = new Random();
y = rand.nextInt(700-image.getHeight()) + 1;
}
g.drawImage(image, x, y, this); // see javadoc for more info on the parameters
}
}
class CirclePanel extends JPanel{
private int x,y;
public CirclePanel(){
setSize(300,800);
x = 30;
y = 0;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
y = y + 4;
if(y+60 >= 800){
Random rand = new Random();
y = rand.nextInt(700-60) + 1;
}
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.