I am creating a simulation of a virtual video player (something like a very basic version of RealPlayer or Window Media Player). All the codes given below are error free.
This is the main class: VideoPlayer.java
This is the video player GUI (this frame is not resizable and the X button is disabled – you must click the Exit button to quit.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class VideoPlayer extends JFrame
implements ActionListener {
JButton check = new JButton("Check Videos");
JButton playlist = new JButton("Create Video List");
JButton update = new JButton("Update Videos");
JButton quit = new JButton("Exit");
public static void main(String[] args) {
new VideoPlayer();
}
public VideoPlayer() {
setLayout(new BorderLayout());
setSize(450, 100);
setTitle("Video Player");
// close application only by clicking the quit button
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
JPanel top = new JPanel();
top.add(new JLabel("Select an option by clicking one of the buttons below"));
add("North", top);
JPanel bottom = new JPanel();
bottom.add(check); check.addActionListener(this);
bottom.add(playlist); playlist.addActionListener(this);
bottom.add(update); update.addActionListener(this);
bottom.add(quit); quit.addActionListener(this);
add("South", bottom);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == check) {
new CheckVideos();
}
else if (e.getSource () == playlist) {
new CreateVideoList();
}
else if (e.getSource() == update) {
new UpdateVideos();
}
else if (e.getSource() == quit) {
VideoData.close();
System.exit(0);
}
}
}
This is the sub class : CheckVideos.java
When you clicks the Check Videos button the GUI appears (with the Video Player GUI still displayed on screen). The GUI will shows an identifying video number for each video. The name of the video is followed by the name of the director. It is also not resizable but the X button disposes of it. If you enters a valid video number and clicks the Check Video button, details of the video are displayed. If the video number is invalid, an error message is displayed instead.
You can list all the videos again by clicking the List All Videos button.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckVideos extends JFrame
implements ActionListener {
JTextField videoNo = new JTextField(2);
TextArea information = new TextArea(6, 50);
JButton list = new JButton("List All Videos");
JButton check = new JButton("Check Video");
public CheckVideos() {
setLayout(new BorderLayout());
setBounds(100, 100, 400, 200);
setTitle("Check Library");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel top = new JPanel();
top.add(new JLabel("Enter Video Number:"));
top.add(videoNo);
top.add(check);
top.add(list);
list.addActionListener(this);
check.addActionListener(this);
add("North", top);
JPanel middle = new JPanel();
information.setText(VideoData.listAll());
middle.add(information);
add("Center", middle);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == list) {
information.setText(VideoData.listAll());
} else {
String key = videoNo.getText();
String name = VideoData.getName(key);
if (name == null) {
information.setText("No such video number");
} else {
information.setText(name + " - " + VideoData.getDirector(key));
information.append("\nRating: " + stars(VideoData.getRating(key)));
information.append("\nPlay count: " + VideoData.getPlayCount(key));
}
}
}
private String stars(int rating) {
String stars = "";
for (int i = 0; i < rating; ++i) {
stars += "*";
}
return stars;
}
}
This is the class that holds the list of videos as a static database : VideoData.java
import java.util.*;
public class VideoData {
private static class Item {
Item(String n, String a, int r) {
name = n;
director = a;
rating = r;
}
// instance variables
private String name;
private String director;
private int rating;
private int playCount;
public String toString() {
return name + " - " + director;
}
}
// with a Map you use put to insert a key, value pair
// and get(key) to retrieve the value associated with a key
private static Map<String, Item> library = new TreeMap<String, Item>();
static {
// if you want to have extra library items, put them in here
// use the same style - keys should be 2 digit Strings
library.put("01", new Item("Tom and Jerry", "Fred Quimby", 3));
library.put("02", new Item("Tweety Pie ", "Wrexler Ripmophomtofz", 5));
library.put("03", new Item("Dr. Strangelove", "Stanley Kubrick", 2));
library.put("04", new Item("Babies 1st Birthday", "Me", 10));
library.put("05", new Item("Rat Pfink a Boo Boo", "Ray Steckler", 0));
}
public static String listAll() {
String output = "";
Iterator iterator = library.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Item item = library.get(key);
output += key + " " + item.name + " - " + item.director + "\n";
}
return output;
}
public static String getName(String key) {
Item item = library.get(key);
if (item == null) {
return null; // null means no such item
} else {
return item.name;
}
}
public static String getDirector(String key) {
Item item = library.get(key);
if (item == null) {
return null; // null means no such item
} else {
return item.director;
}
}
public static int getRating(String key) {
Item item = library.get(key);
if (item == null) {
return -1; // negative quantity means no such item
} else {
return item.rating;
}
}
public static void setRating(String key, int rating) {
Item item = library.get(key);
if (item != null) {
item.rating = rating;
}
}
public static int getPlayCount(String key) {
Item item = library.get(key);
if (item == null) {
return -1; // negative quantity means no such item
} else {
return item.playCount;
}
}
public static void incrementPlayCount(String key) {
Item item = library.get(key);
if (item != null) {
item.playCount += 1;
}
}
public static void close() {
// Does nothing for this static version.
}
}
Dear friends, my problem is that i wanted to create an addition class which which have the following features :
Enter a video number and click a button to add that video to a playlist.If the video number is valid, the video name should be added to the list and all the names in the list should be displayed in a text area,
otherwise a suitable error message should be displayed.Click a button to play the playlist – since this is just a simulation no
video’s will be played, but each video on the playlist should have its
play count value incremented by 1.Click a button to reset the playlist and clear the text area.
Besides that, I wanted to create another class which allow me to enter a video number and a new rating. If the video number is valid a message showing the video name, the new rating and the play count should be displayed, otherwise a suitable error message should be displayed.
I tried to create the classes but failed as i am just a beginner in java. If you don't mind, please guide me to create this two classes. (I am not asking you answer for any solution, please don't mistaken me. I am asking for a guidance.).
Sorry for the lengthy description but it's better to provide full information for better understanding.
Thank you in advance.