Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.

Slides:



Advertisements
Similar presentations
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Drawing in a frame – Java GUI
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
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.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Graphics Programming. In this class we will cover: Drawing lines, rectangles, and ellipses Copying an area Drawing an image.
Graphical User Interfaces, 2D Graphics & Game Programming.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 4: Interface Types.
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
CS 151: Object-Oriented Design October 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Interfaces & Polymorphism part 2:
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Polymorphism and interfaces Horstmann ch 4. Outline Interface Polymorphism Function object Anonymous class User Interface Action Scope of variables (Large)
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
2.3 The Java API Documentation. What follows are the contents of the Java Application Programming Interface (API) Documentation for the system supplied.
Chapter 4 Interface Types and Polymorphism: Graphics, Timer, Animation.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
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.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Chapter 4 Interface Types and Polymorphism Part 2.
Interface types and Polymorphism
ITEC324 Principle of CS III
“Form Ever Follows Function” Louis Henri Sullivan
Chapter 4 Interface Types and Polymorphism Part 2
Chapter 4 Interface Types and Polymorphism Part 1
Ellen Walker Hiram College
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
ITEC324 Principle of CS III
Inner Classes.
Corresponds with Chapter 5
Chapter 4 Interface Types and Polymorphism Part 1
Presentation transcript:

Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee

 Interfaces  A class defines a set of operations (the interface) and statements (implementation).  Separating the interface concept from that of a class can help in the development of “generic” reusable code.  Polymorphism  Polymorphism: Ability to select different methods according to actual object type.  Multiple classes can implement the same interface type so that it is possible to operate on a mixture of objects from any of these classes.

 public interface Icon { int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y); }

 paintIcon method receives graphics context of type Graphics  Actually a Graphics2D object in modern Java versions public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g;... }  Can draw any object that implements Shape interface Shape s =...; g2.draw(s); 

 Rectangle2D.Double constructed with  top left corner  width  height  g2.draw(new Rectangle2D.Double(x, y, width, height));  For Ellipse2D.Double, specify bounding box

 Point2D.Double is a point in the plane  Line2D.Double joins to points Point2D.Double start = new Point2D.Double(x1, y1); Point2D.Double end = new Point2D.Double(x2, y2); Shape segment = new Line2D.Double(start, end); g2.draw(segment);

 g2.drawString(text, x, y);  x, y are base point coordinates

 Fill interior of shape g2.fill(shape);  Set color for fills or strokes: g2.setColor(Color.red);  Program that draws car Ch4/icon3/CarIcon.java Ch4/icon3/CarIcon.java

 No need to name objects that are used only once. Collections.sort(students, new StudentComparatorByName());  No need to name classes that are used only once. Comparator comp = new Comparator () { public int compare(Student student1, Student student2) { return student1.getName().compareTo(student2.getName()); } };

 Anonymous class is commonly used in factory methods: public class Student { public static Comparator comparatorByName() { return new Comparator () { public int compare(Student s1, Student s2) {... } }; } }  Neat arrangement if multiple comparators make sense (by name, by grade,...)

 Ch4/action1/ActionTest.java Ch4/action1/ActionTest.java

 Method of Inner classes can access variables that are visible in the enclosing scope  e.g. actionPerformed method accesses the textField variable of the enclosing main method  If an inner class accesses a local variable from an enclosing scope, the variable must be declared as final. final JTextField textField = new TextField(Field_WIDTH);

 To construct multiple objects of the same anonymous class, you must instantiate the anonymous class in a helper method (a factory method) and then call it multiple times. Note: you should declare parameters final  Debug the following Code public static ActionListener createGreetingButtonListener(String message) { return new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText(message); } }; }

 Answer: public static ActionListener createGreetingButtonListener(final String message) { return new ActionListener() { public void actionPerformed(ActionEvent event) { textField.setText(message); } }; }

Public class ActionTest { Public static void main(String[] args) { … textField = new JTextField(FIELD_SIZE); helloButton.addActionListener( creatGreetingButtonListener(“Hello, World !”)); goodbyeButton. addActionListener( creatGreetingButtonListener(“Goodbye, World !”)); … } // the helper method in the previous slide places here }

 Javax.swing package  Timer generates a sequence of action events spaced apart at equal time interval, and notifies a designated action listener. ActionListener listener =...; final int DELAY = 1000; // 1000 millisec = 1 sec Timer t = new Timer(DELAY, listener); t.start();  The start method returns immediately. A new thread of execution is started that issues action events in the specified frequency.

 Ch4/timer/TimerTester.java Ch4/timer/TimerTester.java

Public class ShapeIcon implements Icon { Public void paintIcon(Component c, Graphics g, int x, int y) { Paint the shape } … }

ShapeIcon icon = new ShapeIcon( … ); JLabel label = new JLabel(icon); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { move the shape label.repaint(); } };

 Use timer to  Move car shapes  Draw car with CarShape  Two responsibilities:  Draw shape  Move shape  Define new interface type MoveableShape : we can decoupled the animation from the car shape.

 Name the methods to conform to standard library public interface MoveableShape { void draw(Graphics2D g2); void translate(int dx, int dy); }  CarShape class implements MoveableShape public class CarShape implements MoveableShape { public void translate(int dx, int dy) { x += dx; y += dy; }... }

 Label contains icon that draws shape  Timer action moves shape, calls repaint on label  Label needs Icon, we have MoveableShape  Supply ShapeIcon adapter class  ShapeIcon.paintIcon calls MoveableShape.draw

 Ch4/animation/MoveableShape.java Ch4/animation/MoveableShape.java  Ch4/animation/ShapeIcon.java Ch4/animation/ShapeIcon.java  Ch4/animation/AnimationTester.java Ch4/animation/AnimationTester.java  Ch4/animation/CarShape.java Ch4/animation/CarShape.java