Chapter 8: Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames The relationship between frame and.

Slides:



Advertisements
Similar presentations
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Advertisements

Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 14 Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Graphics.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
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.
Event-Driven Programming
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 Review of applets & Computer Graphis GUILecture 6 Review of Applets & Compute Graphics GUI Overview  Introduction to Graphics. Applets: a quick review.
1 UFCE3T-15-M Object- oriented Design and Programming Block 3 GUI Programming Jin Sa.
Chapter 10 Getting Started with GUI Programming F GUI Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames F Layout.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Steven Holzner, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Session 7 Welcome Back!!!
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 11 Getting Started.
Chapter 10 Getting Started with GUI Programming F GUI Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames F Layout.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 11 Getting Started with GUI Programming 百闻不如一见.
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.
1 Chapter 10 Getting Started with Graphics Programming F Graphics Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
Basics of GUI Programming Chapter 11 and Chapter 22.
1 Chapter 2 Getting Started with GUI Programming.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
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.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 7 A First Look at GUI Applications Layout Managers.
Chapter 8 Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames  Creating & centering frames, adding.
Object-Orientated Analysis, Design and Programming
Advanced Java Programming CSE 7345/5345/ NTU 531
Modern Programming Language Java
Tim McKenna Layout Mangers in Java Tim McKenna
Chapter 13: Advanced GUIs and Graphics
Chapter 10 Getting Started with Graphics Programming
Chapter 13 Graphics.
Advanced GUIs and Graphics
Chapter 13 Graphics.
Graphical User Interface
Presentation transcript:

Chapter 8: Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames The relationship between frame and other UI components The relationship between frame and other UI components Event-Driven Programming Event-Driven Programming  Event Source, Listener, Listener Interface Layout Managers Layout Managers  FlowLayout, GridLayout, BorderLayout Panels Panels repaint(), update(), paint()and paintComponent() methods repaint(), update(), paint()and paintComponent() methods Drawing Geometric Figures Drawing Geometric Figures  Color, Font, FontMetrics classes  Drawing methods

Graphics Class Hierarchy (AWT)

Graphics Class Hierarchy (Swing)

Frames Frame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java graphical applications. Frame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java graphical applications. The Frame class can be used to create windows. The Frame class can be used to create windows.

UI Components

Frames, cont. import javax.swing.*; public class MyFrame { public static void main(String[] args) public static void main(String[] args) { JFrame f = new JFrame("Test Frame"); JFrame f = new JFrame("Test Frame"); f.setSize(400,300); f.setSize(400,300); f.setVisible(true); f.setVisible(true); }} Run

Event-Driven Programming Procedural programming is executed in procedural order. Procedural programming is executed in procedural order. In event-driven programming, code is executed upon activation of events. In event-driven programming, code is executed upon activation of events.

Events An event can be defined as a type of signal to the program that something has happened. An event can be defined as a type of signal to the program that something has happened. The event is generated by external user actions such as mouse movements, mouse button clicks, and keystrokes, or by the operating system, such as a timer. The event is generated by external user actions such as mouse movements, mouse button clicks, and keystrokes, or by the operating system, such as a timer.

Event Information id: A number that identifies the event. id: A number that identifies the event. target: The AWT component upon which the event occurred. target: The AWT component upon which the event occurred. arg: Additional information about the AWT components. arg: Additional information about the AWT components. x, y coordinates: The mouse pointer location when a mouse movement event occurred. x, y coordinates: The mouse pointer location when a mouse movement event occurred. clickCount: The number of consecutive clicks for the mouse events. For other events, it is zero. clickCount: The number of consecutive clicks for the mouse events. For other events, it is zero. when: The time stamp of the event. when: The time stamp of the event. key: The key that was pressed or released. key: The key that was pressed or released.

Event Classes

Selected User Actions SourceEvent Type User ActionObjectGenerated Clicked on a buttonJButtonActionEvent Changed textJTextComponentTextEvent Dbl-clicked on a list itemJListActionEvent Selected or deselectedJListItemEvent an item with a single click Desel/Selected an itemJComboBoxItemEvent

The Delegation Model

Selected Event Handlers Event ClassListener InterfaceListener Methods (Handlers) ========== ============== ====================== ActionEventActionListeneractionPerformed(ActionEvent) ItemEventItemListeneritemStateChanged(ItemEvent) WindowEventWindowListenerwindowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) ContainerEventContainerListenercomponentAdded(ContainerEvent) componentRemoved(CoantainerEvent)

Example 8.1 Closing Windows MyFrameWithExitHandling Run F Objective: Extend javax.swing.JFrame with window-closing capability.

Example 8.2 Handling Simple Mouse Events Objective: Create a frame and display a solid square at the mouse pointer when the mouse is clicked. Objective: Create a frame and display a solid square at the mouse pointer when the mouse is clicked. TestMouseEvent Run

Example 8.3 Handling Simple Action Events Objective: Display a Close button in the window; terminate the program by clicking the Close button in the window or on the title bar. Objective: Display a Close button in the window; terminate the program by clicking the Close button in the window or on the title bar. TestActionEvent Run

Layout Managers Java’s layout managers provide a level of abstraction to automatically map your user interface on all windowing systems. Java’s layout managers provide a level of abstraction to automatically map your user interface on all windowing systems. The UI components are placed in containers. Each container has a layout manager to arrange the UI components within the container. The UI components are placed in containers. Each container has a layout manager to arrange the UI components within the container.

Kinds of Layout Managers FlowLayout FlowLayout GridLayout GridLayout GridBagLayout GridBagLayout BorderLayout BorderLayout CardLayout CardLayout

Layout Manager Hierarchy

