Java Swing – Frame Creation &
Shape Drawing with Menus
• Java Swing: Part of Java Foundation Classes
(JFC), used for building GUI applications.
• Provides components like frames, panels,
buttons, and menus.
• In this seminar:
• - Create a frame with user-defined size and
position.
• - Use command line arguments.
• - Add menus to draw different shapes with
colors.
Frame in Java Swing
• JFrame: Main window where components are
placed.
• Methods:
• - setSize(width, height) → Sets frame
dimensions.
• - setLocation(x, y) → Sets position on screen.
• - setVisible(true) → Makes frame visible.
• A frame is the container for all GUI
components.
Command Line Arguments
• Definition: Values passed to the program
when it is executed.
• In Java, stored in the args[] array of main().
• Example: java FrameExample 400 300 200 150
• - Width = 400, Height = 300, Position (x=200,
y=150)
• Allows dynamic input from user instead of
hardcoding values.
Simple Frame Example
• JFrame frame = new JFrame("Title");
• [Link](width, height);
• [Link](x, y);
• [Link](true);
• Frame is created with user inputs.
• Example: java FrameExample 400 300 200 150
→ Frame at position (200,150), size 400x300.
Menus in Swing
• Purpose: Provide user options (e.g., select
shape).
• Components:
• - JMenuBar: Top bar that holds menus.
• - JMenu: Dropdown menu (e.g., Shapes).
• - JMenuItem: Clickable items (e.g., Circle,
Rectangle).
• Example:
• JMenuBar mb = new JMenuBar();
Full Code (Simplified)
• class ShapeFrame extends JFrame implements
ActionListener {
• String shape = "";
• ShapeFrame(int w, int h, int x, int y) {
• setSize(w, h); setLocation(x, y);
• JMenuBar mb = new JMenuBar();
• JMenu m = new JMenu("Shapes");
• JMenuItem circle = new
JMenuItem("Circle");
Explanation of Program
• Frame Creation: Takes size & position from
command line arguments.
• Menus: Provides options for shapes.
• ActionListener: Detects which menu item is
selected.
• Paint() Method: Draws the selected shape on
the frame.
• Colors: [Link]([Link]) sets drawing
color.
Output
• Example run: java ShapeFrame 500 400 100
100
• Frame of 500x400 at position (100,100).
• User selects shape from menu.
• Shape is drawn in red.
Conclusion
• Frame is the base window for Swing apps.
• Command line arguments give flexibility in
frame size & position.
• Menus provide user interaction.
• Shapes can be drawn and customized with
colors.
• ✅ This completes the seminar on Java Swing
Frame & Shape Drawing Application.
Thank You
• Thank you for your attention!
• Any questions?