CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

Slides:



Advertisements
Similar presentations
OLD BUSINESS : Lets talk about the next exam Project proposals – Functional Definitions Go over homework – NEW BUSINESS: Chapter 4 today GUIs Assignment.
Advertisements

© 2004 Pearson Addison-Wesley. All rights reserved2-1 Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
ELC 312 Day 6. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Agenda Questions? Problem set two Posted in WebCT  Due Sept 30  On Page 153&154.
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 Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
© 2004 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4: Writing classes Intermediate Java Programming Summer 2007.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Graphics & Applets CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2014 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design 8 th Edition John Lewis William.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Copyright © 2014 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design 8 th Edition John Lewis William.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Intro to Graphics from Chapter 2 of Java Software Solutions
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
Chapter 4 Writing Classes.
Outline The switch Statement The Conditional Operator The do Statement
Chapter 4 Writing Classes.
Chapter 4 Writing Classes.
Chapter 4 Writing Classes
Chapter 6 More Conditionals and Loops
Chapter 4 Writing Classes
Chapter 4 Writing Classes
Chapter 4 Writing Classes
4 Writing Classes Software Solutions Lewis & Loftus java 5TH EDITION
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: CSC 1051 M.A. Papalaskari, Villanova University Writing Classes – Part 3: GUIs

Graphical Objects Some objects contain information that determines how the object should be represented visually Most GUI components are graphical objects We can have some effect on how components get drawn We did this in Chapter 2 when we defined the paint method of an applet Let's look at some other examples of graphical objects CSC 1051 M.A. Papalaskari, Villanova University

Smiling Face Example See SmilingFace.java SmilingFace.java See SmilingFacePanel.java SmilingFacePanel.java CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // SmilingFace.java Author: Lewis/Loftus // // Demonstrates the use of a separate panel class. //******************************************************************** import javax.swing.JFrame; public class SmilingFace { // // Creates the main frame of the program. // public static void main (String[] args) { JFrame frame = new JFrame ("Smiling Face"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); SmilingFacePanel panel = new SmilingFacePanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); }

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // SmilingFace.java Author: Lewis/Loftus // // Demonstrates the use of a separate panel class. //******************************************************************** import javax.swing.JFrame; public class SmilingFace { // // Creates the main frame of the program. // public static void main (String[] args) { JFrame frame = new JFrame ("Smiling Face"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); SmilingFacePanel panel = new SmilingFacePanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); }

The SmilingFace program draws a face by defining the paintComponent method of a panel A UML class diagram: SmilingFace main (args : String[]) : void SmilingFacePanel BASEX: int BASEY: int paintComponent(Graphics p): void

The SmilingFace program draws a face by defining the paintComponent method of a panel A UML class diagram: SmilingFace main (args : String[]) : void SmilingFacePanel BASEX: int BASEY: int paintComponent(Graphics p): void panel 120 BASEX BASEY 60

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // SmilingFacePanel.java Author: Lewis/Loftus // // Demonstrates the use of a separate panel class. //******************************************************************** import javax.swing.JPanel; import java.awt.*; public class SmilingFacePanel extends JPanel { private final int BASEX = 120, BASEY = 60; // base point for head // // Constructor: Sets up the main characteristics of this panel. // public SmilingFacePanel () { setBackground (Color.blue); setPreferredSize (new Dimension(320, 200)); setFont (new Font("Arial", Font.BOLD, 16)); } continue

CSC 1051 M.A. Papalaskari, Villanova University continue // // Draws a face. // public void paintComponent (Graphics page) { super.paintComponent (page); page.setColor (Color.yellow); page.fillOval (BASEX, BASEY, 80, 80); // head page.fillOval (BASEX-5, BASEY+20, 90, 40); // ears page.setColor (Color.black); page.drawOval (BASEX+20, BASEY+30, 15, 7); // eyes page.drawOval (BASEX+45, BASEY+30, 15, 7); page.fillOval (BASEX+25, BASEY+31, 5, 5); // pupils page.fillOval (BASEX+50, BASEY+31, 5, 5); page.drawArc (BASEX+20, BASEY+25, 15, 7, 0, 180); // eyebrows page.drawArc (BASEX+45, BASEY+25, 15, 7, 0, 180); page.drawArc (BASEX+35, BASEY+40, 15, 10, 180, 180); // nose page.drawArc (BASEX+20, BASEY+50, 40, 15, 180, 180); // mouth continue

CSC 1051 M.A. Papalaskari, Villanova University continue page.setColor (Color.white); page.drawString ("Always remember that you are unique!", BASEX-105, BASEY-15); page.drawString ("Just like everyone else.", BASEX-45, BASEY+105); }

Jpanel Class – let’s look at Java API CSC 1051 M.A. Papalaskari, Villanova University

SmilingFacePanel The SmilingFacePanel class is derived from the JPanel class using inheritance Jpanel Class – let’s look at Java API

Smiling Face Example Every Swing component has a paintComponent method The paintComponent method accepts a Graphics object that represents the graphics context for the panel We define the paintComponent method to draw the face with appropriate calls to the Graphics methods Note the difference between drawing on a panel and adding other GUI components to a panel CSC 1051 M.A. Papalaskari, Villanova University

The SmilingFace program draws a face by defining the paintComponent method of a panel A UML class diagram: SmilingFace main (args : String[]) : void SmilingFacePanel BASEX: int BASEY: int paintComponent(Graphics p): void panel 120 BASEX BASEY 60

