Created
January 22, 2016 03:03
-
-
Save connlark/f85a43474b49a9eb3b05 to your computer and use it in GitHub Desktop.
fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Import packages | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
//Main class | |
public class gui1{ | |
//Declare variables | |
static JFrame frame1; | |
static Container pane; | |
static JButton btnAdd, btnDelete; | |
static JLabel title, artist, genre, length; | |
static JTextField txtSong, txtArtist, txtGenre, txtLength; | |
static Insets insets; | |
public static void main (String args[]){ | |
//Set Look and Feel | |
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} | |
catch (ClassNotFoundException e) {} | |
catch (InstantiationException e) {} | |
catch (IllegalAccessException e) {} | |
catch (UnsupportedLookAndFeelException e) {} | |
//Create the frame | |
frame1 = new JFrame ("Sample GUI Application"); | |
frame1.setSize (950,200); | |
pane = frame1.getContentPane(); | |
insets = pane.getInsets(); | |
pane.setLayout (null); | |
//Create controls | |
btnAdd = new JButton ("Add song to library"); | |
btnDelete = new JButton ("Delete song from library"); | |
title = new JLabel ("Title:"); | |
artist = new JLabel ("Artist:"); | |
genre = new JLabel ("Genre:"); | |
length = new JLabel ("Length:"); | |
txtSong = new JTextField (10); | |
txtArtist = new JTextField (10); | |
txtGenre = new JTextField (10); | |
txtLength = new JTextField (10); | |
//Add all components to panel | |
pane.add (title); | |
pane.add (artist); | |
pane.add (genre); | |
pane.add (length); | |
pane.add (txtSong); | |
pane.add (txtArtist); | |
pane.add (txtGenre); | |
pane.add (txtLength); | |
pane.add (btnAdd); | |
pane.add (btnDelete); | |
//Place all components | |
title.setBounds (insets.left + 5, insets.top + 5, title.getPreferredSize().width, title.getPreferredSize().height); | |
txtSong.setBounds (title.getX() + title.getWidth() + 5, insets.top + 5, txtSong.getPreferredSize().width, txtSong.getPreferredSize().height); | |
artist.setBounds (txtSong.getX() + txtSong.getWidth() + 5, insets.top + 5, artist.getPreferredSize().width, artist.getPreferredSize().height); | |
txtArtist.setBounds (artist.getX() + artist.getWidth() + 5, insets.top + 5, txtArtist.getPreferredSize().width, txtArtist.getPreferredSize().height); | |
genre.setBounds (txtArtist.getX() + txtArtist.getWidth() + 5, insets.top + 5, genre.getPreferredSize().width, genre.getPreferredSize().height); | |
txtGenre.setBounds (genre.getX() + genre.getWidth() + 5, insets.top + 5, txtGenre.getPreferredSize().width, txtGenre.getPreferredSize().height); | |
length.setBounds (txtGenre.getX() + txtGenre.getWidth() + 5, insets.top + 5, length.getPreferredSize().width, length.getPreferredSize().height); | |
txtLength.setBounds (length.getX() + length.getWidth() + 5, insets.top + 5, txtLength.getPreferredSize().width, txtLength.getPreferredSize().height); | |
btnAdd.setBounds (txtLength.getX() + txtLength.getWidth() + 5, insets.top + 5, btnAdd.getPreferredSize().width, btnAdd.getPreferredSize().height); | |
btnDelete.setBounds (insets.left + 15, title.getY() + title.getHeight() + 5, btnDelete.getPreferredSize().width, btnDelete.getPreferredSize().height); | |
//Set frame visible | |
frame1.setVisible (true); | |
//Button's action | |
btnAdd.addActionListener(new btnConnectAction()); //Register action | |
} | |
public static class btnConnectAction extends Media implements ActionListener{ | |
public void actionPerformed (ActionEvent e){ | |
Song s = new Song(txtSong.getText(),txtArtist.getText(), Integer.getInteger(txtLength.getText()),txtGenre.getText() ); | |
System.out.println("Song entered: " + s.toString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Connor on 1/21/16. | |
*/ | |
public interface Information { | |
public int getLength(); | |
public String getName(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
/** | |
* Created by Connor on 1/19/16. | |
*/ | |
public class InputDialog { | |
public static String getInput(String prompt){ | |
return JOptionPane.showInputDialog(null, prompt); | |
} | |
public static void showDialog(String in){ | |
JOptionPane.showMessageDialog(null,in); // pop up dialog | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Random; | |
/** | |
* Created by Connor on 1/20/16. | |
*/ | |
public class Media implements Information{ | |
private ArrayList<Song> songs; | |
private ArrayList<Playlist> playlists; | |
private String owner; | |
private static Random random = new Random(); | |
//constructors | |
public Media(){ | |
songs = new ArrayList<Song>(); | |
playlists = new ArrayList<Playlist>(); | |
owner = genString(); | |
} | |
public Media(ArrayList<Song> songs, ArrayList<Playlist> playlists, String owner) { | |
this.songs = songs; | |
this.playlists = playlists; | |
this.owner = owner; | |
} | |
//adding to media | |
public void addSong(Song foo){ | |
songs.add(foo); | |
} | |
public void addPlaylist(Playlist list){ | |
playlists.add(list); | |
} | |
//random string generator for testing | |
public static String genString(){ | |
String out = ""; | |
int length = random.nextInt(9) + 3; | |
while (out.length() != length){ | |
out += Character.toString((char) (random.nextInt((122 - 97) + 1) + 97)); | |
} | |
return out; | |
} | |
//overriding interface methods | |
@Override | |
public int getLength() { | |
return songs.size() + playlists.size(); | |
} | |
@Override | |
public String getName() { | |
return owner; | |
} | |
public static void main(String[] args) { | |
for (int i = 0; i < 20; i++) { | |
System.out.println(genString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
/** | |
* Created by Connor on 1/20/16. | |
*/ | |
public class Playlist extends Media implements Information { | |
private ArrayList<Song> songs; | |
private int numberOfSongs; | |
private int duration; | |
private String name; | |
//constructors | |
public Playlist(){ | |
songs = new ArrayList<Song>(); | |
numberOfSongs = songs.size(); | |
name = super.genString(); | |
super.addPlaylist(this); | |
} | |
public Playlist(ArrayList<Song> songs) { | |
this.songs = songs; | |
this.numberOfSongs = songs.size(); | |
super.addPlaylist(this); | |
} | |
@Override | |
public String toString() { | |
if (songs.size() == 0) return "Empty Playlist"; | |
String out = ""; | |
for (Song s:songs) { | |
out += s.getName() + "\n"; | |
} | |
return out; | |
} | |
public void addToPlaylist(Song song){ | |
songs.add(song); | |
} | |
public ArrayList<Song> getSongs(){ | |
return songs; | |
} | |
public void sortAlpha(){ | |
ArrayList<Song> foo = new ArrayList<Song>(); | |
String[] titles = new String[songs.size()]; | |
for (int i = 0; i < songs.size(); i++) { | |
titles[i] = songs.get(i).getTitle(); | |
} | |
Arrays.sort(titles); | |
for (int i = 0; i < songs.size(); i++) { | |
for (int j = 0; j < songs.size(); j++) { | |
if (titles[i].equals(songs.get(j).getTitle())){ | |
foo.add(songs.get(j)); | |
} | |
} | |
} | |
songs = foo; | |
} | |
public static void main(String[] args) { | |
Playlist list = new Playlist(); | |
for (int i = 0; i < 1000; i++) { | |
Song foo = new Song(); | |
list.addToPlaylist(foo); | |
} | |
list.sortAlpha(); | |
System.out.println(list.toString()); | |
Song foo = new Song(); | |
System.out.println(foo.getName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Connor on 1/20/16. | |
*/ | |
public class Song extends Media implements Information { | |
private String title; | |
private String artist; | |
private String genre; | |
private int length; | |
//constructors | |
public Song() { | |
this.title = genString(); | |
this.artist = genString(); | |
this.length = (int) ((Math.random() * 90) + 60); | |
this.genre = genString(); | |
} | |
public Song(String title, String artist, int length, String genre) { | |
this.title = title; | |
this.artist = artist; | |
this.length = length; | |
this.genre = genre; | |
super.addSong(this); | |
} | |
//getters and setters | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getArtist() { | |
return artist; | |
} | |
public void setArtist(String artist) { | |
this.artist = artist; | |
} | |
@Override | |
public int getLength() { | |
return length; | |
} | |
public void setLength(int length) { | |
this.length = length; | |
} | |
public String getGenre() { | |
return genre; | |
} | |
public void setGenre(String genre) { | |
this.genre = genre; | |
} | |
@Override | |
public String getName(){ | |
return title; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment