0% found this document useful (0 votes)
8 views6 pages

Java GUI Notes

This document provides quick notes on Java Swing GUI components and their usage for a game application. It covers essential elements such as JFrame, JPanel, JButton, and layout management, along with game-specific concepts like screen management and action handling. Additionally, it includes tips for maintaining a responsive UI and common pitfalls to avoid during development.

Uploaded by

raphaelvicuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Java GUI Notes

This document provides quick notes on Java Swing GUI components and their usage for a game application. It covers essential elements such as JFrame, JPanel, JButton, and layout management, along with game-specific concepts like screen management and action handling. Additionally, it includes tips for maintaining a responsive UI and common pitfalls to avoid during development.

Uploaded by

raphaelvicuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Swing GUI — Quick Notes for

Open-Notes Exam
⚙️ SWING BASICS
JFrame: The main application window.​

java​
CopyEdit​
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);

●​
●​ JPanel: A container used to group components.​

○​ Override paintComponent(Graphics g) to draw custom graphics or


background.​

JLabel: Displays text or images.​



java​
CopyEdit​
JLabel label = new JLabel("Hello!");

●​

JButton: A clickable button.​



java​
CopyEdit​
JButton button = new JButton("Attack");
button.addActionListener(e -> doSomething());

●​
●​ JTextArea / JTextField:​
○​ JTextArea: Multi-line, good for logs.​

○​ JTextField: Single-line, for input.​

ImageIcon: Used to load images for backgrounds, icons, or sprites.​



java​
CopyEdit​
ImageIcon icon = new
ImageIcon(getClass().getResource("/images/bg.png"));

●​
●​ Layouts:​

○​ BorderLayout: NORTH, SOUTH, CENTER, EAST, WEST.​

○​ BoxLayout: Horizontal or vertical stacking.​

○​ FlowLayout: Default layout, left-to-right.​

🎮 GAME-SPECIFIC SWING CONCEPTS


📺 Screens
●​ Use different JPanels for game screens:​

○​ StartScreenGUI​

○​ SetupScreenGUI​

○​ BattleScreenGUI​

●​ Use JFrame.setContentPane(panel) to switch between screens.​

🎨 Custom Backgrounds
Use a custom JPanel to paint a scaled image:​

java​
CopyEdit​
class BackgroundPanel extends JPanel {
private Image bg;
public BackgroundPanel(String path) {
bg = new ImageIcon(getClass().getResource(path)).getImage();
}

protected void paintComponent(Graphics g) {


super.paintComponent(g);
// Scale and draw bg
}
}

●​

🕹️ Buttons & Actions


Buttons trigger turn logic:​

java​
CopyEdit​
attackButton.addActionListener(e -> handlePlayerTurn(1));
defendButton.addActionListener(e -> handlePlayerTurn(2));
chargeButton.addActionListener(e -> handlePlayerTurn(3));

●​
●​ Game state (like health, charging, etc.) updates through executeTurn().​

🔁 GAME LOOP LOGIC (IN GUI)


●​ Player clicks an action → triggers handlePlayerTurn(int action)​

●​ executeTurn(playerAction, opponentAction, isPlayer)​

●​ Opponent AI uses opponent.think() to decide move.​


●​ Output is shown using a JTextArea log.​

🔤 DISPLAY ELEMENTS
●​ Health bars can be simple JLabels, optionally with progress bar visuals.​

●​ Use setText() to update player/opponent info dynamically.​

🧠 GUI HELPER TIPS


●​ Use revalidate() and repaint() after dynamic GUI updates.​

●​ Organize logic to separate GUI code from core game logic when possible.​

●​ Group related components (e.g., action buttons in a JPanel with FlowLayout).​

✅ COMPONENT QUICK LIST


Componen Purpose Notes
t

JFrame Main game window Only one per app

JPanel Screen container Can override


paintComponent

JButton User input (attack/defend) Attach ActionListener

JLabel Display text/images setText() to update

JTextArea Battle log output Use .append() for new lines

ImageIcon Background or sprites Load with getResource()


💬 MISC TIPS
Always run GUI on the Event Dispatch Thread (EDT):​

java​
CopyEdit​
SwingUtilities.invokeLater(() -> new GameWindow());

●​

Set fonts and styles for consistency:​



java​
CopyEdit​
label.setFont(new Font("Arial", Font.BOLD, 16));

●​

Disable buttons when needed (e.g., during opponent's turn):​



java​
CopyEdit​
attackButton.setEnabled(false);

●​
●​ Use padding and layout properly to prevent cluttered UI.​

🧪 EXAMPLE: Creating a Button Panel


java
CopyEdit
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(attackButton);
buttonPanel.add(defendButton);
buttonPanel.add(chargeButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);

🚨 COMMON EXAM GOTCHAS


●​ Forgetting to call repaint() or revalidate() after updating UI​

●​ Not using .getResource() properly for image paths (must be in resources)​

●​ Using absolute paths or invalid layouts (e.g., nested panels without layouts)​

●​ Blocking the Event Dispatch Thread with long-running code

You might also like