1.11M Members

simulation of a virtual video player

 
0
 

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.

 
1
 

My problem is that I do not know how much you know about Java. When you said you are a beginner, it is even worse to me because programing GUI requires fundamental Java programming knowledge. If the basic is not firm enough, I am not sure you would understand the GUI part.

To me, you could either modify the existing code or create new classes and extends the existing classes. Not sure which way you prefer. Anyway, please tell us how much knowledge in Java you have right now. It would help us to determine what you should do next.

 
0
 

Dear sir, I understand about GUI..Not really a newbie thought, I am doing degree in computing 1st year..I would prefer to modify the existing code (CheckVideos.java) to complete the remaining two JFrame classes.

 
0
 

After I took a look at your codes, it seems to be OK. However, there are certain things you should consider.

  1. Ensure your display before you set resizable to false all of your classes. I tried your VideoPlayer class and the display of Exit button does not show up because it is located way to far to the right. As a result, I cannot exit the program but had to force quite.
  2. You should have variables to hold on those other classes, such as CheckVideos instance, inside your video player. Those variables will be able to pass any values from their own class operation back to the video player.
  3. You want your CheckVideos to be a pop up window? If so, you should not set it to be visible but rather hide it until the button Check Video is clicked. Also, it is a waste to dispose the object every time you open it up. I would hide it on close instead.
  4. The VideoData class is a static class right now. I believe you want it to read from a local storage? If so, you need to remove the hard-coded static portion because it is useless. Then add a class that can read your storage file system that let you select a file (maybe JFileChooser?).
 
0
 

I may be mistaken, but two classes appear to be missing from the code
CreateVideoList();
UpdateVideos();

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article