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);
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);
ScrollingTextPanel level1Panel = new ScrollingTextPanel("java");
JPanel level3Panel = new JPanel();
level3Panel.add(new JLabel("Level 3 - Coming Soon!"));
mainPanel.add(menuPanel, "Menu");
mainPanel.add(level1Panel, "Level1");
mainPanel.add(level3Panel, "Level3");
btnLevel1.addActionListener(e -> {
cardLayout.show(mainPanel, "Level1");
level1Panel.startScrolling();
});
btnClose.addActionListener(e -> System.exit(0));
btnLevel3.addActionListener(e -> cardLayout.show(mainPanel, "Level3"));
add(mainPanel);
}
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);
}
public void startScrolling() {
x = getWidth();
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;
if (x + getFontMetrics(getFont()).stringWidth(text) < 0) {
x = getWidth();
}
repaint();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
GameLauncher launcher = new GameLauncher();
launcher.setVisible(true);
});
}
}
How It Works
CardLayout is used to switch between the menu and game panels.ScrollingTextPanel uses a javax.swing.Timer to animate "java" moving from right to left.- Buttons:
- Level 1 → switches to the scrolling text panel.
- Close → exits the program.
- Level 3 → shows a placeholder panel.
- Animation runs smoothly at ~33 FPS (30ms delay).
Running in NetBeans
- Create a Java Application project.
- Replace the default
Main.java with the above code. - 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:
Posting Komentar