Objects with a draw() method The next example - Splat - is structured differently It draws a set of colored circles on a panel, but each circle is represented as a separate object that maintains its own graphical information The paintComponent method of the panel "asks" each circle to draw itself See Splat.java Splat.java See SplatPanel.java SplatPanel.java See Circle.java Circle.java CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // Splat.java Author: Lewis/Loftus // // Demonstrates the use of graphical objects. //******************************************************************** import javax.swing.*; import java.awt.*; public class Splat { // // Presents a collection of circles. // public static void main (String[] args) { JFrame frame = new JFrame ("Splat"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new SplatPanel()); frame.pack(); frame.setVisible(true); }

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // Splat.java Author: Lewis/Loftus // // Demonstrates the use of graphical objects. //******************************************************************** import javax.swing.*; import java.awt.*; public class Splat { // // Presents a collection of circles. // public static void main (String[] args) { JFrame frame = new JFrame ("Splat"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new SplatPanel()); frame.pack(); frame.setVisible(true); }

Splat main (args : String[]) : void SplatPanel circle1: Circle circle2: Circle circle3: Circle circle4: Circle circle5: Circle paintComponent(p: Graphics): void Circle diameter: int color: Color x: int y: int draw(p: Graphics): void getDiameter(): int getColor(): Color getX(): int getY(): int setDiameter(size: int): void setColor(shade: Color): void setX(upperX: int): void setY(upperY: int):void

Splat main (args : String[]) : void

Splat main (args : String[]) : void SplatPanel circle1: Circle circle2: Circle circle3: Circle circle4: Circle circle5: Circle paintComponent(p: Graphics): void circle1 circle2 circle3 circle4 circle5

Splat main (args : String[]) : void SplatPanel circle1: Circle circle2: Circle circle3: Circle circle4: Circle circle5: Circle paintComponent(p: Graphics): void Circle diameter: int color: Color x: int y: int draw(p: Graphics): void getDiameter(): int getColor(): Color getX(): int getY(): int setDiameter(size: int): void setColor(shade: Color): void setX(upperX: int): void setY(upperY: int):void circle1 circle2 circle3 circle4 circle5 diameter 60 color x 70 y 35

Splat main (args : String[]) : void SplatPanel circle1: Circle circle2: Circle circle3: Circle circle4: Circle circle5: Circle paintComponent(p: Graphics): void Circle diameter: int color: Color x: int y: int draw(p: Graphics): void getDiameter(): int getColor(): Color getX(): int getY(): int setDiameter(size: int): void setColor(shade: Color): void setX(upperX: int): void setY(upperY: int):void circle1 circle2 circle3 circle4 circle5 diameter 60 color x 70 y 35

Splat main (args : String[]) : void SplatPanel circle1: Circle circle2: Circle circle3: Circle circle4: Circle circle5: Circle paintComponent(p: Graphics): void circle1 Circle diameter: int color: Color x: int y: int draw(p: Graphics): void getDiameter(): int getColor(): Color getX(): int getY(): int setDiameter(size: int): void setColor(shade: Color): void setX(upperX: int): void setY(upperY: int):void circle2 circle3 circle4 circle5 diameter 50 color x 30 y 20

Splat main (args : String[]) : void SplatPanel circle1: Circle circle2: Circle circle3: Circle circle4: Circle circle5: Circle paintComponent(p: Graphics): void circle1 Circle diameter: int color: Color x: int y: int draw(p: Graphics): void getDiameter(): int getColor(): Color getX(): int getY(): int setDiameter(size: int): void setColor(shade: Color): void setX(upperX: int): void setY(upperY: int):void circle2 circle3 circle4 circle5 diameter 60 color x 70 y 35 diameter 50 color x 30 y 20

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // SplatPanel.java Author: Lewis/Loftus // // Demonstrates the use of graphical objects. //******************************************************************** import javax.swing.*; import java.awt.*; public class SplatPanel extends JPanel { private Circle circle1, circle2, circle3, circle4, circle5; // // Constructor: Creates five Circle objects. // public SplatPanel() { circle1 = new Circle (30, Color.red, 70, 35); circle2 = new Circle (50, Color.green, 30, 20); circle3 = new Circle (100, Color.cyan, 60, 85); circle4 = new Circle (45, Color.yellow, 170, 30); circle5 = new Circle (60, Color.blue, 200, 60); setPreferredSize (new Dimension(300, 200)); setBackground (Color.black); } continue

CSC 1051 M.A. Papalaskari, Villanova University continue // // Draws this panel by requesting that each circle draw itself. // public void paintComponent (Graphics page) { super.paintComponent(page); circle1.draw(page); circle2.draw(page); circle3.draw(page); circle4.draw(page); circle5.draw(page); }

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // Circle.java Author: Lewis/Loftus // // Represents a circle with a particular position, size, and color. //******************************************************************** import java.awt.*; public class Circle { private int diameter, x, y; private Color color; // // Constructor: Sets up this circle with the specified values. // public Circle (int size, Color shade, int upperX, int upperY) { diameter = size; color = shade; x = upperX; y = upperY; } continue

CSC 1051 M.A. Papalaskari, Villanova University continue // // Draws this circle in the specified graphics context. // public void draw (Graphics page) { page.setColor (color); page.fillOval (x, y, diameter, diameter); } // // Diameter mutator. // public void setDiameter (int size) { diameter = size; } // // Color mutator. // public void setColor (Color shade) { color = shade; } continue

CSC 1051 M.A. Papalaskari, Villanova University continue // // X mutator. // public void setX (int upperX) { x = upperX; } // // Y mutator. // public void setY (int upperY) { y = upperY; } // // Diameter accessor. // public int getDiameter () { return diameter; } continue

CSC 1051 M.A. Papalaskari, Villanova University continue // // Color accessor. // public Color getColor () { return color; } // // X accessor. // public int getX () { return x; } // // Y accessor. // public int getY () { return y; }

Homework Review Section 4.6 –Be sure to do the self-review exercises in this section Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M.A. Papalaskari, Villanova University