Teknologi 3D pada Halaman WEB
Minggu, 03 Mei 2026
Sabtu, 02 Mei 2026
JAVA - Create Game Launcher (Gemini AI & Copilot)
prompt :
Creating a simple transition between two screens (panels) in NetBeans is most efficiently handled using a CardLayout. This allows you to stack multiple JPanel components and "flip" between them.
Here is a clean, step-by-step guide to setting this up.
1. The Main Frame Setup
The JFrame acts as the container. We will use a JPanel with a CardLayout to hold both your Menu and the Game screen.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameLauncher extends JFrame {
// CardLayout helps us switch between panels
private CardLayout cardLayout = new CardLayout();
private JPanel mainContainer = new JPanel(cardLayout);
public GameLauncher() {
setTitle("Simple Game Launcher");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create the two panels
JPanel menuPanel = createMenuPanel();
JPanel gamePanel = createGamePanel();
// Add panels to the container with unique names
mainContainer.add(menuPanel, "Menu");
mainContainer.add(gamePanel, "Game");
add(mainContainer);
setVisible(true);
}
// --- Panel 1: The Opening Menu ---
private JPanel createMenuPanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.DARK_GRAY);
panel.setLayout(new GridBagLayout()); // Centers the button
JButton playButton = new JButton("PLAY");
playButton.setPreferredSize(new Dimension(100, 40));
// Action: Switch to the "Game" panel when clicked
playButton.addActionListener(e -> cardLayout.show(mainContainer, "Game"));
panel.add(playButton);
return panel;
}
// --- Panel 2: The Game Screen ---
private JPanel createGamePanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setLayout(new GridBagLayout());
JLabel text = new JLabel("GAME BEGIN");
text.setForeground(Color.WHITE);
text.setFont(new Font("Arial", Font.BOLD, 24));
panel.add(text);
return panel;
}
public static void main(String[] args) {
// Run the GUI on the Event Dispatch Thread
SwingUtilities.invokeLater(GameLauncher::new);
}
}
2. Key Components Explained
| Feature | Description |
| CardLayout | Think of this like a deck of cards. Only one "card" (panel) is visible at a time. |
| cardLayout.show() | This method takes the container and the String name of the panel you want to display. |
| GridBagLayout | A simple way to ensure your buttons and text stay perfectly centered in the middle of the window. |
| Lambda Expression | The e -> ... syntax is a shorthand way to write the ActionListener for your button. |
3. How to do this in NetBeans GUI Builder (Design Mode)
If you prefer using the Drag-and-Drop interface in NetBeans:
Create a New JFrame Form: Right-click package > New > JFrame Form.
Add a Panel: Drag a
JPanelonto the Frame. Right-click it > Set Layout > Card Layout. (Let's call thismainPanel).Add Sub-Panels: Drag two more
JPanelcomponents insidemainPanel.In the Navigator (bottom left), give them variable names like
menuPanelandgamePanel.
Design:
Select
menuPaneland drag aJButtoninto it.Select
gamePaneland drag aJLabelinto it.
The Code: Double-click the "Play" button to generate the action event and paste this inside:
JavaCardLayout cl = (CardLayout) mainPanel.getLayout(); cl.show(mainPanel, "gamePanel");(Note: NetBeans usually names the cards based on the variable name of the panel added).
Copilot AI
Got it ✅ — you want a Java Swing program (can be built in NetBeans) with:
- JPanel 1: Contains Play and Close buttons.
- If Play is clicked → Switch to JPanel 2 with text
"Game Begin". - If Close is clicked → Exit the program.
Here’s a complete, runnable Java program that does exactly that.
It uses a CardLayout to switch between panels.
Java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class OpeningGame extends JFrame {
private CardLayout cardLayout;
private JPanel mainPanel;
public OpeningGame() {
setTitle("Opening Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null); // Center window
// CardLayout to switch between panels
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
// Create panels
JPanel panel1 = createOpeningPanel();
JPanel panel2 = createGamePanel();
// Add panels to CardLayout
mainPanel.add(panel1, "Opening");
mainPanel.add(panel2, "Game");
add(mainPanel);
setVisible(true);
}
// Panel 1: Opening screen with Play and Close buttons
private JPanel createOpeningPanel() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton playButton = new JButton("Play");
JButton closeButton = new JButton("Close");
// Play button action
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(mainPanel, "Game");
}
});
// Close button action
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(playButton);
panel.add(closeButton);
return panel;
}
// Panel 2: Game screen
private JPanel createGamePanel() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Game Begin");
label.setFont(new Font("Arial", Font.BOLD, 20));
panel.add(label);
return panel;
}
public static void main(String[] args) {
// Run GUI in Event Dispatch Thread
SwingUtilities.invokeLater(() -> new OpeningGame());
}
}
How it works:
- CardLayout is used to switch between
JPanel 1andJPanel 2. - Play button →
cardLayout.show(mainPanel, "Game")switches to the game panel. - Close button →
System.exit(0)closes the app. - NetBeans:
- Create a new Java Application project.
- Add this code to a
.javafile. - Run it — you’ll see the opening screen with two buttons.
JAVA - Quadcopter Game - Shape Circle
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package quadcopteranimation;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
public class QuadcopterSim extends JPanel implements ActionListener, KeyListener {
private int x = 250, y = 250; // Body position
private double angle = 0; // Rotor rotation angle
private final int speed = 5; // Movement speed
private Timer timer;
public QuadcopterSim() {
setFocusable(true);
addKeyListener(this);
// Timer fires every 16ms (~60 FPS)
timer = new Timer(16, this);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int bodyWidth = 35;
int bodyHeight = 35;
int spokeLen = 30;
// 1. Draw the Body (Rectangle)
g2d.setColor(Color.BLACK);
g2d.drawOval(x - bodyWidth/2, y - bodyHeight/2, bodyWidth, bodyHeight);
// 2. Draw Spokes and Rotors
// Relative positions for the 4 corners
int[][] offsets = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
for (int[] pos : offsets) {
int endX = x + (pos[0] * spokeLen);
int endY = y + (pos[1] * spokeLen);
// Draw Spoke
g2d.setColor(Color.GRAY);
g2d.drawLine(x, y, endX, endY);
// Draw Rotating Rotor
drawRotor(g2d, endX, endY, angle);
}
}
private void drawRotor(Graphics2D g2d, int cx, int cy, double angle) {
AffineTransform old = g2d.getTransform();
g2d.translate(cx, cy);
g2d.rotate(angle);
g2d.setColor(Color.RED);
// Draw the rotor circle
g2d.drawOval(-15, -15, 30, 30);
// Draw a line across the circle to show rotation
g2d.drawLine(-15, 0, 15, 0);
g2d.setTransform(old);
}
@Override
public void actionPerformed(ActionEvent e) {
angle += 0.3; // Spin the rotors
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) y -= speed;
if (key == KeyEvent.VK_S) y += speed;
if (key == KeyEvent.VK_A) x -= speed;
if (key == KeyEvent.VK_D) x += speed;
}
@Override public void keyReleased(KeyEvent e) {}
@Override public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Quadcopter GUI Control");
frame.add(new QuadcopterSim());
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
JAVA - Quadcopter Game - Bonus Code
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package quadcopteranimation4;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.Random;
public class QuadcopterAnimation4 extends JPanel {
// Quadcopter position and movement
private double x = 100, y = 100;
private double dx = 2, dy = 2; // Velocity
private double rotationAngle = 0; // Rotor rotation
private final int BODY_WIDTH = 60;
private final int BODY_HEIGHT = 50;
private final int ROTOR_SIZE = 30;
private final Random rand = new Random();
public QuadcopterAnimation4() {
// Animation Timer: ~60 FPS (16ms delay)
Timer timer = new Timer(16, e -> {
updatePosition();
rotationAngle += 0.5; // Speed of rotor spin
repaint();
});
timer.start();
}
private void updatePosition() {
x += dx;
y += dy;
// Bounce off walls and pick a random new direction
if (x < 0 || x > getWidth() - BODY_WIDTH - ROTOR_SIZE) {
dx = -dx + (rand.nextDouble() - 0.5);
x = Math.max(0, Math.min(x, getWidth() - BODY_WIDTH));
}
if (y < 0 || y > getHeight() - BODY_HEIGHT - ROTOR_SIZE) {
dy = -dy + (rand.nextDouble() - 0.5);
y = Math.max(0, Math.min(y, getHeight() - BODY_HEIGHT));
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 1. Draw Body
g2d.setColor(Color.ORANGE);
g2d.fillRect((int)x, (int)y, BODY_WIDTH, BODY_HEIGHT);
// 2. Draw 4 Rotors (Corners)
drawRotor(g2d, x, y); // Top Left
drawRotor(g2d, x + BODY_WIDTH, y); // Top Right
drawRotor(g2d, x, y + BODY_HEIGHT); // Bottom Left
drawRotor(g2d, x + BODY_WIDTH, y + BODY_HEIGHT); // Bottom Right
}
private void drawRotor(Graphics2D g2d, double cx, double cy) {
g2d.setColor(Color.BLACK);
// Draw the outer circle
Ellipse2D circle = new Ellipse2D.Double(cx - ROTOR_SIZE/2.0, cy - ROTOR_SIZE/2.0, ROTOR_SIZE, ROTOR_SIZE);
g2d.draw(circle);
// Draw 4 Rotating Spokes
for (int i = 0; i < 4; i++) {
double angle = rotationAngle + (i * Math.PI / 2);
int x2 = (int) (cx + Math.cos(angle) * (ROTOR_SIZE / 2.0));
int y2 = (int) (cy + Math.sin(angle) * (ROTOR_SIZE / 2.0));
g2d.drawLine((int)cx, (int)cy, x2, y2);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Quadcopter Random Flight");
frame.add(new QuadcopterAnimation4());
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
JAVA - Quadcopter Game 4
JAVA - Quadcopter Game 3
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package quadcoptergame4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.Random;
public class QuadcopterGame4 extends JPanel implements ActionListener, KeyListener {
// Game Constants
private final int WIDTH = 800;
private final int HEIGHT = 600;
private Timer timer;
private int score = 0;
private boolean gameOver = false;
// Player Properties
private double playerX = 400, playerY = 300;
private double playerSpeed = 5;
private double rotorAngle = 0;
// Game Entities
private ArrayList<Point2D.Double> playerMissiles = new ArrayList<>();
private ArrayList<Enemy> enemies = new ArrayList<>();
private ArrayList<Point2D.Double> enemyMissiles = new ArrayList<>();
private Random rand = new Random();
public QuadcopterGame4() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
// Initialize 20 enemies
for (int i = 0; i < 20; i++) {
enemies.add(new Enemy(rand.nextInt(WIDTH), rand.nextInt(HEIGHT)));
}
// Game Loop (approx 60 FPS)
timer = new Timer(16, this);
timer.start();
// Enemy firing timer (every 2 seconds)
Timer fireTimer = new Timer(2000, e -> {
if (!enemies.isEmpty() && !gameOver) {
Enemy shooter = enemies.get(rand.nextInt(enemies.size()));
enemyMissiles.add(new Point2D.Double(shooter.x, shooter.y));
}
});
fireTimer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (gameOver) {
g2.setColor(Color.RED);
g2.setFont(new Font("Arial", Font.BOLD, 50));
g2.drawString("GAME OVER", WIDTH/2 - 150, HEIGHT/2);
g2.setFont(new Font("Arial", Font.PLAIN, 20));
g2.drawString("Final Score: " + score, WIDTH/2 - 60, HEIGHT/2 + 40);
return;
}
// Draw Score
g2.setColor(Color.WHITE);
g2.drawString("Score: " + score, 20, 30);
// Draw Player (Quadcopter)
drawQuadcopter(g2, playerX, playerY);
// Draw Player Missiles
g2.setColor(Color.YELLOW);
for (Point2D.Double m : playerMissiles) g2.fill(new Ellipse2D.Double(m.x, m.y, 5, 5));
// Draw Enemies
g2.setColor(Color.RED);
for (Enemy e : enemies) g2.fill(new Rectangle2D.Double(e.x, e.y, 20, 20));
// Draw Enemy Missiles
g2.setColor(Color.ORANGE);
for (Point2D.Double m : enemyMissiles) g2.fill(new Ellipse2D.Double(m.x, m.y, 8, 8));
}
private void drawQuadcopter(Graphics2D g2, double x, double y) {
g2.setColor(Color.CYAN);
rotorAngle += 0.5; // Spin speed
// Draw 4 Spokes and Rotors
int[][] offsets = {{-20,-20}, {20,-20}, {-20,20}, {20,20}};
for (int[] offset : offsets) {
double rx = x + offset[0];
double ry = y + offset[1];
g2.draw(new Line2D.Double(x, y, rx, ry));
// Spinning rotor effect
AffineTransform old = g2.getTransform();
g2.translate(rx, ry);
g2.rotate(rotorAngle);
g2.draw(new Ellipse2D.Double(-10, -10, 20, 20));
g2.draw(new Line2D.Double(-10, 0, 10, 0)); // Spoke inside rotor
g2.setTransform(old);
}
g2.fill(new Ellipse2D.Double(x-10, y-10, 20, 20)); // Body
}
@Override
public void actionPerformed(ActionEvent e) {
if (gameOver) return;
// Move Player Missiles
for (int i = 0; i < playerMissiles.size(); i++) {
playerMissiles.get(i).y -= 10;
if (playerMissiles.get(i).y < 0) playerMissiles.remove(i);
}
// Move Enemy Missiles
for (int i = 0; i < enemyMissiles.size(); i++) {
enemyMissiles.get(i).y += 5;
// Check Collision with Player
if (enemyMissiles.get(i).distance(playerX, playerY) < 20) gameOver = true;
if (enemyMissiles.get(i).y > HEIGHT) enemyMissiles.remove(i);
}
// Move Enemies and Check Collision
for (int i = 0; i < enemies.size(); i++) {
Enemy en = enemies.get(i);
en.update();
// Check Player Missile hit Enemy
for (int j = 0; j < playerMissiles.size(); j++) {
if (playerMissiles.get(j).distance(en.x, en.y) < 20) {
enemies.remove(i);
playerMissiles.remove(j);
score += 10;
break;
}
}
}
repaint();
}
// Controls
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) playerY -= playerSpeed;
if (key == KeyEvent.VK_S) playerY += playerSpeed;
if (key == KeyEvent.VK_A) playerX -= playerSpeed;
if (key == KeyEvent.VK_D) playerX += playerSpeed;
if (key == KeyEvent.VK_SPACE) playerMissiles.add(new Point2D.Double(playerX, playerY));
}
// Inner class for Enemy behavior
class Enemy {
double x, y, dx, dy;
Enemy(double x, double y) {
this.x = x; this.y = y;
this.dx = (rand.nextDouble() - 0.5) * 4;
this.dy = (rand.nextDouble() - 0.5) * 4;
}
void update() {
x += dx; y += dy;
if (x < 0 || x > WIDTH) dx *= -1;
if (y < 0 || y > HEIGHT) dy *= -1;
}
}
// Boilers
@Override public void keyReleased(KeyEvent e) {}
@Override public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Quadcopter Combat");
QuadcopterGame4 game = new QuadcopterGame4();
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
JAVA - Quadcopter Game 2
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package quadcoptergame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class QuadcopterGame extends JPanel implements ActionListener {
// Positioning and Physics
private double x = 300, y = 300;
private double angle = 0;
private final double speed = 4.0;
private final double rotationSpeed = 0.15;
// Missile Management
private class Missile {
double mx, my;
Missile(double x, double y) { this.mx = x; this.my = y; }
void update() { my -= 10; } // Move upward
}
private ArrayList<Missile> missiles = new ArrayList<>();
// Key Handling
private final Set<Integer> activeKeys = new HashSet<>();
private Timer timer;
public QuadcopterGame() {
setFocusable(true);
// Standard Timer for 60 FPS (approx 16ms delay)
timer = new Timer(16, this);
timer.start();
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) { activeKeys.add(e.getKeyCode()); }
@Override
public void keyReleased(KeyEvent e) { activeKeys.remove(e.getKeyCode()); }
});
}
private void updateLogic() {
if (activeKeys.contains(KeyEvent.VK_W)) y -= speed;
if (activeKeys.contains(KeyEvent.VK_S)) y += speed;
if (activeKeys.contains(KeyEvent.VK_A)) x -= speed;
if (activeKeys.contains(KeyEvent.VK_D)) x += speed;
if (activeKeys.contains(KeyEvent.VK_SPACE)) {
missiles.add(new Missile(x + 20, y));
activeKeys.remove(KeyEvent.VK_SPACE); // Fire once per press
}
// Update spokes rotation and missiles
angle += rotationSpeed;
missiles.forEach(Missile::update);
missiles.removeIf(m -> m.my < 0); // Clean up off-screen missiles
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw Missiles
g2d.setColor(Color.RED);
for (Missile m : missiles) g2d.fillRect((int)m.mx, (int)m.my, 5, 15);
// Draw Quadcopter Body
g2d.setColor(Color.ORANGE);
g2d.fillRect((int)x, (int)y, 40, 40);
// Draw 4 Rotating Spokes
g2d.setColor(Color.BLACK);
drawRotor(g2d, x, y); // Top Left
drawRotor(g2d, x + 40, y); // Top Right
drawRotor(g2d, x, y + 40); // Bottom Left
drawRotor(g2d, x + 40, y + 40); // Bottom Right
}
private void drawRotor(Graphics2D g2d, double rx, double ry) {
AffineTransform old = g2d.getTransform();
g2d.translate(rx, ry);
g2d.rotate(angle);
g2d.draw(new Line2D.Double(-15, 0, 15, 0));
g2d.draw(new Line2D.Double(0, -15, 0, 15));
g2d.drawOval(-15, -15, 30, 30);
g2d.setTransform(old);
}
@Override
public void actionPerformed(ActionEvent e) {
updateLogic();
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Java Quadcopter Sim");
frame.add(new QuadcopterGame());
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}







