Field Trip #19 Animations with Java By Keith Lynn.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

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
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Event Handling Events and Listeners Timers and Animation.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
CS 112 GUI 06 May 2008 Bilkent. Java GUI API Containers: ◦ contain other GUI components. E.g, Window, Panel, Applet, Frame Dialog. Components: ◦ Buttons,
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CPSC150 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Field Trip #26 Create a Find a Word Puzzle in Java By Keith Lynn.
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.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
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.
Field Trip # 21 Creating PDFs with Java By Keith Lynn.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
CMSC 202 Swing. Fall Introduction to Swing Swing is a Java package used to create GUIs The Java AWT (Abstract Window Toolkit) package is the original.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Field Trip #31 CrossWord Puzzle By Keith Lynn. JApplet A JApplet is a top-level container in Java We will use the JApplet to contain two Jpanels The first.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Field Trip #32 Digital Alarm Clock By Keith Lynn.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Field Trip #22 Creating an Excel Spreadsheet with Java By Keith Lynn.
Field Trip #25 Creating a Client/Server By Keith Lynn.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Field Trip #23 Hangman By Keith Lynn. JApplet A JApplet is a top-level container An applet is a small Java program that is executed by another program.
Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
Field Trip #27 Using Java to Download Images and Sounds By Keith Lynn.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
User Interface Components. Layout Placement of UI components in a window – Size & Position Each component occupies a rectangular region in the window.
Field Trip #34 Creating and Reading Zip Files in Java By Keith Lynn.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Field Trip #29 Creating an Event Calendar with Java By Keith Lynn.
Java Applet What is a Java Applet? How is applet compiled?
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
GUIS: Graphical User Interfaces
Introduction to Graphical Interfaces in Java: AWT and Swing
Advanced GUIs and Graphics
Presentation transcript:

Field Trip #19 Animations with Java By Keith Lynn

Overview JButton JPanel LayoutManagers Events The paint method The Graphics object Threads Drawing a stick figure and making it move

JButton A JButton is a simple component that allows us to put a push button on a container It is found in the javax.swing package A JButton by default will have margins. If the label won't display, we can obtains the insets and change the left and right margin We can attach a listener to a JButton so that clicks are detected

JPanel A simple container that holds other containers It can be used to segment a larger frame into smaller independent pieces We can change the layout of the JPanel and it won't affect other Jpanels We can set the preferred size of a Jpanel with the method setPreferredSize which will accept a Dimension object. e.g. setPreferredSize(new Dimension(50,20));

LayoutManagers LayoutManagers define how components are layed out in a Container There are predefined LayoutManagers: FlowLayout (the components are layed out linearly), GridLayout (the components are layed out in a grid), GridBagLayout (allows us to assign weights to components so that they can take up more than one row or column) If we want to layout a Container ourselves, we can remove the LayoutManager by setting it to null

Events Events occur when we do something to a component, e.g. clicking a button In order to act on the event, we assign a listener to the component Typical listeners are ActionListener for button clicks and ItemListener for selections ActionListener and ItemListener are interfaces that require us to implement certain methods If a listener is properly registered, then the corresponding method will be called when the event happens

The paint method There is a special method inherited by components that allow us to draw on a Container JApplet and JFrame inherit the paint method Subclasses of JComponent, e.g. JPanel, inherit the paintComponent method which should be used instead of paint When a Component first becomes visible or is repainted, then the paint or paintComponent methods will be called if possible

The Graphics object The paint and paintComponent methods accept a parameter of type Graphics Graphics is an abstract class. Therefore we cannot create an instance of a Graphics object. We rely on the interpreter to create a Graphics object for us Using the Graphics objects, we can draw lines, rectangles, ovals and polygons

Threads When an applet is executed, something called a thread is created A thread is a line of execution We should not place a loop in the applet's thread because it will not be able to respond to events Instead we create a seperate thread

Threads con't Thread implements the Runnable interface To create a new Thread, we can  Subclass Thread and override its run method  Create a Runnable class, and then create a Thread using the class We should never call the run method directly In order to start a Thread we call the start method To keep a thread from running forever, we can use a boolean to determine how long it should run

Drawing a Stick Figure and Making it Move In order to make the stick figure appear to move, we draw several stick figures We then display each of those figures in order When we repeat this, the figure moves