Rabu, 06 Mei 2026

JAVA Netbeans Menu Launcher Game 5 (Scrolling Text)

 



prompt

java netbeans swing gui canvas jpanel create menu game launcher button "level 1", "close ", "level 3", if button "level 1" clicked then open new jpanel with text="java" scrolling animation from right to left, if button "close" clicked then windows close 


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GameLauncher extends JFrame {

    private CardLayout cardLayout;
    private JPanel mainPanel;

    public GameLauncher() {
        setTitle("Game Launcher");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);

        // Menu Panel
        JPanel menuPanel = new JPanel();
        menuPanel.setLayout(new GridLayout(3, 1, 10, 10));

        JButton btnLevel1 = new JButton("Level 1");
        JButton btnClose = new JButton("Close");
        JButton btnLevel3 = new JButton("Level 3");

        menuPanel.add(btnLevel1);
        menuPanel.add(btnClose);
        menuPanel.add(btnLevel3);

        // Level 1 Panel with scrolling text
        ScrollingTextPanel level1Panel = new ScrollingTextPanel("java");

        // Level 3 placeholder panel
        JPanel level3Panel = new JPanel();
        level3Panel.add(new JLabel("Level 3 - Coming Soon!"));

        // Add panels to CardLayout
        mainPanel.add(menuPanel, "Menu");
        mainPanel.add(level1Panel, "Level1");
        mainPanel.add(level3Panel, "Level3");

        // Button actions
        btnLevel1.addActionListener(e -> {
            cardLayout.show(mainPanel, "Level1");
            level1Panel.startScrolling();
        });

        btnClose.addActionListener(e -> System.exit(0));

        btnLevel3.addActionListener(e -> cardLayout.show(mainPanel, "Level3"));

        add(mainPanel);
    }

    // Panel with scrolling text animation
    static class ScrollingTextPanel extends JPanel implements ActionListener {
        private String text;
        private int x;
        private Timer timer;

        public ScrollingTextPanel(String text) {
            this.text = text;
            setBackground(Color.BLACK);
            setForeground(Color.GREEN);
            setFont(new Font("Arial", Font.BOLD, 40));
            timer = new Timer(30, this); // 30ms refresh
        }

        public void startScrolling() {
            x = getWidth(); // start from right edge
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(getForeground());
            g.setFont(getFont());
            g.drawString(text, x, getHeight() / 2);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            x -= 2; // move left
            if (x + getFontMetrics(getFont()).stringWidth(text) < 0) {
                x = getWidth(); // reset to right
            }
            repaint();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GameLauncher launcher = new GameLauncher();
            launcher.setVisible(true);
        });
    }
}

How It Works

  1. CardLayout is used to switch between the menu and game panels.
  2. ScrollingTextPanel uses a javax.swing.Timer to animate "java" moving from right to left.
  3. Buttons:
    • Level 1 → switches to the scrolling text panel.
    • Close → exits the program.
    • Level 3 → shows a placeholder panel.
  4. Animation runs smoothly at ~33 FPS (30ms delay).

Running in NetBeans

  1. Create a Java Application project.
  2. Replace the default Main.java with the above code.
  3. Press Run ▶️.

If you want, I can add a "Back to Menu" button inside the Level 1 and Level 3 panels so you can return without restarting the app.
Do you want me to add that?

Tidak ada komentar: