Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.

Slides:



Advertisements
Similar presentations
G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Graphical User Interfaces
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Corresponds with Chapter 12
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Laying Out Components Interior Design for GUIs. Nov 042 What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame’s.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
3461 Laying Out Components Interior Design for GUIs.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Csc Basic Graphical User Interface (GUI) Components.
Clicker quick questions 10/17/13 CSE 1102 Fall 2013.
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Graphical User Interfaces Tonga Institute of Higher Education.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Chapter 7 A First Look at GUI Applications Layout Managers.
Graphical User Interfaces
Swing JComponents.
INFSY 547: WEB-Based Technologies
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Java Swing.
Tim McKenna Layout Mangers in Java Tim McKenna
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
Containers and Components
Steps to Creating a GUI Interface
Presentation transcript:

Graphical User Interface (GUI) Two-Dimensional Graphical Shapes

GUI Classes AWT (abstract windows toolkit) - java.awt package Swing - javax.swing package Take advantage of inheritance is-a relationships

GUI AWT Components (adapted from Figure 7.2 ContainerComponent JComponent JPanel Dialog JDialog Window Frame JFrame

package ballappsimple; import javax.swing.JFrame; public class BallAppSimple extends JFrame { public BallAppSimple (String title) { super(title); this.setSize(600, 350); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); this.add(new BallPanelSimple()); this.setVisible(true); } public static void main(String[] args) { BallAppSimple app = new BallAppSimple ("Simple Ball"); } Lab 7: BallAppSimple Note:JFrame Adds the panel to the frame

Import javax.swing.JPanel; public class BallPanelSimple extends JPanel { private final int INIT_X = 75; constant attributes private final int INIT_Y = 75; private final int DIAMETER = 60; private Ball _ball1;object of type Ball or class Ball public BallPanelSimple () { super(); this.setBackground (Color.yellow); _ball1=new Ball (java.awt.Color.red, this); _ball1.setLocation(INIT_X, INIT_Y); _ball1.setSize(DIAMETER, DIAMETER); } public void paintComponent (java.awt.Graphics aBrush) { super.paintComponent(aBrush); java.awt.Graphics2D betterBrush = (java.awt.Graphics2D) aBrush; _ball.fill(betterBrush); } Lab 7: BallAppSimple Note:JPanel

paintComponent used by JPanel to paint/repaint a panel public void paintComponent (java.awt.Graphics aBrush) { super.paintComponent(aBrush); java.awt.Graphics2D betterBrush = (java.awt.Graphics2D) aBrush; _ball.fill(betterBrush); } A shape including its color and other attributes Can draw 2D shapes better with The 2DGraphics (casting) Important call

repaint Automatically called when: panel changes size panel changes location panel is reopened after being resized repaint () calls paintComponent Programmer calls repaint() when something on the panel has changed All JPanels have a repaint method Paintbrush: Must always instantiate the super.paintComponent method and pass a Brush

import java.awt.Color; public class Ball extends java.awt.geom.Ellipse2D.Double { Color _fillColor; public Ball (Color aColor) { super (); this.setFillColor (aColor); } // more readable versions of methods provided by Java public void setLocation (double x, double y) { this.setFrame (x, y, this.getWidth(), this.getHeight()); } public void setSize (int aWidth, int aHeight) { this.setFrame(this.getX(), this.getY(), aWidth, aHeight); } public void setFillColor (java.awt.Color aColor) { _fillColor = aColor; } public void fill (java.awt.Graphics2D aBetterBrush){ java.awt.Color savedColor = aBetterBrush.getColor(); aBetterBrush.setColor(_fillColor); aBetterBrush.fill(this); aBetterBrush.setColor(savedColor); } }

Lab 8: Run the BallAppSimple Code Modify program to change height and width of the JFrame Modify program to change ball color and background color of the JPanel Modify the program to add another shape, perhaps a rectangle, in a different color to the JPanel. Experiment with size and location.

GUI Components and Screen Design GUI Demo Message Hello World CloseClearDisplay Panel Instance Textfield Instance Label Instance Button Instance Frame Instance

Screen Design Layout Managers facilitate better programmer control of interfaces!

Layout Managers Are objects that place components within a container determines component size determines component position Specifies a strategy for screen layout

Predefined Layout Manager Subclasses FlowLayout GridLayout BorderLayout BoxLayout CardLayout GridBagLayout Featured in text

Layout Managers Containers have default layout managers Programmer can explicitly set the layout manager Each has its own rules governing how components will be arranged

Border Layout (default for JFrame) North South CenterEastWest

BorderLayout public BallAppSimple (String title) { super(title); this.setSize(600, 350); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); this.add(new BallPanelSimple()); or this.add (new BallpanelSimple (), java.awt.BorderLayout.CENTER); this.setVisible(true); } North South CenterEastWest

Border Layout (pg 267) Each area displays a component such as a JPanel Each of the four outer areas enlarges as needed to accommodate the component added to it If nothing is added to the outer areas, they take up no space and other areas expand to fill the void The center area expands to fill space as needed

Methods common to all layouts add () remove () setLayout ()

public class ButtonPanelLabApp extends javax.swing.JFrame { public ButtonPanelLabApp (String title) { super(title); this.setSize(600, 350); this.setDefaultCloseOperation (javax.swing.JFrame.EXIT_ON_CLOSE); this.add(new ButtonPanel()); this.pack(); this.setVisible(true); } public static void main(String[] args) { ButtonPanelLabApp app = new ButtonPanelLabApp ("Simple Panel GUI Lab");} } Simple Application (Example of a Button Panel)

Calling pack makes JFrame resize to fit contents (default size for JFrame is (0,0) ); here it fits to enclose the button which has a default size Simple Application

ButtonPanel package buttonpanellab; import javax.swing.JButton; import javax.swing.JPanel; public class ButtonPanel extends JPanel { JButton _button1; public ButtonPanel () { _button = new JButton ("Click Me"); this.add(_button); } }

Lab – Creating a Second Panel Start with the BallAppSimple Code (Note: if you want to save it as is, make another copy in another package)

Lab 8 Continued – Add a Create Button Panelclass to program CreateButton creates a button, extends javax.swing.JButton Receives three parameters a color for the button, a string message to place on the button, and a panel on which the button will be placedl Send the message parameter to the super class JButton Set the button background to the color that is passed Add the button to the passed JPanel Do whatever else you desire to finish a button

Flow Layout Flow layout puts as many components as possible on a row, then moves to the next row Rows are created as needed to accommodate all of the components Components are displayed in the order they are added to the container Each row of components is centered horizontally in the window by default, but could also be aligned left or right The horizontal and vertical gaps between the components can be explicitly set

Grid Layout A grid layout presents a container’s components in a rectangular grid of rows and columns One component is placed in each cell of the grid, and all cells have the same size As components are added to the container, they fill the grid from left-to-right and top-to-bottom (by default) The size of each cell is determined by the overall size of the container setLayout (new GridLayout (, )

public class ButtonPanelGrid extends JPanel { JButton _button1, ; public ButtonPanelGrid () { super (); <set the layout to a grid layout of 4 rows with 2 buttons to each row; note that to apply this to the Panel, we need to precede the statement with either a panel name or, in this case “this”> <create each button in turn for a total of 8 buttons in this manner, adding and passing whatever is necessary> } Add another panel class ButtonPanelGrid.java

public class BallAppSimple2 extends javax.swing.JFrame { private BallPanelSimple _panel1; private ColorHolder _holder; public BallAppSimple2 (String title) { super (title); _holder= new ColorHolder(); BallPanelSimple _panel1 = new BallPanelSimple(); this.setSize(400, 250); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); add(new ButtonPanelGrid (_holder), BorderLayout.EAST); add(_panel1, BorderLayout.CENTER); this.setVisible(true); } public static void main(String[] args) { BallAppSimple2 app = new BallAppSimple2 ("Simple Ball and Buttons"); } Add the other JPanel to the JFrame

Two Panel Example