|
1 | | -import java.io.*; |
2 | | -import java.net.*; |
3 | | -import java.util.*; |
| 1 | +import javax.swing.*; |
| 2 | +import javax.swing.border.LineBorder; |
4 | 3 |
|
5 | | -public class MotivationalQuote { |
| 4 | +import java.awt.*; |
6 | 5 |
|
7 | | - private static final String QUOTE_FILE = "quotes.txt"; |
| 6 | +public class RandomQuoteGenerator { |
8 | 7 |
|
9 | | - public static void main(String[] args) { |
10 | | - Scanner scanner = new Scanner(System.in); |
11 | | - System.out.println("Choose an option:"); |
12 | | - System.out.println("1. Show random local quote"); |
13 | | - System.out.println("2. Fetch quote from API"); |
14 | | - System.out.print("Enter choice: "); |
15 | | - |
16 | | - int choice = scanner.nextInt(); |
17 | | - scanner.close(); |
18 | | - |
19 | | - switch (choice) { |
20 | | - case 1: |
21 | | - showRandomLocalQuote(); |
22 | | - break; |
23 | | - case 2: |
24 | | - fetchQuoteFromAPI(); |
25 | | - break; |
26 | | - default: |
27 | | - System.out.println("Invalid choice!"); |
28 | | - } |
| 8 | + private String[] quotes = { |
| 9 | + "Life isn’t about getting and having, it’s about giving and being. - Kevin Kruse", |
| 10 | + "Whatever the mind of man can conceive and believe, it can achieve. - Napoleon Hill", |
| 11 | + "The only way to do great work is to love what you do. - Steve Jobs" |
| 12 | + }; |
| 13 | + |
| 14 | + public String getRandomQuote() { |
| 15 | + int randomIndex = (int) (Math.random() * quotes.length); |
| 16 | + return quotes[randomIndex]; |
29 | 17 | } |
30 | 18 |
|
31 | | - // Option 1: Read from local file |
32 | | - private static void showRandomLocalQuote() { |
33 | | - List<String> quotes = new ArrayList<>(); |
34 | | - try (BufferedReader br = new BufferedReader(new FileReader(QUOTE_FILE))) { |
35 | | - String line; |
36 | | - while ((line = br.readLine()) != null) { |
37 | | - if (!line.trim().isEmpty()) { |
38 | | - quotes.add(line.trim()); |
39 | | - } |
40 | | - } |
| 19 | + public static void main(String[] args) { |
| 20 | + RandomQuoteGenerator generator = new RandomQuoteGenerator(); |
41 | 21 |
|
42 | | - if (quotes.isEmpty()) { |
43 | | - System.out.println("No quotes found in file!"); |
44 | | - return; |
45 | | - } |
| 22 | + // Create JFrame |
| 23 | + JFrame frame = new JFrame("Seep"); |
| 24 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 25 | + frame.setSize(1920, 1080); |
| 26 | + frame.setResizable(false); |
| 27 | + frame.setLayout(new BorderLayout()); |
46 | 28 |
|
47 | | - Random random = new Random(); |
48 | | - int index = random.nextInt(quotes.size()); |
49 | | - System.out.println("\n💬 Random Quote:\n" + quotes.get(index)); |
| 29 | + // Load image |
| 30 | + ImageIcon image = new ImageIcon("Seep.png"); |
| 31 | + JLabel imageLabel = new JLabel(image); |
| 32 | + imageLabel.setHorizontalAlignment(JLabel.CENTER); |
| 33 | + imageLabel.setVerticalAlignment(JLabel.CENTER); |
| 34 | + imageLabel.setLayout(new BorderLayout()); // enable overlay of quote |
50 | 35 |
|
51 | | - } catch (IOException e) { |
52 | | - System.out.println("Error reading quotes file: " + e.getMessage()); |
53 | | - } |
54 | | - } |
| 36 | + // Quote label (white text on black background) |
| 37 | + JLabel quoteLabel = new JLabel(generator.getRandomQuote(), SwingConstants.CENTER); |
| 38 | + quoteLabel.setFont(new Font("Arial", Font.BOLD, 16)); |
| 39 | + quoteLabel.setForeground(Color.WHITE); |
| 40 | + quoteLabel.setBackground(Color.BLACK); |
| 41 | + quoteLabel.setOpaque(true); |
| 42 | + quoteLabel.setHorizontalAlignment(SwingConstants.CENTER); |
| 43 | + quoteLabel.setVerticalAlignment(SwingConstants.CENTER); |
| 44 | + |
| 45 | + // Overlay quote on image |
| 46 | + imageLabel.add(quoteLabel, BorderLayout.CENTER); |
| 47 | + |
| 48 | + // Create top panel for two buttons |
| 49 | + JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10)); |
| 50 | + topPanel.setOpaque(true); |
| 51 | + topPanel.setBackground(Color.BLACK); // transparent so image shows behind |
| 52 | + |
| 53 | + JButton button1 = new JButton("Fetch Quote from API"); |
| 54 | + button1.setFont(new Font("Arial", Font.PLAIN, 16)); |
| 55 | + button1.setBackground(new Color(30, 30, 30)); |
| 56 | + button1.setForeground(Color.WHITE); |
| 57 | + button1.setFocusPainted(false); |
| 58 | + |
| 59 | + JButton fullscreenbutton = new JButton("Fullscreen"); |
| 60 | + fullscreenbutton.setFont(new Font("Arial", Font.PLAIN, 16)); |
| 61 | + fullscreenbutton.setBackground(new Color(30, 30, 30)); |
| 62 | + fullscreenbutton.setForeground(Color.WHITE); |
| 63 | + fullscreenbutton.setFocusPainted(false); |
| 64 | + |
| 65 | + GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); |
55 | 66 |
|
56 | | - // Option 2: Fetch from public API |
57 | | - private static void fetchQuoteFromAPI() { |
58 | | - try { |
59 | | - URL url = new URL("https://api.quotable.io/random"); |
60 | | - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
61 | | - conn.setRequestMethod("GET"); |
62 | | - |
63 | | - if (conn.getResponseCode() == 200) { |
64 | | - BufferedReader in = new BufferedReader( |
65 | | - new InputStreamReader(conn.getInputStream())); |
66 | | - StringBuilder response = new StringBuilder(); |
67 | | - String line; |
68 | | - |
69 | | - while ((line = in.readLine()) != null) { |
70 | | - response.append(line); |
71 | | - } |
72 | | - in.close(); |
73 | | - |
74 | | - // Simple extraction of the "content" field |
75 | | - String json = response.toString(); |
76 | | - String quote = json.split("\"content\":\"")[1].split("\",")[0]; |
77 | | - System.out.println("\n🌐 Fetched Quote:\n" + quote); |
| 67 | + boolean[] isFullScreen = {false}; // use array to allow modification in lambda |
| 68 | + |
| 69 | + fullscreenbutton.addActionListener(e -> { |
| 70 | + if (!isFullScreen[0]) { |
| 71 | + frame.dispose(); |
| 72 | + frame.setUndecorated(true); |
| 73 | + device.setFullScreenWindow(frame); |
| 74 | + frame.setVisible(true); |
| 75 | + isFullScreen[0] = true; |
78 | 76 | } else { |
79 | | - System.out.println("Failed to fetch quote from API."); |
| 77 | + device.setFullScreenWindow(null); |
| 78 | + frame.dispose(); |
| 79 | + frame.setUndecorated(false); |
| 80 | + frame.setExtendedState(JFrame.NORMAL); |
| 81 | + frame.setSize(1920, 1080); |
| 82 | + frame.setVisible(true); |
| 83 | + isFullScreen[0] = false; |
80 | 84 | } |
| 85 | + }); |
| 86 | + |
| 87 | + |
| 88 | + topPanel.add(button1); |
| 89 | + topPanel.add(fullscreenbutton); |
| 90 | + |
| 91 | + // Create bottom "New Quote" button |
| 92 | + JButton newQuoteButton = new JButton("New Quote"); |
| 93 | + newQuoteButton.setFont(new Font("Arial", Font.PLAIN, 18)); |
| 94 | + newQuoteButton.setFocusPainted(false); |
| 95 | + newQuoteButton.setBackground(new Color(30, 30, 30)); // dark gray |
| 96 | + newQuoteButton.setForeground(Color.WHITE); |
| 97 | + newQuoteButton.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); |
| 98 | + newQuoteButton.setPreferredSize(new Dimension(200, 60)); |
| 99 | + newQuoteButton.addActionListener(e -> quoteLabel.setText(generator.getRandomQuote())); |
| 100 | + |
| 101 | + // Main panel to hold image + bottom button |
| 102 | + JPanel mainPanel = new JPanel(new BorderLayout()); |
| 103 | + mainPanel.add(imageLabel, BorderLayout.CENTER); |
| 104 | + mainPanel.add(newQuoteButton, BorderLayout.SOUTH); |
| 105 | + |
| 106 | + // Add panels to frame |
| 107 | + frame.add(topPanel, BorderLayout.NORTH); |
| 108 | + frame.add(mainPanel, BorderLayout.CENTER); |
| 109 | + |
| 110 | + // Show window |
| 111 | + frame.setVisible(true); |
81 | 112 |
|
82 | | - } catch (Exception e) { |
83 | | - System.out.println("Error fetching quote: " + e.getMessage()); |
| 113 | + // Auto-update quote every 5 seconds |
| 114 | + new Timer(5000, e -> quoteLabel.setText(generator.getRandomQuote())).start(); |
| 115 | + // Make button1 disappear after 5 seconds |
| 116 | + Timer hideTimer = new Timer(5000, e -> button1.setVisible(false)); |
| 117 | + hideTimer.setRepeats(false); // only hide once |
| 118 | + hideTimer.start(); |
| 119 | + |
| 120 | + // Make button1 visible again when the mouse moves |
| 121 | + frame.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { |
| 122 | + @Override |
| 123 | + public void mouseMoved(java.awt.event.MouseEvent e) { |
| 124 | + button1.setVisible(true); |
| 125 | + // Restart the hide timer |
| 126 | + hideTimer.restart(); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + Timer hideTimer2 = new Timer(5000, e -> fullscreenbutton.setVisible(false)); |
| 131 | + hideTimer2.setRepeats(false); // only hide once |
| 132 | + hideTimer2.start(); |
| 133 | + |
| 134 | + frame.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { |
| 135 | + @Override |
| 136 | + public void mouseMoved(java.awt.event.MouseEvent e) { |
| 137 | + fullscreenbutton.setVisible(true); |
| 138 | + // Restart the hide timer |
| 139 | + hideTimer2.restart(); |
84 | 140 | } |
| 141 | + }); |
| 142 | + |
| 143 | + |
85 | 144 | } |
86 | 145 | } |
0 commit comments