Example 8.4 Testing the FlowLayout Manager The components are arranged in the container from left to right in the order in which they were added. When one row becomes filled, a new row is started. ShowFlowLayoutRun

FlowLayout Constructors public FlowLayout(int align, int hGap, int vGap) public FlowLayout(int align, int hGap, int vGap) Constructs a new FlowLayout with a specified alignment, horizontal gap, and vertical gap. The gaps are the distances in pixel between components. public FlowLayout(int alignment) public FlowLayout(int alignment) Constructs a new FlowLayout with a specified alignment and a default gap of five pixels for both horizontal and vertical. public FlowLayout() public FlowLayout() Constructs a new FlowLayout with a default center alignment and a default gap of five pixels for both horizontal and vertical.

Example 8.5 Testing the GridLayout Manager The GridLayout manager arranges components in a grid (matrix) formation with the number of rows and columns defined by the constructor. The components are placed in the grid from left to right starting with the first row, then the second, and so on. ShowGridLayoutRun

GridLayout Constructors public GridLayout(int rows, int columns) public GridLayout(int rows, int columns) Constructs a new GridLayout with the specified number of rows and columns. public GridLayout(int rows, int columns, int hGap, int vGap) public GridLayout(int rows, int columns, int hGap, int vGap) Constructs a new GridLayout with the specified number of rows and columns, along with specified horizontal and vertical gaps between components.

Example 8.6 Testing the BorderLayout Manager The BorderLayout manager divides the window into five areas: East, South, West, North, and Center. Components are added to a BorderLayout by using add(String, Component), where String is "East", "South", "West", "North", or "Center". ShowBorderLayoutRun

Example 8.7 Testing Panel Panels act as smaller containers for grouping user interface components. Panels act as smaller containers for grouping user interface components. It is recommended that you place the user interface components in panels and place the panels in a frame. You can also place panels in a panel. It is recommended that you place the user interface components in panels and place the panels in a frame. You can also place panels in a panel. TestPanelsRun

repaint(), update(), paint(), and paintCompoent() The Java system automatically creates a default graphics context, an object of the Graphics class, and passes it as a parameter to the update(), paint(), and paintCompoent() methods. This object is local to those methods, and it cannot be used outside of those methods.

Example 8.7 Drawing on Panels JPanel can be used be used to draw graphics (including text) and enable user interaction. JPanel can be used be used to draw graphics (including text) and enable user interaction. RunPanelDrawingDemo

Colors Color c = new Color(r, g, b); r, g, and b specify a color by its red, green, and blue components. Example: Color c = new Color(128, 100, 100);

Setting Colors You can use the following methods to set the component’s background and foreground colors: setBackground(Color c) setForeground(Color c) Example: setBackground(Color.yellow); setForeground(Color.red);

Graphics Graphics Coordinate System Graphics Coordinate System Font and FontMetrics Font and FontMetrics Drawing Lines Drawing Lines Drawing Rectangles Drawing Rectangles Drawing Ovals Drawing Ovals Drawing Arcs Drawing Arcs Drawing Polygons Drawing Polygons

Graphics Coordinate System

Fonts Font myFont = Font(name, style, size); Example: Font myFont = new Font("TimesRoman", Font.BOLD, 16); Font myFont = new Font("Courier", Font.BOLD+Font.ITALIC, 12);

Font Example public void paint(Graphics g) { Font myFont = new Font("Times", Font.BOLD, 16); Font myFont = new Font("Times", Font.BOLD, 16); g.setFont(myFont); g.setFont(myFont); g.drawString("Welcome to Java", 20, 40); g.drawString("Welcome to Java", 20, 40); //set a new font //set a new font g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 12)); g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 12)); g.drawString("Welcome to Java", 20, 70); g.drawString("Welcome to Java", 20, 70);}

The FontMetrics Class

Get FontMetrics g.getFontMetrics(Font f); g.getFontMetrics(Font f); or or g.getFontMetrics(); g.getFontMetrics(); public int getAscent() public int getAscent() public int getDescent() public int getDescent() public int getLeading() public int getLeading() public int getHeight() public int getHeight() public int stringWidth(String str) public int stringWidth(String str)

Example 8.9 Using FontMetrics Objective: Display “Welcome to Java” in Helvetica 20-point bold, centered in the frame. Objective: Display “Welcome to Java” in Helvetica 20-point bold, centered in the frame. TestFontMetrics Run

Drawing Lines drawLine(x1, y1, x2, y2);

Drawing Rectangles drawRect(x, y, w, h); drawRect(x, y, w, h); fillRect(x, y, w, h); fillRect(x, y, w, h);

Drawing Rounded Rectangles drawRoundRect(x, y, w, h, aw, ah); drawRoundRect(x, y, w, h, aw, ah); fillRoundRect(x, y, w, h, aw, ah); fillRoundRect(x, y, w, h, aw, ah);

Drawing Ovals drawOval(x, y, w, h); drawOval(x, y, w, h); fillOval(x, y, w, h); fillOval(x, y, w, h);

Drawing Arcs drawArc(x, y, w, h, angle1, angle2); drawArc(x, y, w, h, angle1, angle2); fillArc(x, y, w, h, angle1, angle2); fillArc(x, y, w, h, angle1, angle2);

Drawing Polygons int x[] = {40, 70, 60, 45, 20}; int y[] = {20, 40, 80, 45, 60}; g.drawPolygon(x, y, x.length); g.fillPolygon(x, y, x.length);

Example 8.10 Drawing a Clock Objective: Use drawing and trigonometric methods to draw a clock showing the specified hour, minute, and second in a frame. Objective: Use drawing and trigonometric methods to draw a clock showing the specified hour, minute, and second in a frame. DisplayClock Run